@@ -60,6 +60,30 @@ func findByMAC(mac string) *govee.GoveeDevice {
6060 return nil
6161}
6262
63+ func resolveAlias (deviceID , defAddress , defModel string ) (address , model string ) {
64+ if len (deviceID ) != 0 {
65+ cfg := govee .ReadConfig ()
66+ candidates := cfg .Devices .Where (govee .FieldALIAS , deviceID )
67+ cnt := candidates .Count ()
68+ if cnt == 0 {
69+ die (RETVAL_CFG_ALIAS , "Could not find alias %q in repository\n " , deviceID )
70+ }
71+
72+ if cnt > 1 {
73+ die (RETVAL_CFG_ALIAS , "Found %d entries. Alias not unique, please correct config\n " , cnt )
74+ }
75+
76+ fmt .Println ("\t Found " , candidates [0 ], "\n \t At :" , candidates [0 ].Location )
77+ address = candidates [0 ].MacAddress
78+ model = candidates [0 ].Model
79+ } else {
80+ address = defAddress
81+ model = defModel
82+ }
83+
84+ return address , model
85+ }
86+
6387// print a message and terminate application execution
6488func die (retVal int , format string , v ... any ) {
6589 fmt .Printf (format + "\n " , v ... )
@@ -85,8 +109,8 @@ func main() {
85109 const (
86110 DEF_BRIGHT uint = 101
87111 )
88- fmt .Printf ("\t \t ../ %s (c)2023-%d Lord of Scripts \\ ..\n " , govee .Version , time .Now ().Year ())
89- fmt .Println ("\t \t https://allmylinks.com/lordofscripts" )
112+ fmt .Printf ("\t ../ %s (c)2023-%d Lord of Scripts \\ ..\n " , govee .Version , time .Now ().Year ())
113+ fmt .Println ("\t https://allmylinks.com/lordofscripts" )
90114 // declare real CLI options
91115 var optHelp , optList , optOn , optOff , optState , optInit bool
92116 // declare CLI options which have an explicit value
@@ -145,22 +169,7 @@ func main() {
145169 }
146170
147171 // Config
148- if len (inAlias ) != 0 {
149- cfg := govee .ReadConfig ()
150- candidates := cfg .Devices .Where (govee .FieldALIAS , inAlias )
151- cnt := candidates .Count ()
152- if cnt == 0 {
153- die (RETVAL_CFG_ALIAS , "Could not find alias %q in repository\n " , inAlias )
154- }
155-
156- if cnt > 1 {
157- die (RETVAL_CFG_ALIAS , "Found %d entries. Alias not unique, please correct config\n " , cnt )
158- }
159-
160- fmt .Println ("\t Found " , candidates [0 ], "\n \t At :" , candidates [0 ].Location )
161- inDevice = candidates [0 ].MacAddress
162- inModel = candidates [0 ].Model
163- }
172+ inDevice , inModel = resolveAlias (inAlias , inDevice , inModel )
164173
165174 // with STATE, ON & OFF commands DEVICE & MODEL are required
166175 if (optOn || optOff || optState ) && ((len (inDevice ) == 0 ) && (len (inModel ) == 0 )) {
0 commit comments