Skip to content

Commit

Permalink
v1.2 final
Browse files Browse the repository at this point in the history
  • Loading branch information
Lord of Scripts committed Jul 13, 2024
1 parent 5046c9b commit 581cc6c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 28 deletions.
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
45 changes: 27 additions & 18 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand All @@ -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
Expand Down Expand Up @@ -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)) {
Expand Down
17 changes: 8 additions & 9 deletions govee_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package govee
import (
"encoding/json"
"fmt"
"log"
"os"
"path"
"runtime"
Expand Down Expand Up @@ -68,13 +69,15 @@ 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()

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
Expand Down Expand Up @@ -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":
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 581cc6c

Please sign in to comment.