Compare commits

..

13 Commits

25 changed files with 471 additions and 86 deletions

View File

@ -4,12 +4,16 @@ BINDIR=$(CURDIR)/bin
PREFIX=/usr PREFIX=/usr
DESTDIR=bin DESTDIR=bin
BUILDDIR=$(CURDIR)/build BUILDDIR=$(CURDIR)/build
MANDIR=$(PREFIX)/share/man/man1
PKGREVISION=1
DEBBUILDDIR=$(BUILDDIR)/deb DEBBUILDDIR=$(BUILDDIR)/deb
DEBTMPLDIR=$(CURDIR)/packaging/debian DEBTMPLDIR=$(CURDIR)/packaging/debian
DEBDATE=$(shell date -R) DEBDATE=$(shell date -R)
DEBORIGSRC=lume_$(DEBVERSION).orig.tar.xz DEBORIGSRC=lume_$(DEBVERSION).orig.tar.xz
DEBORIGSRCDIR=lume-$(DEBVERSION) DEBORIGSRCDIR=lume-$(DEBVERSION)
DEBREVISION=$(PKGREVISION)
RPMVERSION=$(subst -,_,$(LUME_VERSION)) RPMVERSION=$(subst -,_,$(LUME_VERSION))
RPMBUILDDIR=$(BUILDDIR)/rpm RPMBUILDDIR=$(BUILDDIR)/rpm
@ -17,6 +21,7 @@ RPMTMPLDIR=$(CURDIR)/packaging/rpm
RPMDATE=$(shell date "+%a %b %d %Y") RPMDATE=$(shell date "+%a %b %d %Y")
RPMORIGSRC=lume-$(RPMVERSION).tar.xz RPMORIGSRC=lume-$(RPMVERSION).tar.xz
RPMORIGSRCDIR=lume-$(RPMVERSION) RPMORIGSRCDIR=lume-$(RPMVERSION)
RPMREVISION=$(PKGREVISION)
ifeq ($(OS), Windows_NT) ifeq ($(OS), Windows_NT)
EXE=$(BINDIR)/lume.exe EXE=$(BINDIR)/lume.exe
@ -50,21 +55,29 @@ build:
clean: deb-clean rpm-clean clean: deb-clean rpm-clean
$(Q) $(RM) $(EXE) $(Q) $(RM) $(EXE)
install-man:
install -p -D -m 0644 lume.1 $(DESTDIR)$(MANDIR)/lume.1
.PHONY: install .PHONY: install
install: install: install-man
$(Q) install -p -D -m 0755 $(EXE) $(DESTDIR)${PREFIX}/bin/lume $(Q) install -p -D -m 0755 $(EXE) $(DESTDIR)${PREFIX}/bin/lume
$(Q) install -p -D -m 0644 .lumerc.sample $(DESTDIR)${PREFIX}/share/lume/lumerc $(Q) install -p -D -m 0644 .lumerc.sample $(DESTDIR)${PREFIX}/share/lume/lumerc
.PHONY: deb .PHONY: deb
deb: deb: deb-clean
$(Q) mkdir -p $(DEBBUILDDIR) $(Q) mkdir -p $(DEBBUILDDIR)
$(Q) git archive --format tar --prefix lume-$(DEBVERSION)/ $(LUME_VERSION) | xz > $(DEBBUILDDIR)/$(DEBORIGSRC) $(Q) git archive --format tar --prefix lume-$(DEBVERSION)/ $(LUME_VERSION) | xz > $(DEBBUILDDIR)/$(DEBORIGSRC)
$(Q) tar xf $(DEBBUILDDIR)/$(DEBORIGSRC) -C $(DEBBUILDDIR) $(Q) tar xf $(DEBBUILDDIR)/$(DEBORIGSRC) -C $(DEBBUILDDIR)
$(Q) mkdir $(DEBBUILDDIR)/$(DEBORIGSRCDIR)/debian $(Q) mkdir $(DEBBUILDDIR)/$(DEBORIGSRCDIR)/debian
$(Q) sed -e 's/__VERSION__/$(DEBVERSION)/g' $(DEBTMPLDIR)/rules > $(DEBBUILDDIR)/$(DEBORIGSRCDIR)/debian/rules $(Q) mkdir $(DEBBUILDDIR)/$(DEBORIGSRCDIR)/debian/source
$(Q) sed -e 's/__VERSION__/$(DEBVERSION)/g' -e 's/__DATE__/$(DEBDATE)/g' $(DEBTMPLDIR)/changelog > $(DEBBUILDDIR)/$(DEBORIGSRCDIR)/debian/changelog $(Q) sed -e 's/__VERSION__/$(DEBVERSION)/g' -e 's/__REVISION__/$(DEBREVISION)/g' $(DEBTMPLDIR)/rules > $(DEBBUILDDIR)/$(DEBORIGSRCDIR)/debian/rules
$(Q) echo 9 > $(DEBBUILDDIR)/$(DEBORIGSRCDIR)/debian/compat $(Q) chmod 0755 $(DEBBUILDDIR)/$(DEBORIGSRCDIR)/debian/rules
$(Q) sed -e 's/__VERSION__/$(DEBVERSION)/g' -e 's/__DATE__/$(DEBDATE)/g' -e 's/__REVISION__/$(DEBREVISION)/g' $(DEBTMPLDIR)/changelog > $(DEBBUILDDIR)/$(DEBORIGSRCDIR)/debian/changelog
$(Q) echo 10 > $(DEBBUILDDIR)/$(DEBORIGSRCDIR)/debian/compat
$(Q) echo "3.0 (quilt)" > $(DEBBUILDDIR)/$(DEBORIGSRCDIR)/debian/source/format
$(Q) cp $(DEBTMPLDIR)/control $(DEBBUILDDIR)/$(DEBORIGSRCDIR)/debian/control $(Q) cp $(DEBTMPLDIR)/control $(DEBBUILDDIR)/$(DEBORIGSRCDIR)/debian/control
$(Q) cp $(DEBTMPLDIR)/copyright $(DEBBUILDDIR)/$(DEBORIGSRCDIR)/debian/copyright
$(Q) cp $(DEBTMPLDIR)/lume.manpages $(DEBBUILDDIR)/$(DEBORIGSRCDIR)/debian/lume.manpages
$(Q) cd $(DEBBUILDDIR)/$(DEBORIGSRCDIR) && dpkg-buildpackage -us -uc $(Q) cd $(DEBBUILDDIR)/$(DEBORIGSRCDIR) && dpkg-buildpackage -us -uc
$(Q) mv $(DEBBUILDDIR)/*.dsc $(BUILDDIR) $(Q) mv $(DEBBUILDDIR)/*.dsc $(BUILDDIR)
$(Q) mv $(DEBBUILDDIR)/*.changes $(BUILDDIR) $(Q) mv $(DEBBUILDDIR)/*.changes $(BUILDDIR)
@ -73,10 +86,10 @@ deb:
$(Q) mv $(DEBBUILDDIR)/*.tar.* $(BUILDDIR) $(Q) mv $(DEBBUILDDIR)/*.tar.* $(BUILDDIR)
.PHONY: rpm .PHONY: rpm
rpm: rpm: rpm-clean
$(Q) mkdir -p $(RPMBUILDDIR)/SPECS $(Q) mkdir -p $(RPMBUILDDIR)/SPECS
$(Q) mkdir -p $(RPMBUILDDIR)/SOURCES $(Q) mkdir -p $(RPMBUILDDIR)/SOURCES
$(Q) sed -e 's/__VERSION__/$(RPMVERSION)/g' -e 's/__DATE__/$(RPMDATE)/g' $(RPMTMPLDIR)/lume.spec > $(RPMBUILDDIR)/SPECS/lume.spec $(Q) sed -e 's/__VERSION__/$(RPMVERSION)/g' -e 's/__DATE__/$(RPMDATE)/g' -e 's/__REVISION__/$(RPMREVISION)/g' $(RPMTMPLDIR)/lume.spec > $(RPMBUILDDIR)/SPECS/lume.spec
$(Q) git archive --format tar --prefix $(RPMORIGSRCDIR)/ $(LUME_VERSION) | xz > $(RPMBUILDDIR)/SOURCES/$(RPMORIGSRC) $(Q) git archive --format tar --prefix $(RPMORIGSRCDIR)/ $(LUME_VERSION) | xz > $(RPMBUILDDIR)/SOURCES/$(RPMORIGSRC)
$(Q) rpmbuild --define "_topdir $(RPMBUILDDIR)" -ba $(RPMBUILDDIR)/SPECS/lume.spec $(Q) rpmbuild --define "_topdir $(RPMBUILDDIR)" -ba $(RPMBUILDDIR)/SPECS/lume.spec
$(Q) mv $(RPMBUILDDIR)/RPMS/*/*.rpm $(BUILDDIR) $(Q) mv $(RPMBUILDDIR)/RPMS/*/*.rpm $(BUILDDIR)

View File

@ -2,6 +2,7 @@ package lumecmd
import ( import (
"flag" "flag"
"fmt"
"git.kill0.net/chill9/lifx-go" "git.kill0.net/chill9/lifx-go"
) )
@ -16,7 +17,8 @@ func NewCmdBreathe() Command {
selector := fs.String("selector", defaultSelector, "Set the selector") selector := fs.String("selector", defaultSelector, "Set the selector")
fs.StringVar(selector, "s", defaultSelector, "Set the selector") fs.StringVar(selector, "s", defaultSelector, "Set the selector")
fs.String("color", defaultColor, "The color to use for the breathe effect") color := fs.String("color", defaultColor, "The color to use for the breathe effect")
fs.StringVar(color, "c", defaultColor, "The color to use for the breathe effect")
fs.String("from-color", defaultColor, "The color to start the effect from") fs.String("from-color", defaultColor, "The color to start the effect from")
@ -43,7 +45,10 @@ func BreatheCmd(ctx Context) (int, error) {
c := ctx.Client c := ctx.Client
breathe := lifx.NewBreathe() breathe := lifx.NewBreathe()
selector := ctx.Flags.String("selector") selector := ctx.Flags.String("selector")
format := ctx.Flags.String("output-format") format, err := getOutputFormatFromFlags(ctx.Flags)
if err != nil {
return ExitFailure, err
}
if format == "" && ctx.Config.OutputFormat != "" { if format == "" && ctx.Config.OutputFormat != "" {
format = ctx.Config.OutputFormat format = ctx.Config.OutputFormat
@ -98,7 +103,7 @@ func BreatheCmd(ctx Context) (int, error) {
} }
p = NewPrinter(format) p = NewPrinter(format)
p.Results(r.Results) fmt.Print(p.Results(r.Results))
return ExitSuccess, nil return ExitSuccess, nil
} }

View File

@ -1,6 +1,7 @@
package lumecmd package lumecmd
import ( import (
"errors"
"flag" "flag"
"fmt" "fmt"
"strconv" "strconv"
@ -100,6 +101,22 @@ func GetCommand(name string) (Command, bool) {
func mergeGlobalFlags(fs *flag.FlagSet) { func mergeGlobalFlags(fs *flag.FlagSet) {
fs.Bool("debug", false, "Enable debug mode") fs.Bool("debug", false, "Enable debug mode")
outputFormat := fs.String("output-format", defaultOutputFormat, "Set the output format") formatTable := fs.Bool("table", false, "Format output as an ASCII table")
fs.StringVar(outputFormat, "o", defaultOutputFormat, "Set the output format") fs.BoolVar(formatTable, "t", false, "Format output as an ASCII table")
fs.Bool("simple", false, "Format output simply")
}
func getOutputFormatFromFlags(fs Flags) (string, error) {
formatSimple := fs.Bool("simple")
formatTable := fs.Bool("table")
switch {
case formatSimple && formatTable:
return "", errors.New("only one output format permitted")
case formatTable:
return "table", nil
default:
return "simple", nil
}
} }

View File

@ -3,6 +3,7 @@ package lumecmd
import ( import (
"fmt" "fmt"
"sort" "sort"
"strings"
) )
func NewCmdHelp() Command { func NewCmdHelp() Command {
@ -16,15 +17,21 @@ func NewCmdHelp() Command {
func HelpCmd(ctx Context) (int, error) { func HelpCmd(ctx Context) (int, error) {
if len(ctx.Args) == 0 { if len(ctx.Args) == 0 {
printHelp(commandRegistry) fmt.Print(printHelp(commandRegistry))
} else if len(ctx.Args) >= 1 { } else if len(ctx.Args) >= 1 {
printCmdHelp(ctx.Args[0]) if cmdHelp, err := printCmdHelp(ctx.Args[0]); err == nil {
fmt.Print(cmdHelp)
} else {
fmt.Print(err)
}
} }
return ExitSuccess, nil return ExitSuccess, nil
} }
func printHelp(commands map[string]Command) { func printHelp(commands map[string]Command) string {
var b strings.Builder
var maxLen, cmdLen int var maxLen, cmdLen int
var keys []string var keys []string
@ -36,34 +43,45 @@ func printHelp(commands map[string]Command) {
} }
} }
fmt.Printf("usage:\n lume <command> [<args...>]") fmt.Fprintf(&b, "usage:\n lume <command> [<args...>]")
fmt.Println() fmt.Fprintln(&b)
fmt.Println("\ncommands:") fmt.Fprintln(&b, "\ncommands:")
sort.Strings(keys) sort.Strings(keys)
for _, k := range keys { for _, k := range keys {
c := commands[k] c := commands[k]
fmt.Printf(" %-*s %s\n", maxLen, c.Name, c.Short) fmt.Fprintf(&b, " %-*s %s\n", maxLen, c.Name, c.Short)
} }
return b.String()
} }
func printCmdHelp(name string) error { func printCmdHelp(name string) (string, error) {
var b strings.Builder
subCmd, ok := commandRegistry[name] subCmd, ok := commandRegistry[name]
if !ok { if !ok {
return fmt.Errorf("unknown commnnd: %s\n", name) return "", fmt.Errorf("unknown commnnd: %s\n", name)
} }
if subCmd.Use != "" { if subCmd.Use != "" {
fmt.Printf("usage:\n lume %s %s\n", subCmd.Name, subCmd.Use) fmt.Fprintf(&b, "usage:\n lume %s %s\n", subCmd.Name, subCmd.Use)
} else { } else {
fmt.Printf("usage:\n lume %s\n", subCmd.Name) fmt.Fprintf(&b, "usage:\n lume %s\n", subCmd.Name)
} }
if subCmd.Flags != nil { if subCmd.Flags != nil {
fmt.Println() out := subCmd.Flags.Output()
fmt.Print("flags:\n") defer subCmd.Flags.SetOutput(out)
fmt.Fprintln(&b)
fmt.Fprint(&b, "flags:\n")
subCmd.Flags.SetOutput(&b)
subCmd.Flags.PrintDefaults() subCmd.Flags.PrintDefaults()
} }
return nil return b.String(), nil
} }

View File

@ -2,6 +2,7 @@ package lumecmd
import ( import (
"flag" "flag"
"fmt"
) )
func NewCmdLs() Command { func NewCmdLs() Command {
@ -26,7 +27,10 @@ func LsCmd(ctx Context) (int, error) {
c := ctx.Client c := ctx.Client
selector := ctx.Flags.String("selector") selector := ctx.Flags.String("selector")
format := ctx.Flags.String("output-format") format, err := getOutputFormatFromFlags(ctx.Flags)
if err != nil {
return ExitFailure, err
}
if format == "" && ctx.Config.OutputFormat != "" { if format == "" && ctx.Config.OutputFormat != "" {
format = ctx.Config.OutputFormat format = ctx.Config.OutputFormat
@ -39,7 +43,7 @@ func LsCmd(ctx Context) (int, error) {
} }
p = NewPrinter(format) p = NewPrinter(format)
p.Lights(lights) fmt.Print(p.Lights(lights))
return ExitSuccess, nil return ExitSuccess, nil
} }

View File

@ -20,6 +20,7 @@ func init() {
RegisterCommand(NewCmdToggle()) RegisterCommand(NewCmdToggle())
RegisterCommand(NewCmdVersion()) RegisterCommand(NewCmdVersion())
RegisterCommand(NewCmdBreathe()) RegisterCommand(NewCmdBreathe())
RegisterCommand(NewCmdValidate())
} }
var Version string var Version string

View File

@ -2,6 +2,7 @@ package lumecmd
import ( import (
"flag" "flag"
"fmt"
"git.kill0.net/chill9/lifx-go" "git.kill0.net/chill9/lifx-go"
) )
@ -32,8 +33,11 @@ func PoweroffCmd(ctx Context) (int, error) {
c := ctx.Client c := ctx.Client
duration := ctx.Flags.Float64("duration") duration := ctx.Flags.Float64("duration")
selector := ctx.Flags.String("selector") selector := ctx.Flags.String("selector")
format := ctx.Flags.String("output-format")
state := lifx.State{Power: "off", Duration: duration} state := lifx.State{Power: "off", Duration: duration}
format, err := getOutputFormatFromFlags(ctx.Flags)
if err != nil {
return ExitFailure, err
}
if format == "" && ctx.Config.OutputFormat != "" { if format == "" && ctx.Config.OutputFormat != "" {
format = ctx.Config.OutputFormat format = ctx.Config.OutputFormat
@ -45,7 +49,7 @@ func PoweroffCmd(ctx Context) (int, error) {
} }
p = NewPrinter(format) p = NewPrinter(format)
p.Results(r.Results) fmt.Print(p.Results(r.Results))
return ExitSuccess, nil return ExitSuccess, nil
} }

View File

@ -2,6 +2,7 @@ package lumecmd
import ( import (
"flag" "flag"
"fmt"
"git.kill0.net/chill9/lifx-go" "git.kill0.net/chill9/lifx-go"
) )
@ -32,8 +33,11 @@ func PoweronCmd(ctx Context) (int, error) {
c := ctx.Client c := ctx.Client
duration := ctx.Flags.Float64("duration") duration := ctx.Flags.Float64("duration")
selector := ctx.Flags.String("selector") selector := ctx.Flags.String("selector")
format := ctx.Flags.String("output-format")
state := lifx.State{Power: "on", Duration: duration} state := lifx.State{Power: "on", Duration: duration}
format, err := getOutputFormatFromFlags(ctx.Flags)
if err != nil {
return ExitFailure, err
}
if format == "" && ctx.Config.OutputFormat != "" { if format == "" && ctx.Config.OutputFormat != "" {
format = ctx.Config.OutputFormat format = ctx.Config.OutputFormat
@ -45,7 +49,7 @@ func PoweronCmd(ctx Context) (int, error) {
} }
p = NewPrinter(format) p = NewPrinter(format)
p.Results(r.Results) fmt.Print(p.Results(r.Results))
return ExitSuccess, nil return ExitSuccess, nil
} }

View File

@ -2,7 +2,8 @@ package lumecmd
import ( import (
"fmt" "fmt"
"os" "io"
"strings"
"time" "time"
"git.kill0.net/chill9/lifx-go" "git.kill0.net/chill9/lifx-go"
@ -11,8 +12,8 @@ import (
) )
type Printer interface { type Printer interface {
Results(results []lifx.Result) Results(results []lifx.Result) string
Lights(lights []lifx.Light) Lights(lights []lifx.Light) string
} }
type defaultPrinter struct{} type defaultPrinter struct{}
@ -28,17 +29,19 @@ func NewPrinter(format string) Printer {
} }
} }
func (dp *defaultPrinter) Results(results []lifx.Result) { func (dp *defaultPrinter) Results(results []lifx.Result) string {
var b strings.Builder
sortResults(results) sortResults(results)
table := tablewriter.NewWriter(os.Stdout) table := tablewriter.NewWriter(&b)
_, rows := makeResultsTable(results) _, rows := makeResultsTable(results)
for _, v := range rows { for _, v := range rows {
table.Append(v) table.Append(v)
} }
fmt.Printf("total %d\n", len(results)) fmt.Fprintf(&b, "total %d\n", len(results))
table.SetAlignment(tablewriter.ALIGN_LEFT) table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetAutoWrapText(false) table.SetAutoWrapText(false)
table.SetBorder(false) table.SetBorder(false)
@ -49,12 +52,16 @@ func (dp *defaultPrinter) Results(results []lifx.Result) {
table.SetRowSeparator("") table.SetRowSeparator("")
table.SetTablePadding(" ") table.SetTablePadding(" ")
table.Render() table.Render()
return b.String()
} }
func (tp *tablePrinter) Results(results []lifx.Result) { func (tp *tablePrinter) Results(results []lifx.Result) string {
var b strings.Builder
sortResults(results) sortResults(results)
table := tablewriter.NewWriter(os.Stdout) table := tablewriter.NewWriter(&b)
hdr, rows := makeResultsTable(results) hdr, rows := makeResultsTable(results)
for _, v := range rows { for _, v := range rows {
@ -63,19 +70,23 @@ func (tp *tablePrinter) Results(results []lifx.Result) {
table.SetHeader(hdr) table.SetHeader(hdr)
table.Render() table.Render()
return b.String()
} }
func (dp *defaultPrinter) Lights(lights []lifx.Light) { func (dp *defaultPrinter) Lights(lights []lifx.Light) string {
var b strings.Builder
sortLights(lights) sortLights(lights)
table := tablewriter.NewWriter(os.Stdout) table := tablewriter.NewWriter(&b)
_, rows := makeLightsTable(lights) _, rows := makeLightsTable(lights)
for _, v := range rows { for _, v := range rows {
table.Append(v) table.Append(v)
} }
fmt.Printf("total %d\n", len(lights)) fmt.Fprintf(&b, "total %d\n", len(lights))
table.SetAlignment(tablewriter.ALIGN_LEFT) table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetAutoWrapText(false) table.SetAutoWrapText(false)
table.SetBorder(false) table.SetBorder(false)
@ -86,12 +97,16 @@ func (dp *defaultPrinter) Lights(lights []lifx.Light) {
table.SetRowSeparator("") table.SetRowSeparator("")
table.SetTablePadding(" ") table.SetTablePadding(" ")
table.Render() table.Render()
return b.String()
} }
func (tp *tablePrinter) Lights(lights []lifx.Light) { func (tp *tablePrinter) Lights(lights []lifx.Light) string {
var b strings.Builder
sortLights(lights) sortLights(lights)
table := tablewriter.NewWriter(os.Stdout) table := tablewriter.NewWriter(&b)
hdr, rows := makeLightsTable(lights) hdr, rows := makeLightsTable(lights)
for _, v := range rows { for _, v := range rows {
@ -100,6 +115,8 @@ func (tp *tablePrinter) Lights(lights []lifx.Light) {
table.SetHeader(hdr) table.SetHeader(hdr)
table.Render() table.Render()
return b.String()
} }
func ColorizeIndicator(s string) string { func ColorizeIndicator(s string) string {
@ -138,6 +155,11 @@ func PrintfWithIndent(indent int, format string, a ...interface{}) (n int, err e
return fmt.Printf(format, a...) return fmt.Printf(format, a...)
} }
func FprintfWithIndent(w io.Writer, indent int, format string, a ...interface{}) (n int, err error) {
format = fmt.Sprintf("%*s%s", indent, "", format)
return fmt.Fprintf(w, format, a...)
}
func makeLightsTable(lights []lifx.Light) (hdr []string, rows [][]string) { func makeLightsTable(lights []lifx.Light) (hdr []string, rows [][]string) {
hdr = []string{"", "ID", "Location", "Group", "Label", "Last Seen", "Power"} hdr = []string{"", "ID", "Location", "Group", "Label", "Last Seen", "Power"}

View File

@ -54,7 +54,10 @@ func SetColorCmd(ctx Context) (int, error) {
c := ctx.Client c := ctx.Client
state := lifx.State{} state := lifx.State{}
selector := ctx.Flags.String("selector") selector := ctx.Flags.String("selector")
format := ctx.Flags.String("output-format") format, err := getOutputFormatFromFlags(ctx.Flags)
if err != nil {
return ExitFailure, err
}
if format == "" && ctx.Config.OutputFormat != "" { if format == "" && ctx.Config.OutputFormat != "" {
format = ctx.Config.OutputFormat format = ctx.Config.OutputFormat
@ -127,7 +130,7 @@ func SetColorCmd(ctx Context) (int, error) {
if !fast { if !fast {
p = NewPrinter(format) p = NewPrinter(format)
p.Results(r.Results) fmt.Print(p.Results(r.Results))
} }
return ExitSuccess, nil return ExitSuccess, nil

View File

@ -2,6 +2,7 @@ package lumecmd
import ( import (
"flag" "flag"
"fmt"
"git.kill0.net/chill9/lifx-go" "git.kill0.net/chill9/lifx-go"
) )
@ -47,7 +48,10 @@ func SetStateCmd(ctx Context) (int, error) {
c := ctx.Client c := ctx.Client
state := lifx.State{} state := lifx.State{}
selector := ctx.Flags.String("selector") selector := ctx.Flags.String("selector")
format := ctx.Flags.String("output-format") format, err := getOutputFormatFromFlags(ctx.Flags)
if err != nil {
return ExitFailure, err
}
if format == "" && ctx.Config.OutputFormat != "" { if format == "" && ctx.Config.OutputFormat != "" {
format = ctx.Config.OutputFormat format = ctx.Config.OutputFormat
@ -93,7 +97,7 @@ func SetStateCmd(ctx Context) (int, error) {
if !fast { if !fast {
p = NewPrinter(format) p = NewPrinter(format)
p.Results(r.Results) fmt.Print(p.Results(r.Results))
} }
return ExitSuccess, nil return ExitSuccess, nil

View File

@ -2,6 +2,7 @@ package lumecmd
import ( import (
"flag" "flag"
"fmt"
"git.kill0.net/chill9/lifx-go" "git.kill0.net/chill9/lifx-go"
) )
@ -50,7 +51,10 @@ func SetWhiteCmd(ctx Context) (int, error) {
c := ctx.Client c := ctx.Client
state := lifx.State{} state := lifx.State{}
selector := ctx.Flags.String("selector") selector := ctx.Flags.String("selector")
format := ctx.Flags.String("output-format") format, err := getOutputFormatFromFlags(ctx.Flags)
if err != nil {
return ExitFailure, err
}
if format == "" && ctx.Config.OutputFormat != "" { if format == "" && ctx.Config.OutputFormat != "" {
format = ctx.Config.OutputFormat format = ctx.Config.OutputFormat
@ -111,7 +115,7 @@ func SetWhiteCmd(ctx Context) (int, error) {
if !fast { if !fast {
p = NewPrinter(format) p = NewPrinter(format)
p.Results(r.Results) fmt.Print(p.Results(r.Results))
} }
return ExitSuccess, nil return ExitSuccess, nil

View File

@ -3,6 +3,7 @@ package lumecmd
import ( import (
"flag" "flag"
"fmt" "fmt"
"strings"
) )
const Tabstop int = 2 const Tabstop int = 2
@ -26,6 +27,8 @@ func NewCmdShow() Command {
func ShowCmd(ctx Context) (int, error) { func ShowCmd(ctx Context) (int, error) {
var indent int var indent int
var b strings.Builder
c := ctx.Client c := ctx.Client
selector := ctx.Flags.String("selector") selector := ctx.Flags.String("selector")
lights, err := c.ListLights(selector) lights, err := c.ListLights(selector)
@ -38,7 +41,8 @@ func ShowCmd(ctx Context) (int, error) {
for i, l := range lights { for i, l := range lights {
indent = 0 indent = 0
fmt.Printf( fmt.Fprintf(
&b,
"%s Light ID: %s, %s, Power: %s\n", "%s Light ID: %s, %s, Power: %s\n",
ColorizeIndicator(l.Power), ColorizeIndicator(l.Power),
l.Id, l.Id,
@ -46,41 +50,43 @@ func ShowCmd(ctx Context) (int, error) {
ColorizePower(l.Power), ColorizePower(l.Power),
) )
indent += Tabstop + 2 indent += Tabstop + 2
PrintfWithIndent(indent, "Label: %s, ID: %s\n", l.Label, l.Id) FprintfWithIndent(&b, indent, "Label: %s, ID: %s\n", l.Label, l.Id)
PrintfWithIndent(indent, "UUID: %s\n", l.UUID) FprintfWithIndent(&b, indent, "UUID: %s\n", l.UUID)
PrintfWithIndent(indent, "Location: %s, ID: %s\n", l.Location.Name, l.Location.Id) FprintfWithIndent(&b, indent, "Location: %s, ID: %s\n", l.Location.Name, l.Location.Id)
PrintfWithIndent(indent, "Group: %s, ID: %s\n", l.Group.Name, l.Group.Id) FprintfWithIndent(&b, indent, "Group: %s, ID: %s\n", l.Group.Name, l.Group.Id)
PrintfWithIndent(indent, "Color: Hue: %.1f, Saturation: %.1f%%, Kelvin: %d\n", FprintfWithIndent(&b, indent, "Color: Hue: %.1f, Saturation: %.1f%%, Kelvin: %d\n",
*l.Color.H, *l.Color.S, *l.Color.K) *l.Color.H, *l.Color.S, *l.Color.K)
PrintfWithIndent(indent, "Brightness: %.1f%%\n", l.Brightness*100) FprintfWithIndent(&b, indent, "Brightness: %.1f%%\n", l.Brightness*100)
if l.Effect != "" { if l.Effect != "" {
PrintfWithIndent(indent, "Effect: %s\n", l.Effect) FprintfWithIndent(&b, indent, "Effect: %s\n", l.Effect)
} }
PrintfWithIndent(indent, "Product: %s\n", l.Product.Name) FprintfWithIndent(&b, indent, "Product: %s\n", l.Product.Name)
PrintfWithIndent(indent, "Capabilities: ") FprintfWithIndent(&b, indent, "Capabilities: ")
fmt.Printf("Color: %s, ", YesNo(l.Product.Capabilities.HasColor)) fmt.Fprintf(&b, "Color: %s, ", YesNo(l.Product.Capabilities.HasColor))
fmt.Printf("Variable Color Temp: %s, ", YesNo(l.Product.Capabilities.HasVariableColorTemp)) fmt.Fprintf(&b, "Variable Color Temp: %s, ", YesNo(l.Product.Capabilities.HasVariableColorTemp))
fmt.Printf("IR: %s, ", YesNo(l.Product.Capabilities.HasIR)) fmt.Fprintf(&b, "IR: %s, ", YesNo(l.Product.Capabilities.HasIR))
fmt.Printf("Chain: %s, ", YesNo(l.Product.Capabilities.HasChain)) fmt.Fprintf(&b, "Chain: %s, ", YesNo(l.Product.Capabilities.HasChain))
fmt.Printf("Multizone: %s, ", YesNo(l.Product.Capabilities.HasMultizone)) fmt.Fprintf(&b, "Multizone: %s, ", YesNo(l.Product.Capabilities.HasMultizone))
fmt.Printf("Min Kelvin: %.1f, ", l.Product.Capabilities.MinKelvin) fmt.Fprintf(&b, "Min Kelvin: %.1f, ", l.Product.Capabilities.MinKelvin)
fmt.Printf("Max Kelvin: %.1f ", l.Product.Capabilities.MaxKelvin) fmt.Fprintf(&b, "Max Kelvin: %.1f ", l.Product.Capabilities.MaxKelvin)
fmt.Println() fmt.Fprintln(&b)
// List applicable selectors (most to least specific) // List applicable selectors (most to least specific)
PrintfWithIndent(indent, "Selectors:\n") FprintfWithIndent(&b, indent, "Selectors:\n")
indent += Tabstop indent += Tabstop
PrintfWithIndent(indent, "id:%s\n", l.Id) FprintfWithIndent(&b, indent, "id:%s\n", l.Id)
PrintfWithIndent(indent, "label:%s\n", l.Label) FprintfWithIndent(&b, indent, "label:%s\n", l.Label)
PrintfWithIndent(indent, "group_id:%s\n", l.Group.Id) FprintfWithIndent(&b, indent, "group_id:%s\n", l.Group.Id)
PrintfWithIndent(indent, "group:%s\n", l.Group.Name) FprintfWithIndent(&b, indent, "group:%s\n", l.Group.Name)
PrintfWithIndent(indent, "location_id:%s\n", l.Location.Id) FprintfWithIndent(&b, indent, "location_id:%s\n", l.Location.Id)
PrintfWithIndent(indent, "location:%s\n", l.Location.Name) FprintfWithIndent(&b, indent, "location:%s\n", l.Location.Name)
indent -= Tabstop indent -= Tabstop
PrintfWithIndent(indent, "Last Seen: %s (%.1fs ago)\n", l.LastSeen, l.SecondsLastSeen) FprintfWithIndent(&b, indent, "Last Seen: %s (%.1fs ago)\n", l.LastSeen, l.SecondsLastSeen)
if i < len(lights)-1 { if i < len(lights)-1 {
fmt.Println() fmt.Fprintln(&b)
} }
fmt.Print(b.String())
} }
return ExitSuccess, nil return ExitSuccess, nil
} }

View File

@ -2,6 +2,7 @@ package lumecmd
import ( import (
"flag" "flag"
"fmt"
) )
func NewCmdToggle() Command { func NewCmdToggle() Command {
@ -30,7 +31,10 @@ func ToggleCmd(ctx Context) (int, error) {
c := ctx.Client c := ctx.Client
duration := ctx.Flags.Float64("duration") duration := ctx.Flags.Float64("duration")
selector := ctx.Flags.String("selector") selector := ctx.Flags.String("selector")
format := ctx.Flags.String("output-format") format, err := getOutputFormatFromFlags(ctx.Flags)
if err != nil {
return ExitFailure, err
}
if format == "" && ctx.Config.OutputFormat != "" { if format == "" && ctx.Config.OutputFormat != "" {
format = ctx.Config.OutputFormat format = ctx.Config.OutputFormat
@ -42,7 +46,7 @@ func ToggleCmd(ctx Context) (int, error) {
} }
p = NewPrinter(format) p = NewPrinter(format)
p.Results(r.Results) fmt.Print(p.Results(r.Results))
return ExitSuccess, nil return ExitSuccess, nil
} }

51
cmd/validate.go Normal file
View File

@ -0,0 +1,51 @@
package lumecmd
import (
"errors"
"flag"
"fmt"
"strings"
"git.kill0.net/chill9/lifx-go"
)
func NewCmdValidate() Command {
return Command{
Name: "validate",
Func: ValidateCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("validate", flag.ExitOnError)
return fs
}(),
Use: "<command>",
Short: "Validate a color string",
}
}
func ValidateCmd(ctx Context) (int, error) {
var b strings.Builder
c := ctx.Client
if len(ctx.Args) != 1 {
fmt.Print(printCmdHelp(ctx.Name))
return ExitFailure, nil
}
color := lifx.NamedColor(ctx.Args[0])
i, err := c.ValidateColor(color)
if err != nil {
return ExitFailure, err
}
if validColor, ok := i.(*lifx.HSBKColor); ok {
fmt.Fprintln(&b, validColor)
} else {
return ExitFailure, errors.New("go type %T but wanted *HSBKColor")
}
fmt.Print(b.String())
return ExitSuccess, nil
}

2
go.mod
View File

@ -3,7 +3,7 @@ module git.kill0.net/chill9/lume
go 1.15 go 1.15
require ( require (
git.kill0.net/chill9/lifx-go v0.0.0-20210329222320-2107a0586447 git.kill0.net/chill9/lifx-go v0.0.0-20210418161634-4c1678b62c73
github.com/BurntSushi/toml v0.3.1 github.com/BurntSushi/toml v0.3.1
github.com/fatih/color v1.10.0 github.com/fatih/color v1.10.0
github.com/mattn/go-runewidth v0.0.10 // indirect github.com/mattn/go-runewidth v0.0.10 // indirect

2
go.sum
View File

@ -4,6 +4,8 @@ git.kill0.net/chill9/lifx-go v0.0.0-20210323044657-dbe1c40e1621 h1:koWq2W08Hjmvs
git.kill0.net/chill9/lifx-go v0.0.0-20210323044657-dbe1c40e1621/go.mod h1:jInpjEqTBhrFpQKk7zPIWISvgjjfS2djXeKB3yB/8dY= git.kill0.net/chill9/lifx-go v0.0.0-20210323044657-dbe1c40e1621/go.mod h1:jInpjEqTBhrFpQKk7zPIWISvgjjfS2djXeKB3yB/8dY=
git.kill0.net/chill9/lifx-go v0.0.0-20210329222320-2107a0586447 h1:tN+zR5aszesrZRrhS3uOqAIWLcADCIH7GFJ6SOQS9r0= git.kill0.net/chill9/lifx-go v0.0.0-20210329222320-2107a0586447 h1:tN+zR5aszesrZRrhS3uOqAIWLcADCIH7GFJ6SOQS9r0=
git.kill0.net/chill9/lifx-go v0.0.0-20210329222320-2107a0586447/go.mod h1:jInpjEqTBhrFpQKk7zPIWISvgjjfS2djXeKB3yB/8dY= git.kill0.net/chill9/lifx-go v0.0.0-20210329222320-2107a0586447/go.mod h1:jInpjEqTBhrFpQKk7zPIWISvgjjfS2djXeKB3yB/8dY=
git.kill0.net/chill9/lifx-go v0.0.0-20210418161634-4c1678b62c73 h1:fteCAelwAcfam2Q8eeJFyK4+sXGOpR6Me5YMKBi+MYY=
git.kill0.net/chill9/lifx-go v0.0.0-20210418161634-4c1678b62c73/go.mod h1:jInpjEqTBhrFpQKk7zPIWISvgjjfS2djXeKB3yB/8dY=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= 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 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=

186
lume.1 Normal file
View File

@ -0,0 +1,186 @@
.Dd $Mdocdate$
.Dt lume \&1 "User Commands"
.Sh NAME
.Nm lume
.Nd CLI tool for the LIFX HTTP API
.Sh SYNOPSIS
.Nm lume
.Ar COMMAND
.Op Ar OPTIONS ...
.Sh COMMANDS
.Bl -tag -width Ds
.It Xo Ic breathe
.Op Fl s Ar selector | Fl Fl selector Ns = Ns Ar selector
.Fl c | Fl Fl color
.Op Fl Fl from-color Ns = Ns Ar color
.Op Fl Fl cycles Ns = Ns Ar cycles
.Op Fl Fl peak Ns = Ns Ar peak
.Op Fl Fl period Ns = Ns Ar period
.Op Fl Fl persist
.Op Fl Fl power-on
.Xc
.It Xo Ic help
.Op Ar COMMAND
.Xc
Print the help message. If a command is specified, then show the subcommand's help message.
.It Xo Ic ls
.Op Fl s Ar selector | Fl Fl selector Ns = Ns Ar selector
.Op Fl Fl simple | Fl Fl table
.Xc
List the lights and their basic state
.It Xo Ic poweroff
.Op Fl s Ar selector | Fl Fl selector Ns = Ns Ar selector
.Op Fl Fl simple | Fl Fl table
.Op Fl d | Fl Fl duration
.Xc
Power off lights
.It Xo Ic poweron
.Op Fl s Ar selector | Fl Fl selector Ns = Ns Ar selector
.Op Fl Fl simple | Fl Fl table
.Op Fl d | Fl Fl duration
.Xc
Power off lights
.It Xo Ic set-color
.Op Fl b Ar brightness | Fl Fl brightness Ns = Ns Ar brightness
.Op Fl d | Fl Fl duration
.Op Fl f Ar fast | Fl Fl fast Ns = Ns Ar fast
.Op Fl H Ar hue | Fl Fl hue Ns = Ns Ar hue
.Op Fl n Ar name | Fl Fl name Ns = Ns Ar name
.Op Fl p Ar power | Fl Fl power Ns = Ns Ar power
.Op Fl r Ar rgb | Fl Fl rgb Ns = Ns Ar rgb
.Op Fl S Ar saturation | Fl Fl saturation Ns = Ns Ar saturation
.Op Fl s Ar selector | Fl Fl selector Ns = Ns Ar selector
.Op Fl Fl simple | Fl Fl table
.Xc
Set light color
.It Xo Ic set-state
.Op Fl b Ar brightness | Fl Fl brightness Ns = Ns Ar brightness
.Op Fl c | Fl Fl color
.Op Fl d | Fl Fl duration
.Op Fl f Ar fast | Fl Fl fast Ns = Ns Ar fast
.Op Fl i Ar infrared | Fl Fl infrared Ns = Ns Ar infrared
.Op Fl p Ar power | Fl Fl power Ns = Ns Ar power
.Op Fl s Ar selector | Fl Fl selector Ns = Ns Ar selector
.Op Fl Fl simple | Fl Fl table
.Xc
Set light properties
.It Xo Ic set-white
.Op Fl b Ar brightness | Fl Fl brightness Ns = Ns Ar brightness
.Op Fl d | Fl Fl duration
.Op Fl f Ar fast | Fl Fl fast Ns = Ns Ar fast
.Op Fl i Ar infrared | Fl Fl infrared Ns = Ns Ar infrared
.Op Fl k Ar kelvin | Fl Fl kelvin Ns = Ns Ar kelvin
.Op Fl n Ar name | Fl Fl name Ns = Ns Ar name
.Op Fl p Ar power | Fl Fl power Ns = Ns Ar power
.Op Fl s Ar selector | Fl Fl selector Ns = Ns Ar selector
.Op Fl Fl simple | Fl Fl table
.Xc
Set light white levels
.It Xo Ic show
.Op Fl s Ar selector | Fl Fl selector Ns = Ns Ar selector
.Xc
Show extended details about the lights
.It Xo Ic toggle
.Op Fl d | Fl Fl duration
.Op Fl s Ar selector | Fl Fl selector Ns = Ns Ar selector
.Op Fl Fl simple | Fl Fl table
.Xc
Toggle the power
.It Xo Ic validate
.Ar color_string
.Xc
Validate a color string
.It Xo Ic version
.Xc
Print the version
.El
.Sh OPTIONS
.Bl -tag -width Ds
.It Fl b , Fl Fl brightness Ns = Ns Ar brightness
The brightness level from 0.0 to 1.0. Overrides any brightness set in color (if any)
.It Fl c , Fl Fl color Ns = Ns Ar color
This color to use for the action.
.Pp
When used with the
.Nm breathe
effect, this
is the "to" color.
.It Fl Fl cycles Ns = Ns Ar cycles
The number of times to repeat the effect. Defaults to 1.0 cycle.
.It Fl d , Fl Fl duration Ns = Ns Ar duration
The time in seconds to spend performing the action. Range: 0.0 3155760000.0 (100 years).
.It Fl f , Fl Fl fast
Execute the action fast without any state checks or waiting for the result from the HTTP API
.It Fl Fl from-color Ns = Ns Ar color
The color to start the
.Nm breathe
effect from. If this parameter is omitted
then the color the bulb is currently set to is used instead.
.It Fl H , Fl Fl hue Ns = Ns Ar hue
Sets the hue. Range 0 - 360.
.It Fl i , Fl Fl infrared Ns = Ns Ar infrared
Sets the maximum brightness of the infrared channel from 0.0 to 1.0.
.It Fl k , Fl Fl kelvin Ns = Ns Ar kelvin
Set the kelvin value. The saturation is automatically set to 0.
.It Fl n , Fl Fl name Ns = Ns Ar name
Set the color using a named color from
.Pa lumerc
.It Fl Fl peak Ns = Ns Ar peak
Defines where in a period the target color is at its maximum. Minimum 0.0,
maximum 1.0. Defaults to 0.5.
.It Fl Fl period Ns = Ns Ar period
The time in seconds for one cycle of the
.Nm breathe
effect. Defaults to 1.0
second.
.It Fl Fl persist
If false set the light back to its previous value when effect ends, if true
leave the last effect color.
.It Fl p , Fl Fl power Ns = Ns Ar [ on | off ]
Set the power state
.It Fl Fl power-on
If true, turn the bulb on if it is not already on.
.It Fl r , Fl Fl rgb Ns = Ns Ar R,G,B
Set the color via a comma delimited R,G,B string. Values range from 0 - 255.
.It Fl S , Fl Fl saturation Ns = Ns Ar saturation
Set the saturation. Range 0.0 - 1.0.
.It Fl s , Fl Fl selector Ns = Ns Ar selector
The selector is used to group lights together belonging in the same account
.Pp
Selectors can be in the following format: all, label:[value], id:[value], group_id:[value], group:[value], location_id:[value], location:[value], scene_id:[value]
.Pp
The default selector is "all"
.Sh FILES
.Bl -tag -width "~/.config/lume/lume.conf" -compact
.It Pa ~/.lumerc
Default
.Nm
configuration file
.It Pa ~/.config/lume/lume.conf
XDG config home
.Nm
configuration file
.Sh EXAMPLES
Sample
.Pa lumerc
file:
.Bd -literal -offset indent
access_token = "token"
# indicator = "●"
# output_format = "table"
[colors]
purple_candy = [ 280.0, 0.29, 0.71 ]
wasabi = [ 120.0, 1.0, 0.7 ]
honeydew = [ 120.0, 1.0, 0.97 ]
green_mist = [ 92.0, 0.72, 0.75 ]
pea = [ 90.0, 0.42, 0.47 ]
cat_eye = [ 76.0, 0.74, 0.61 ]
seagreen = [ 160.0, 1.0, 0.50 ]
blue_mist = [ 202.0, 0.97, 0.75 ]
.Ed

View File

@ -1,4 +1,4 @@
lume (__VERSION__) UNRELEASED; urgency=medium lume (__VERSION__-__REVISION__) unstable; urgency=medium
* Package generated with make deb * Package generated with make deb

View File

@ -1 +0,0 @@
9

View File

@ -1,7 +1,14 @@
Source: lume Source: lume
Section: unknown
Priority: optional
Maintainer: Ryan Cavicchioni <ryan@cavi.cc> Maintainer: Ryan Cavicchioni <ryan@cavi.cc>
Build-Depends: debhelper (>= 10)
Standards-Version: 4.1.2
Homepage: https://git.kill0.net/chill9/lume
Vcs-Git: https://git.kill0.net/chill9/lume.git
Vcs-Browser: https://git.kill0.net/chill9/lume.git
Package: lume Package: lume
Architecture: any Architecture: any
Depends: ${misc:Depends}, ${shlibs:Depends} Depends: ${shlibs:Depends}, ${misc:Depends}
Description: A CLI tool for the LIFX HTTP API Description: A CLI tool for the LIFX HTTP API

View File

@ -0,0 +1,28 @@
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: lume
Source: https://git.kill0.net/chill9/lume
Files: *
Copyright: 2021 Ryan Cavicchioni <ryan@cavi.cc>
License: MPL-2.0
License: MPL-2.0
Licensed under the Mozilla Public License License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may
obtain a copy of the License at
.
https://www.mozilla.org/en-US/MPL/2.0/
.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
.
On Debian systems, the complete text of the Mozilla Public License version 2.0
license can be found in "/usr/share/common-licenses/MPL-2.0".
# Please also look if there are files or directories which have a
# different copyright/license attached and list them here.
# Please avoid picking licenses with terms that are more restrictive than the
# packaged work, as it may make Debian's contributions unacceptable upstream.

View File

@ -0,0 +1 @@
lume.1

View File

@ -2,7 +2,8 @@
DISTRIBUTION = $(shell lsb_release -sr) DISTRIBUTION = $(shell lsb_release -sr)
VERSION = __VERSION__ VERSION = __VERSION__
PACKAGEVERSION = $(VERSION) REVISION = __REVISION__
PACKAGEVERSION = $(VERSION)-$(REVISION)
%: %:
dh $@ dh $@

View File

@ -1,6 +1,6 @@
Name: lume Name: lume
Version: __VERSION__ Version: __VERSION__
Release: 1%{?dist} Release: __REVISION__%{?dist}
Summary: A CLI tool for the LIFX HTTP API Summary: A CLI tool for the LIFX HTTP API
License: MPL License: MPL
@ -25,6 +25,7 @@ Source: %{name}-%{version}.tar.xz
%{_bindir}/lume %{_bindir}/lume
%license LICENSE %license LICENSE
/usr/share/lume/lumerc /usr/share/lume/lumerc
%doc %{_mandir}/man1/lume.1.*
%changelog %changelog