From f0b8828af9ebcd5f9f7dc24942465bc19184d9d3 Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Wed, 20 Jan 2021 23:09:37 -0600 Subject: [PATCH] Add method to validate configuration --- cmd/command.go | 10 ++++++++++ cmd/main.go | 5 ++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/cmd/command.go b/cmd/command.go index aa66d24..ab0e0fa 100644 --- a/cmd/command.go +++ b/cmd/command.go @@ -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 +} diff --git a/cmd/main.go b/cmd/main.go index 6031846..c0c60f8 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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()