Compare commits

...

2 Commits

4 changed files with 22 additions and 10 deletions

View File

@ -1,6 +1,7 @@
package lumecmd
import (
"errors"
"flag"
"fmt"
"strconv"
@ -92,3 +93,12 @@ func GetCommand(name string) (Command, bool) {
cmd, ok := commandRegistry[name]
return cmd, ok
}
// Validate configuration struct
func (c *Config) Validate() error {
var err error
if c.AccessToken == "" {
err = errors.New("access_token is not set")
}
return err
}

View File

@ -1,16 +1,11 @@
package main
import (
"fmt"
"os"
lumecmd "git.kill0.net/chill9/lume/cmd"
)
func main() {
exitCode, err := lumecmd.Main(os.Args)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
}
os.Exit(exitCode)
lumecmd.ExitWithCode(lumecmd.Main(os.Args))
}

View File

@ -33,9 +33,8 @@ func Main(args []string) (int, error) {
config.AccessToken = envAccessToken
}
if config.AccessToken == "" {
err = errors.New("fatal: access token is not set")
return ExitError, err
if err = config.Validate(); err != nil {
return ExitError, fmt.Errorf("fatal: %s", err)
}
flag.Parse()

View File

@ -2,6 +2,7 @@ package lumecmd
import (
"fmt"
"os"
"sort"
"strconv"
"strings"
@ -151,3 +152,10 @@ func sortResults(res []lifx.Result) {
return res[i].Label < res[j].Label
})
}
func ExitWithCode(code int, err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
}
os.Exit(code)
}