diff --git a/CHANGELOG.md b/CHANGELOG.md index be86888..2b3db78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,27 @@ > Govee API Utility `govee` written in pure GO +## [1.2](https://github.com/lordofscripts/govee/compare/v1.2...v1.1) (2024-07-13) + +> A quick maintenance release with support for MacOS & Windows. GoveeLux has +> now gone beyond the Raspberry Pi box boundaries. + +### New Features + +* Added `-init` command-line flag. Creates sample configuration. +* It should now work on Linux, MacOS & Windows + +### Other Changes + +* README now has informational badges and Go mascot cheering Govee +* The configuration file name is now `goveelux.json` +* The resulting executable is now `goveelux` +* Coverage improvements: new tests. +* Some refactoring to improve readability and maintenance +* Formatting changes to boost Go Report Card from C to A. +* Implemented GitHub workflow for Go + + ## [1.1](https://github.com/lordofscripts/govee/compare/v1.1...v1.0) (2024-07-10) > Added basic support for light control. diff --git a/cmd/main.go b/cmd/main.go index f887395..93e1b74 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -60,6 +60,30 @@ func findByMAC(mac string) *govee.GoveeDevice { return nil } +func resolveAlias(deviceID, defAddress, defModel string) (address, model string) { + if len(deviceID) != 0 { + cfg := govee.ReadConfig() + candidates := cfg.Devices.Where(govee.FieldALIAS, deviceID) + cnt := candidates.Count() + if cnt == 0 { + die(RETVAL_CFG_ALIAS, "Could not find alias %q in repository\n", deviceID) + } + + if cnt > 1 { + die(RETVAL_CFG_ALIAS, "Found %d entries. Alias not unique, please correct config\n", cnt) + } + + fmt.Println("\tFound ", candidates[0], "\n\tAt :", candidates[0].Location) + address = candidates[0].MacAddress + model = candidates[0].Model + } else { + address = defAddress + model = defModel + } + + return address, model +} + // print a message and terminate application execution func die(retVal int, format string, v ...any) { fmt.Printf(format+"\n", v...) @@ -85,8 +109,8 @@ func main() { const ( DEF_BRIGHT uint = 101 ) - fmt.Printf("\t\t../ %s (c)2023-%d Lord of Scripts \\..\n", govee.Version, time.Now().Year()) - fmt.Println("\t\t https://allmylinks.com/lordofscripts") + fmt.Printf("\t../ %s (c)2023-%d Lord of Scripts \\..\n", govee.Version, time.Now().Year()) + fmt.Println("\t https://allmylinks.com/lordofscripts") // declare real CLI options var optHelp, optList, optOn, optOff, optState, optInit bool // declare CLI options which have an explicit value @@ -145,22 +169,7 @@ func main() { } // Config - if len(inAlias) != 0 { - cfg := govee.ReadConfig() - candidates := cfg.Devices.Where(govee.FieldALIAS, inAlias) - cnt := candidates.Count() - if cnt == 0 { - die(RETVAL_CFG_ALIAS, "Could not find alias %q in repository\n", inAlias) - } - - if cnt > 1 { - die(RETVAL_CFG_ALIAS, "Found %d entries. Alias not unique, please correct config\n", cnt) - } - - fmt.Println("\tFound ", candidates[0], "\n\tAt :", candidates[0].Location) - inDevice = candidates[0].MacAddress - inModel = candidates[0].Model - } + inDevice, inModel = resolveAlias(inAlias, inDevice, inModel) // with STATE, ON & OFF commands DEVICE & MODEL are required if (optOn || optOff || optState) && ((len(inDevice) == 0) && (len(inModel) == 0)) { diff --git a/govee_config.go b/govee_config.go index 6e67b35..732fa94 100644 --- a/govee_config.go +++ b/govee_config.go @@ -9,6 +9,7 @@ package govee import ( "encoding/json" "fmt" + "log" "os" "path" "runtime" @@ -68,6 +69,7 @@ func CreateSampleConfigFile() (bool, error) { sample.Devices = append(sample.Devices, sampleDev) if fd, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0640); err != nil { + log.Println("ERR-CFG", err.Error()) return false, err } else { defer fd.Close() @@ -75,6 +77,7 @@ func CreateSampleConfigFile() (bool, error) { jsonEncoder := json.NewEncoder(fd) jsonEncoder.SetIndent("", "\t") if err := jsonEncoder.Encode(sample); err != nil { + log.Println("ERR-CFG", err.Error()) return false, err } else { created = true @@ -158,13 +161,8 @@ func getEnvAPI() (string, bool) { // build the full path to the GoveeLux configuration file and take into // account whether it is MacOS, Linux or Windows. func getConfigFullPath() string { - const ( - HOME_ENV = "HOME" // environment var. holding user home directory - MY_CONFIG = ".config/goveelux.json" // config file relative to HOME_ENV - ) // platform-generic user profile directory 'HOME' - basename := MY_CONFIG - envVar := HOME_ENV + var basename, envVar string switch runtime.GOOS { case "windows": @@ -176,10 +174,11 @@ func getConfigFullPath() string { basename = ".goveelux.json" break case "linux": - fallthrough + envVar = "HOME" + basename = ".config/goveelux.json" + break default: - envVar = HOME_ENV - basename = MY_CONFIG + log.Fatal("ERR-CFG", "unknown OS") } return path.Join(os.Getenv(envVar), basename) diff --git a/version.go b/version.go index 730a338..c32f9d3 100644 --- a/version.go +++ b/version.go @@ -23,7 +23,7 @@ const ( ) // NOTE: Change these values accordingly -var Version string = version{NAME, "1.1.1", statusReleased, 1}.String() +var Version string = version{NAME, "1.2", statusReleased, 0}.String() /* ---------------------------------------------------------------- * T y p e s