Return error codes instead of exiting from Main()

This commit is contained in:
Ryan Cavicchioni 2021-01-16 10:36:07 -06:00
parent 14333b1342
commit 4aa0409fd1
2 changed files with 7 additions and 7 deletions

View File

@ -7,5 +7,5 @@ import (
) )
func main() { func main() {
lumecmd.Main(os.Args) os.Exit(lumecmd.Main(os.Args))
} }

View File

@ -12,19 +12,19 @@ import (
const lumercFile string = ".lumerc" const lumercFile string = ".lumerc"
func Main(args []string) { func Main(args []string) int {
var config Config var config Config
configPath := getConfigPath() configPath := getConfigPath()
if configPath == "" { if configPath == "" {
fmt.Println("fatal: ~/.lumerc was not found") fmt.Println("fatal: ~/.lumerc was not found")
os.Exit(1) return 1
} }
if _, err := toml.DecodeFile(configPath, &config); err != nil { if _, err := toml.DecodeFile(configPath, &config); err != nil {
fmt.Printf("fatal: failed to parse %s\n", configPath) fmt.Printf("fatal: failed to parse %s\n", configPath)
fmt.Println(err) fmt.Println(err)
os.Exit(1) return 1
} }
envAccessToken := os.Getenv("LIFX_ACCESS_TOKEN") envAccessToken := os.Getenv("LIFX_ACCESS_TOKEN")
@ -34,7 +34,7 @@ func Main(args []string) {
if config.AccessToken == "" { if config.AccessToken == "" {
fmt.Println("access token is not set") fmt.Println("access token is not set")
os.Exit(1) return 1
} }
flag.Parse() flag.Parse()
@ -51,7 +51,7 @@ func Main(args []string) {
cmd, ok := GetCommand(command) cmd, ok := GetCommand(command)
if !ok { if !ok {
fmt.Printf("lume: '%s' is not lume command. See 'lume help'\n", command) fmt.Printf("lume: '%s' is not lume command. See 'lume help'\n", command)
os.Exit(1) return 1
} }
fs := cmd.Flags fs := cmd.Flags
fs.Parse(args[2:]) fs.Parse(args[2:])
@ -61,7 +61,7 @@ func Main(args []string) {
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "fatal: %s\n", err) fmt.Fprintf(os.Stderr, "fatal: %s\n", err)
} }
os.Exit(exitCode) return exitCode
} }
func getConfigPath() string { func getConfigPath() string {