Compare commits

..

2 Commits

4 changed files with 22 additions and 10 deletions

View File

@ -1,6 +1,7 @@
package lumecmd package lumecmd
import ( import (
"errors"
"flag" "flag"
"fmt" "fmt"
"strconv" "strconv"
@ -92,3 +93,12 @@ func GetCommand(name string) (Command, bool) {
cmd, ok := commandRegistry[name] cmd, ok := commandRegistry[name]
return cmd, ok 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 package main
import ( import (
"fmt"
"os" "os"
lumecmd "git.kill0.net/chill9/lume/cmd" lumecmd "git.kill0.net/chill9/lume/cmd"
) )
func main() { func main() {
exitCode, err := lumecmd.Main(os.Args) lumecmd.ExitWithCode(lumecmd.Main(os.Args))
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
}
os.Exit(exitCode)
} }

View File

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

View File

@ -2,6 +2,7 @@ package lumecmd
import ( import (
"fmt" "fmt"
"os"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
@ -151,3 +152,10 @@ func sortResults(res []lifx.Result) {
return res[i].Label < res[j].Label 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)
}