Compare commits
3 Commits
0.1.0
...
a933343e1b
Author | SHA1 | Date | |
---|---|---|---|
a933343e1b
|
|||
f90e938421
|
|||
09fb7caebd
|
12
Makefile
12
Makefile
@ -12,16 +12,14 @@ else
|
||||
endif
|
||||
|
||||
LUME_VERSION ?= $(shell git describe --tags --always)
|
||||
GIT_COMMIT := $(shell git rev-parse --short HEAD)
|
||||
LDFLAGS := $(LDFLAGS) \
|
||||
-X git.kill0.net/chill9/lume/cmd.Version=$(LUME_VERSION) \
|
||||
-X git.kill0.net/chill9/lume/cmd.BuildDate=$(BUILD_DATE) \
|
||||
-X git.kill0.net/chill9/lume/cmd.GitCommit=$(GIT_COMMIT)
|
||||
LDFLAGS := ${LDFLAGS} \
|
||||
-X git.kill0.net/chill9/lume/cmd.Version=${LUME_VERSION} \
|
||||
-X git.kill0.net/chill9/lume/cmd.BuildDate=${BUILD_DATE}
|
||||
|
||||
.PHONY: build
|
||||
build:
|
||||
$(Q) go build -o $(EXE) -ldflags="$(LDFLAGS)" ./cmd/lume
|
||||
$(Q) go build -o ${EXE} -ldflags="${LDFLAGS}" ./cmd/lume
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
$(Q) $(RM) $(EXE)
|
||||
${RM} ${EXE}
|
@ -15,14 +15,12 @@ const (
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
AccessToken string `toml:"access_token"`
|
||||
OutputFormat string `toml:"output_format"`
|
||||
Colors map[string][]float32 `toml:"colors"`
|
||||
AccessToken string `toml:"access_token"`
|
||||
Colors map[string][]float32 `toml:"colors"`
|
||||
}
|
||||
|
||||
type CmdArgs struct {
|
||||
Flags Flags
|
||||
Args []string
|
||||
Client *lifx.Client
|
||||
Config Config
|
||||
Name string
|
||||
@ -44,20 +42,19 @@ type Command struct {
|
||||
var commandRegistry = make(map[string]Command)
|
||||
|
||||
var (
|
||||
defaultSelector string = "all"
|
||||
defaultDuration float64 = 1.0
|
||||
defaultPower string = ""
|
||||
defaultColor string = ""
|
||||
defaultBrightness string = ""
|
||||
defaultInfrared string = ""
|
||||
defaultFast bool = false
|
||||
defaultWhiteKelvin string = ""
|
||||
defaultWhiteName string = ""
|
||||
defaultHue string = ""
|
||||
defaultSaturation string = ""
|
||||
defaultRGB string = ""
|
||||
defaultName string = ""
|
||||
defaultOutputFormat string = ""
|
||||
defaultSelector string = "all"
|
||||
defaultDuration float64 = 1.0
|
||||
defaultPower string = ""
|
||||
defaultColor string = ""
|
||||
defaultBrightness string = ""
|
||||
defaultInfrared string = ""
|
||||
defaultFast bool = false
|
||||
defaultWhiteKelvin string = ""
|
||||
defaultWhiteName string = ""
|
||||
defaultHue string = ""
|
||||
defaultSaturation string = ""
|
||||
defaultRGB string = ""
|
||||
defaultName string = ""
|
||||
)
|
||||
|
||||
func (f Flags) String(name string) string {
|
||||
|
28
cmd/help.go
28
cmd/help.go
@ -1,24 +1,32 @@
|
||||
package lumecmd
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"sort"
|
||||
)
|
||||
|
||||
func NewCmdHelp() Command {
|
||||
return Command{
|
||||
Name: "help",
|
||||
Func: HelpCmd,
|
||||
Name: "help",
|
||||
Func: HelpCmd,
|
||||
Flags: func() *flag.FlagSet {
|
||||
fs := flag.NewFlagSet("help", flag.ExitOnError)
|
||||
|
||||
return fs
|
||||
}(),
|
||||
Use: "<command>",
|
||||
Short: "Show help for a command",
|
||||
}
|
||||
}
|
||||
|
||||
func HelpCmd(args CmdArgs) (int, error) {
|
||||
if len(args.Args) == 0 {
|
||||
argv := args.Flags.Args()
|
||||
|
||||
if len(argv) == 0 {
|
||||
printHelp(commandRegistry)
|
||||
} else if len(args.Args) >= 1 {
|
||||
printCmdHelp(args.Args[0])
|
||||
} else if len(argv) >= 1 {
|
||||
printCmdHelp(argv[0])
|
||||
}
|
||||
|
||||
return ExitSuccess, nil
|
||||
@ -55,15 +63,11 @@ func printCmdHelp(name string) error {
|
||||
|
||||
if subCmd.Use != "" {
|
||||
fmt.Printf("usage:\n lume %s %s\n", subCmd.Name, subCmd.Use)
|
||||
} else {
|
||||
fmt.Printf("usage:\n lume %s\n", subCmd.Name)
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
if subCmd.Flags != nil {
|
||||
fmt.Println()
|
||||
fmt.Print("flags:\n")
|
||||
subCmd.Flags.PrintDefaults()
|
||||
}
|
||||
fmt.Print("flags:\n")
|
||||
subCmd.Flags.PrintDefaults()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
21
cmd/ls.go
21
cmd/ls.go
@ -1,8 +1,6 @@
|
||||
package lumecmd
|
||||
|
||||
import (
|
||||
"flag"
|
||||
)
|
||||
import "flag"
|
||||
|
||||
func NewCmdLs() Command {
|
||||
return Command{
|
||||
@ -14,8 +12,6 @@ func NewCmdLs() Command {
|
||||
selector := fs.String("selector", defaultSelector, "Set the selector")
|
||||
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
|
||||
|
||||
fs.String("format", defaultOutputFormat, "Set the output format")
|
||||
|
||||
return fs
|
||||
}(),
|
||||
Use: "[--selector=<selector>]",
|
||||
@ -26,23 +22,10 @@ func NewCmdLs() Command {
|
||||
func LsCmd(args CmdArgs) (int, error) {
|
||||
c := args.Client
|
||||
selector := args.Flags.String("selector")
|
||||
format := args.Flags.String("format")
|
||||
|
||||
if format == "" && args.Config.OutputFormat != "" {
|
||||
format = args.Config.OutputFormat
|
||||
}
|
||||
|
||||
lights, err := c.ListLights(selector)
|
||||
if err != nil {
|
||||
return ExitFailure, err
|
||||
}
|
||||
|
||||
switch format {
|
||||
case "table":
|
||||
PrintLightsTable(lights)
|
||||
default:
|
||||
PrintLights(lights)
|
||||
}
|
||||
|
||||
PrintLights(lights)
|
||||
return ExitSuccess, nil
|
||||
}
|
||||
|
13
cmd/main.go
13
cmd/main.go
@ -29,9 +29,8 @@ func init() {
|
||||
RegisterCommand(NewCmdVersion())
|
||||
}
|
||||
|
||||
var Version string
|
||||
var Version string = "0.1.0-pre"
|
||||
var BuildDate string
|
||||
var GitCommit string
|
||||
|
||||
const lumercFile string = ".lumerc"
|
||||
|
||||
@ -75,7 +74,6 @@ func Main(args []string) (int, error) {
|
||||
cmdArgs := CmdArgs{
|
||||
Client: c,
|
||||
Config: config,
|
||||
Args: args[2:],
|
||||
}
|
||||
|
||||
cmd, ok := GetCommand(command)
|
||||
@ -83,14 +81,11 @@ func Main(args []string) (int, error) {
|
||||
err = fmt.Errorf("lume: '%s' is not lume command. See 'lume help'", command)
|
||||
return ExitFailure, err
|
||||
}
|
||||
|
||||
fs := cmd.Flags
|
||||
if fs != nil {
|
||||
fs.Parse(args[2:])
|
||||
cmdArgs.Flags = Flags{FlagSet: fs}
|
||||
}
|
||||
cmdArgs.Name = command
|
||||
fs.Parse(args[2:])
|
||||
|
||||
cmdArgs.Flags = Flags{FlagSet: fs}
|
||||
cmdArgs.Name = command
|
||||
exitCode, err := cmd.Func(cmdArgs)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("fatal: %s", err)
|
||||
|
@ -19,8 +19,6 @@ func NewCmdPoweroff() Command {
|
||||
selector := fs.String("selector", defaultSelector, "Set the selector")
|
||||
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
|
||||
|
||||
fs.String("format", defaultOutputFormat, "Set the output format")
|
||||
|
||||
return fs
|
||||
}(),
|
||||
Use: "[--selector <selector>] [--duration <sec>]",
|
||||
@ -32,24 +30,12 @@ func PoweroffCmd(args CmdArgs) (int, error) {
|
||||
c := args.Client
|
||||
duration := args.Flags.Float64("duration")
|
||||
selector := args.Flags.String("selector")
|
||||
format := args.Flags.String("format")
|
||||
state := lifx.State{Power: "off", Duration: duration}
|
||||
|
||||
if format == "" && args.Config.OutputFormat != "" {
|
||||
format = args.Config.OutputFormat
|
||||
}
|
||||
|
||||
r, err := c.SetState(selector, state)
|
||||
if err != nil {
|
||||
return ExitFailure, err
|
||||
}
|
||||
|
||||
switch format {
|
||||
case "table":
|
||||
PrintResultsTable(r.Results)
|
||||
default:
|
||||
PrintResults(r.Results)
|
||||
}
|
||||
|
||||
PrintResults(r.Results)
|
||||
return ExitSuccess, nil
|
||||
}
|
||||
|
@ -19,8 +19,6 @@ func NewCmdPoweron() Command {
|
||||
selector := fs.String("selector", defaultSelector, "Set the selector")
|
||||
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
|
||||
|
||||
fs.String("format", defaultOutputFormat, "Set the output format")
|
||||
|
||||
return fs
|
||||
}(),
|
||||
Use: "[--selector <selector>] [--duration <sec>]",
|
||||
@ -32,24 +30,12 @@ func PoweronCmd(args CmdArgs) (int, error) {
|
||||
c := args.Client
|
||||
duration := args.Flags.Float64("duration")
|
||||
selector := args.Flags.String("selector")
|
||||
format := args.Flags.String("format")
|
||||
state := lifx.State{Power: "on", Duration: duration}
|
||||
|
||||
if format == "" && args.Config.OutputFormat != "" {
|
||||
format = args.Config.OutputFormat
|
||||
}
|
||||
|
||||
r, err := c.SetState(selector, state)
|
||||
if err != nil {
|
||||
return ExitFailure, err
|
||||
}
|
||||
|
||||
switch format {
|
||||
case "table":
|
||||
PrintResultsTable(r.Results)
|
||||
default:
|
||||
PrintResults(r.Results)
|
||||
}
|
||||
|
||||
PrintResults(r.Results)
|
||||
return ExitSuccess, nil
|
||||
}
|
||||
|
145
cmd/print.go
145
cmd/print.go
@ -1,145 +0,0 @@
|
||||
package lumecmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"git.kill0.net/chill9/lifx-go"
|
||||
"github.com/fatih/color"
|
||||
"github.com/olekukonko/tablewriter"
|
||||
)
|
||||
|
||||
func ColorizePower(s string) string {
|
||||
c := color.New(color.FgRed)
|
||||
if s == "on" {
|
||||
c = color.New(color.FgGreen)
|
||||
}
|
||||
|
||||
return c.Sprint(s)
|
||||
}
|
||||
|
||||
func ColorizeStatus(s lifx.Status) string {
|
||||
c := color.New(color.FgRed)
|
||||
if s == "ok" {
|
||||
c = color.New(color.FgGreen)
|
||||
}
|
||||
|
||||
return c.Sprint(s)
|
||||
}
|
||||
|
||||
func PrintWithIndent(indent int, s string) {
|
||||
fmt.Printf("%*s%s", indent, "", s)
|
||||
}
|
||||
|
||||
func PrintfWithIndent(indent int, format string, a ...interface{}) (n int, err error) {
|
||||
format = fmt.Sprintf("%*s%s", indent, "", format)
|
||||
return fmt.Printf(format, a...)
|
||||
}
|
||||
|
||||
func makeLightsTable(lights []lifx.Light) (hdr []string, rows [][]string) {
|
||||
hdr = []string{"ID", "Location", "Group", "Label", "Last Seen", "Power"}
|
||||
|
||||
for _, l := range lights {
|
||||
rows = append(rows, []string{
|
||||
fmt.Sprint(l.Id),
|
||||
fmt.Sprint(l.Location.Name),
|
||||
fmt.Sprint(l.Group.Name),
|
||||
fmt.Sprint(l.Label),
|
||||
fmt.Sprint(l.LastSeen.Local().Format(time.RFC3339)),
|
||||
fmt.Sprint(ColorizePower(l.Power)),
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func makeResultsTable(results []lifx.Result) (hdr []string, rows [][]string) {
|
||||
hdr = []string{"ID", "Label", "Status"}
|
||||
|
||||
for _, r := range results {
|
||||
rows = append(rows, []string{
|
||||
fmt.Sprint(r.Id),
|
||||
fmt.Sprint(r.Label),
|
||||
fmt.Sprint(ColorizeStatus(r.Status)),
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func PrintLights(lights []lifx.Light) {
|
||||
sortLights(lights)
|
||||
|
||||
table := tablewriter.NewWriter(os.Stdout)
|
||||
_, rows := makeLightsTable(lights)
|
||||
|
||||
for _, v := range rows {
|
||||
table.Append(v)
|
||||
}
|
||||
|
||||
fmt.Printf("total %d\n", len(lights))
|
||||
table.SetAlignment(tablewriter.ALIGN_LEFT)
|
||||
table.SetAutoWrapText(false)
|
||||
table.SetBorder(false)
|
||||
table.SetCenterSeparator("")
|
||||
table.SetColumnSeparator("")
|
||||
table.SetHeaderLine(false)
|
||||
table.SetNoWhiteSpace(true)
|
||||
table.SetRowSeparator("")
|
||||
table.SetTablePadding(" ")
|
||||
table.Render()
|
||||
}
|
||||
|
||||
func PrintLightsTable(lights []lifx.Light) {
|
||||
sortLights(lights)
|
||||
|
||||
table := tablewriter.NewWriter(os.Stdout)
|
||||
hdr, rows := makeLightsTable(lights)
|
||||
|
||||
for _, v := range rows {
|
||||
table.Append(v)
|
||||
}
|
||||
|
||||
table.SetHeader(hdr)
|
||||
table.Render()
|
||||
}
|
||||
|
||||
func PrintResults(results []lifx.Result) {
|
||||
sortResults(results)
|
||||
|
||||
table := tablewriter.NewWriter(os.Stdout)
|
||||
_, rows := makeResultsTable(results)
|
||||
|
||||
for _, v := range rows {
|
||||
table.Append(v)
|
||||
}
|
||||
|
||||
fmt.Printf("total %d\n", len(results))
|
||||
table.SetAlignment(tablewriter.ALIGN_LEFT)
|
||||
table.SetAutoWrapText(false)
|
||||
table.SetBorder(false)
|
||||
table.SetCenterSeparator("")
|
||||
table.SetColumnSeparator("")
|
||||
table.SetHeaderLine(false)
|
||||
table.SetNoWhiteSpace(true)
|
||||
table.SetRowSeparator("")
|
||||
table.SetTablePadding(" ")
|
||||
table.Render()
|
||||
}
|
||||
|
||||
func PrintResultsTable(results []lifx.Result) {
|
||||
sortResults(results)
|
||||
|
||||
table := tablewriter.NewWriter(os.Stdout)
|
||||
hdr, rows := makeResultsTable(results)
|
||||
|
||||
for _, v := range rows {
|
||||
table.Append(v)
|
||||
}
|
||||
|
||||
table.SetHeader(hdr)
|
||||
table.Render()
|
||||
}
|
@ -41,8 +41,6 @@ func NewCmdSetColor() Command {
|
||||
fast := fs.Bool("fast", defaultFast, "fast state")
|
||||
fs.BoolVar(fast, "f", defaultFast, "fast state")
|
||||
|
||||
fs.String("format", defaultOutputFormat, "Set the output format")
|
||||
|
||||
return fs
|
||||
}(),
|
||||
Use: "[--selector <selector>] [--power (on|off)] [--hue <hue>] [--saturation <saturation>] [--rgb <rbg>] [--name <color>] [--brightness <brightness>] [--duration <sec>] [--fast]",
|
||||
@ -54,11 +52,6 @@ func SetColorCmd(args CmdArgs) (int, error) {
|
||||
c := args.Client
|
||||
state := lifx.State{}
|
||||
selector := args.Flags.String("selector")
|
||||
format := args.Flags.String("format")
|
||||
|
||||
if format == "" && args.Config.OutputFormat != "" {
|
||||
format = args.Config.OutputFormat
|
||||
}
|
||||
|
||||
power := args.Flags.String("power")
|
||||
if power != "" {
|
||||
@ -126,12 +119,7 @@ func SetColorCmd(args CmdArgs) (int, error) {
|
||||
}
|
||||
|
||||
if !fast {
|
||||
switch format {
|
||||
case "table":
|
||||
PrintResultsTable(r.Results)
|
||||
default:
|
||||
PrintResults(r.Results)
|
||||
}
|
||||
PrintResults(r.Results)
|
||||
}
|
||||
|
||||
return ExitSuccess, nil
|
||||
|
@ -34,8 +34,6 @@ func NewCmdSetState() Command {
|
||||
fast := fs.Bool("fast", defaultFast, "fast state")
|
||||
fs.BoolVar(fast, "f", defaultFast, "fast state")
|
||||
|
||||
fs.String("format", defaultOutputFormat, "Set the output format")
|
||||
|
||||
return fs
|
||||
}(),
|
||||
Use: "[--selector <selector>] [--power (on|off)] [--color <color>] [--brightness <brightness>] [--duration <sec>] [--infrared <infrared>] [--fast]",
|
||||
@ -47,11 +45,6 @@ func SetStateCmd(args CmdArgs) (int, error) {
|
||||
c := args.Client
|
||||
state := lifx.State{}
|
||||
selector := args.Flags.String("selector")
|
||||
format := args.Flags.String("format")
|
||||
|
||||
if format == "" && args.Config.OutputFormat != "" {
|
||||
format = args.Config.OutputFormat
|
||||
}
|
||||
|
||||
power := args.Flags.String("power")
|
||||
if power != "" {
|
||||
@ -92,12 +85,7 @@ func SetStateCmd(args CmdArgs) (int, error) {
|
||||
}
|
||||
|
||||
if !fast {
|
||||
switch format {
|
||||
case "table":
|
||||
PrintResultsTable(r.Results)
|
||||
default:
|
||||
PrintResults(r.Results)
|
||||
}
|
||||
PrintResults(r.Results)
|
||||
}
|
||||
|
||||
return ExitSuccess, nil
|
||||
|
@ -37,8 +37,6 @@ func NewCmdSetWhite() Command {
|
||||
fast := fs.Bool("fast", defaultFast, "fast state")
|
||||
fs.BoolVar(fast, "f", defaultFast, "fast state")
|
||||
|
||||
fs.String("format", defaultOutputFormat, "Set the output format")
|
||||
|
||||
return fs
|
||||
}(),
|
||||
Use: "[--selector <selector>] [--power (on|off)] [--kelvin <kelvin>] [--name <color>] [--brightness <brightness>] [--duration <sec>] [--infrared] [--fast]",
|
||||
@ -50,11 +48,6 @@ func SetWhiteCmd(args CmdArgs) (int, error) {
|
||||
c := args.Client
|
||||
state := lifx.State{}
|
||||
selector := args.Flags.String("selector")
|
||||
format := args.Flags.String("format")
|
||||
|
||||
if format == "" && args.Config.OutputFormat != "" {
|
||||
format = args.Config.OutputFormat
|
||||
}
|
||||
|
||||
power := args.Flags.String("power")
|
||||
if power != "" {
|
||||
@ -110,12 +103,7 @@ func SetWhiteCmd(args CmdArgs) (int, error) {
|
||||
}
|
||||
|
||||
if !fast {
|
||||
switch format {
|
||||
case "table":
|
||||
PrintResultsTable(r.Results)
|
||||
default:
|
||||
PrintResults(r.Results)
|
||||
}
|
||||
PrintResults(r.Results)
|
||||
}
|
||||
|
||||
return ExitSuccess, nil
|
||||
|
@ -42,7 +42,7 @@ func ShowCmd(args CmdArgs) (int, error) {
|
||||
"Light ID: %s, %s, Power: %s\n",
|
||||
l.Id,
|
||||
connected(l.Connected),
|
||||
ColorizePower(l.Power),
|
||||
powerColor(l.Power),
|
||||
)
|
||||
indent += Tabstop
|
||||
PrintfWithIndent(indent, "Label: %s, ID: %s\n", l.Label, l.Id)
|
||||
|
@ -17,8 +17,6 @@ func NewCmdToggle() Command {
|
||||
selector := fs.String("selector", defaultSelector, "Set the selector")
|
||||
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
|
||||
|
||||
fs.String("format", defaultOutputFormat, "Set the output format")
|
||||
|
||||
return fs
|
||||
}(),
|
||||
Use: "[--selector <selector>] [--duration <sec>]",
|
||||
@ -30,23 +28,10 @@ func ToggleCmd(args CmdArgs) (int, error) {
|
||||
c := args.Client
|
||||
duration := args.Flags.Float64("duration")
|
||||
selector := args.Flags.String("selector")
|
||||
format := args.Flags.String("format")
|
||||
|
||||
if format == "" && args.Config.OutputFormat != "" {
|
||||
format = args.Config.OutputFormat
|
||||
}
|
||||
|
||||
r, err := c.Toggle(selector, duration)
|
||||
if err != nil {
|
||||
return ExitFailure, err
|
||||
}
|
||||
|
||||
switch format {
|
||||
case "table":
|
||||
PrintResultsTable(r.Results)
|
||||
default:
|
||||
PrintResults(r.Results)
|
||||
}
|
||||
|
||||
PrintResults(r.Results)
|
||||
return ExitSuccess, nil
|
||||
}
|
||||
|
115
cmd/util.go
115
cmd/util.go
@ -6,10 +6,116 @@ import (
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.kill0.net/chill9/lifx-go"
|
||||
)
|
||||
|
||||
func powerColor(s string) string {
|
||||
fs := "\033[1;31m%s\033[0m"
|
||||
if s == "on" {
|
||||
fs = "\033[1;32m%s\033[0m"
|
||||
}
|
||||
|
||||
return fmt.Sprintf(fs, s)
|
||||
}
|
||||
|
||||
func statusColor(s lifx.Status) string {
|
||||
fs := "\033[1;31m%s\033[0m"
|
||||
if s == "ok" {
|
||||
fs = "\033[1;32m%s\033[0m"
|
||||
}
|
||||
|
||||
return fmt.Sprintf(fs, s)
|
||||
}
|
||||
|
||||
func PrintResults(res []lifx.Result) {
|
||||
var length int
|
||||
var widths map[string]int
|
||||
|
||||
widths = make(map[string]int)
|
||||
|
||||
for _, r := range res {
|
||||
length = len(r.Id)
|
||||
if widths["id"] < length {
|
||||
widths["id"] = length
|
||||
}
|
||||
|
||||
length = len(r.Label)
|
||||
if widths["label"] < length {
|
||||
widths["label"] = length
|
||||
}
|
||||
|
||||
length = len(r.Status)
|
||||
if widths["status"] < length {
|
||||
widths["status"] = length
|
||||
}
|
||||
}
|
||||
|
||||
sortResults(res)
|
||||
|
||||
for _, r := range res {
|
||||
fmt.Printf("%*s %*s %*s\n",
|
||||
widths["id"], r.Id,
|
||||
widths["label"], r.Label,
|
||||
widths["status"], statusColor(r.Status))
|
||||
}
|
||||
}
|
||||
|
||||
func PrintLights(lights []lifx.Light) {
|
||||
var length int
|
||||
var widths map[string]int
|
||||
|
||||
widths = make(map[string]int)
|
||||
|
||||
for _, l := range lights {
|
||||
length = len(l.Id)
|
||||
if widths["id"] < length {
|
||||
widths["id"] = length
|
||||
}
|
||||
|
||||
length = len(l.Location.Name)
|
||||
if widths["location"] < length {
|
||||
widths["location"] = length
|
||||
}
|
||||
|
||||
length = len(l.Group.Name)
|
||||
if widths["group"] < length {
|
||||
widths["group"] = length
|
||||
}
|
||||
|
||||
length = len(l.Label)
|
||||
if widths["label"] < length {
|
||||
widths["label"] = length
|
||||
}
|
||||
|
||||
length = len(l.LastSeen.Local().Format(time.RFC3339))
|
||||
if widths["last_seen"] < length {
|
||||
widths["last_seen"] = length
|
||||
}
|
||||
|
||||
length = len(l.Power)
|
||||
if widths["power"] < length {
|
||||
widths["power"] = length
|
||||
}
|
||||
}
|
||||
|
||||
sortLights(lights)
|
||||
|
||||
fmt.Printf("total %d\n", len(lights))
|
||||
for _, l := range lights {
|
||||
fmt.Printf(
|
||||
"%*s %*s %*s %*s %*s %-*s\n",
|
||||
widths["id"], l.Id,
|
||||
widths["loction"], l.Location.Name,
|
||||
widths["group"], l.Group.Name,
|
||||
widths["label"], l.Label,
|
||||
widths["last_seen"], l.LastSeen.Local().Format(time.RFC3339),
|
||||
widths["power"], powerColor(l.Power),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func parseRGB(s string) (lifx.RGBColor, error) {
|
||||
var c lifx.RGBColor
|
||||
rgb := strings.SplitN(s, ",", 3)
|
||||
@ -60,3 +166,12 @@ func YesNo(v bool) string {
|
||||
}
|
||||
return "no"
|
||||
}
|
||||
|
||||
func PrintWithIndent(indent int, s string) {
|
||||
fmt.Printf("%*s%s", indent, "", s)
|
||||
}
|
||||
|
||||
func PrintfWithIndent(indent int, format string, a ...interface{}) (n int, err error) {
|
||||
format = fmt.Sprintf("%*s%s", indent, "", format)
|
||||
return fmt.Printf(format, a...)
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ package lumecmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func NewCmdVersion() Command {
|
||||
@ -16,18 +16,11 @@ func NewCmdVersion() Command {
|
||||
}
|
||||
|
||||
func VersionCmd(args CmdArgs) (int, error) {
|
||||
var b strings.Builder
|
||||
|
||||
fmt.Fprintf(&b, "lume %s", Version)
|
||||
b.WriteString(" ")
|
||||
if GitCommit != "" {
|
||||
fmt.Fprintf(&b, "(git: %s)", GitCommit)
|
||||
b.WriteString(" ")
|
||||
}
|
||||
fmt.Printf("lume %s\n", Version)
|
||||
fmt.Printf(" os/arch: %s/%s\n", runtime.GOOS, runtime.GOARCH)
|
||||
fmt.Printf(" go version: %s\n", runtime.Version())
|
||||
if BuildDate != "" {
|
||||
fmt.Fprintf(&b, "build_date: %s", BuildDate)
|
||||
fmt.Printf(" build date: %s\n", BuildDate)
|
||||
}
|
||||
|
||||
fmt.Println(b.String())
|
||||
return ExitSuccess, nil
|
||||
}
|
||||
|
2
go.mod
2
go.mod
@ -5,7 +5,5 @@ go 1.15
|
||||
require (
|
||||
git.kill0.net/chill9/lifx-go v0.0.0-20210215004437-f86c28b0a5ef
|
||||
github.com/BurntSushi/toml v0.3.1
|
||||
github.com/fatih/color v1.10.0
|
||||
github.com/olekukonko/tablewriter v0.0.5
|
||||
golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061
|
||||
)
|
||||
|
12
go.sum
12
go.sum
@ -2,17 +2,5 @@ git.kill0.net/chill9/lifx-go v0.0.0-20210215004437-f86c28b0a5ef h1:8yyXAk+qiRvro
|
||||
git.kill0.net/chill9/lifx-go v0.0.0-20210215004437-f86c28b0a5ef/go.mod h1:ZFKIcwdJ4Nqlrkn/eUHbeLt0NVhFsfxBREkVoA+jzUc=
|
||||
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
|
||||
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
|
||||
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
|
||||
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
|
||||
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
|
||||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
|
||||
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
|
||||
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
|
||||
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
|
||||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061 h1:DQmQoKxQWtyybCtX/3dIuDBcAhFszqq8YiNeS6sNu1c=
|
||||
golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
|
Reference in New Issue
Block a user