Skip to content

Commit

Permalink
Improve config flag handling
Browse files Browse the repository at this point in the history
  • Loading branch information
janos committed Feb 1, 2020
1 parent 5c714d9 commit 56ba309
Show file tree
Hide file tree
Showing 20 changed files with 169 additions and 114 deletions.
11 changes: 6 additions & 5 deletions newreleases/cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ func (c *command) initAuthCmd() (err error) {

return nil
},
PreRunE: c.setAuthService,
}

if err := addClientFlags(cmd, c.config); err != nil {
return err
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := addClientFlags(cmd, c.config); err != nil {
return err
}
return c.setAuthService(cmd, args)
},
}

c.root.AddCommand(cmd)
Expand Down
16 changes: 11 additions & 5 deletions newreleases/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package cmd
import (
"errors"
"io"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -60,6 +61,10 @@ func newCommand(opts ...option) (c *command, err error) {
More information at https://newreleases.io.`,
SilenceErrors: true,
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
cmdName := cmd.Name()
return c.initConfig(cmdName != cmdNameConfigure && cmdName != cmdNameGetAuthKey)
},
},
}

Expand All @@ -71,9 +76,6 @@ More information at https://newreleases.io.`,
}

c.initGlobalFlags()
if err := c.initConfig(); err != nil {
return nil, err
}

if err := c.initProjectCmd(); err != nil {
return nil, err
Expand Down Expand Up @@ -133,7 +135,7 @@ func (c *command) initGlobalFlags() {
globalFlags.StringVar(&c.cfgFile, "config", "", "config file (default is $HOME/.newreleases.yaml)")
}

func (c *command) initConfig() (err error) {
func (c *command) initConfig(requireConfigFileIfSet bool) (err error) {
config := viper.New()
configName := ".newreleases"
if c.cfgFile != "" {
Expand All @@ -147,6 +149,7 @@ func (c *command) initConfig() (err error) {
// Search config in home directory with name ".newreleases" (without extension).
config.AddConfigPath(c.homeDir)
config.SetConfigName(configName)
requireConfigFileIfSet = false
}

// Environment
Expand All @@ -160,8 +163,11 @@ func (c *command) initConfig() (err error) {

// If a config file is found, read it in.
if err := config.ReadInConfig(); err != nil {
if requireConfigFileIfSet {
return err
}
var e viper.ConfigFileNotFoundError
if !errors.As(err, &e) {
if !errors.As(err, &e) && !os.IsNotExist(err) {
return err
}
}
Expand Down
4 changes: 3 additions & 1 deletion newreleases/cmd/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import (
"github.com/spf13/cobra"
)

const cmdNameConfigure = "configure"

func (c *command) initConfigureCmd() {
c.root.AddCommand(&cobra.Command{
Use: "configure",
Use: cmdNameConfigure,
Short: "Provide configuration values to be stored in a file",
Long: configurationHelp,
RunE: func(cmd *cobra.Command, args []string) (err error) {
Expand Down
33 changes: 23 additions & 10 deletions newreleases/cmd/configure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestConfigureCmd(t *testing.T) {
for _, tc := range []struct {
name string
withConfigFlag bool
newConfig bool
authKey string
wantOutputFunc func(filename string) string
wantErrorOutput string
Expand All @@ -43,16 +44,26 @@ func TestConfigureCmd(t *testing.T) {
wantOutputFunc: func(filename string) string {
return fmt.Sprintf("Auth Key: Configuration saved to: %s.\n", filename)
},
wantData: "auth-key: z8jwn5ne0sg5a9b4qOpc9ty6an16rpymcw71\ntimeout: 30s\n",
wantData: "auth-key: z8jwn5ne0sg5a9b4qOpc9ty6an16rpymcw71\n",
},
{
name: "valid key with config flag",
name: "valid key with new config flag",
withConfigFlag: true,
newConfig: true,
authKey: "9ty6an1z8jwn5ne0sg5a9b4qOpc6rpymcw71",
wantOutputFunc: func(filename string) string {
return fmt.Sprintf("Auth Key: Configuration saved to: %s.\n", filename)
},
wantData: "auth-key: 9ty6an1z8jwn5ne0sg5a9b4qOpc6rpymcw71\ntimeout: 30s\n",
wantData: "auth-key: 9ty6an1z8jwn5ne0sg5a9b4qOpc6rpymcw71\n",
},
{
name: "valid key with existing config flag",
withConfigFlag: true,
authKey: "9ty6an1z8jwn5ne0sg5a9b4qOpc6rpymcw71",
wantOutputFunc: func(filename string) string {
return fmt.Sprintf("Auth Key: Configuration saved to: %s.\n", filename)
},
wantData: "auth-key: 9ty6an1z8jwn5ne0sg5a9b4qOpc6rpymcw71\n",
},
} {
t.Run(tc.name, func(t *testing.T) {
Expand All @@ -63,12 +74,14 @@ func TestConfigureCmd(t *testing.T) {
defer os.RemoveAll(dir)

cfgFile := filepath.Join(dir, ".newreleases.yaml")
f, err := os.Create(cfgFile)
if err != nil {
t.Fatal(err)
}
if err := f.Close(); err != nil {
t.Fatal(err)
if !tc.newConfig {
f, err := os.Create(cfgFile)
if err != nil {
t.Fatal(err)
}
if err := f.Close(); err != nil {
t.Fatal(err)
}
}

args := []string{"configure"}
Expand Down Expand Up @@ -166,7 +179,7 @@ func TestConfigureCmd_overwrite(t *testing.T) {
if err != nil {
t.Fatal(err)
}
wantData := fmt.Sprintf("auth-key: %s\ntimeout: 30s\n", authKey)
wantData := fmt.Sprintf("auth-key: %s\n", authKey)
if string(gotData) != wantData {
t.Errorf("got config file data %q, want %q", string(gotData), wantData)
}
Expand Down
11 changes: 6 additions & 5 deletions newreleases/cmd/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ func (c *command) initDiscordCmd() (err error) {

return nil
},
PreRunE: c.setDiscordChannelsService,
}

if err := addClientFlags(cmd, c.config); err != nil {
return err
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := addClientFlags(cmd, c.config); err != nil {
return err
}
return c.setDiscordChannelsService(cmd, args)
},
}

c.root.AddCommand(cmd)
Expand Down
15 changes: 9 additions & 6 deletions newreleases/cmd/get_auth_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ import (
"github.com/spf13/cobra"
)

const cmdNameGetAuthKey = "get-auth-key"

func (c *command) initGetAuthKeyCmd() (err error) {
getAuthKeyCmd := &cobra.Command{
Use: "get-auth-key",
Use: cmdNameGetAuthKey,
Short: "Get API auth key and store it in the configuration",
Long: configurationHelp,
RunE: func(cmd *cobra.Command, args []string) (err error) {
Expand Down Expand Up @@ -93,11 +95,12 @@ func (c *command) initGetAuthKeyCmd() (err error) {
cmd.Printf("Configuration saved to: %s.\n", c.cfgFile)
return nil
},
PreRunE: c.setAuthKeysGetter,
}

if err := addClientFlags(getAuthKeyCmd, c.config); err != nil {
return err
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := addClientFlags(cmd, c.config); err != nil {
return err
}
return c.setAuthKeysGetter(cmd, args)
},
}

c.root.AddCommand(getAuthKeyCmd)
Expand Down
28 changes: 21 additions & 7 deletions newreleases/cmd/get_auth_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestGetAuthKeyCmd(t *testing.T) {
for _, tc := range []struct {
name string
withConfigFlag bool
newConfig bool
input string
authKeysGetter cmd.AuthKeysGetter
wantOutputFunc func(filename string) string
Expand Down Expand Up @@ -58,7 +59,18 @@ func TestGetAuthKeyCmd(t *testing.T) {
wantData: "auth-key: z8jwn5ne0sg5a9b4qOpc9ty6an16rpymcw71\ntimeout: 30s\n",
},
{
name: "single key with config flag",
name: "single key with new config flag",
withConfigFlag: true,
newConfig: true,
input: "me@newreleases.io\n",
authKeysGetter: newMockAuthKeysGetter("me@newreleases.io", "myPassword", []newreleases.AuthKey{{Name: "Master", Secret: "z8jwn5ne0sg5a9b4qOpc9ty6an16rpymcw71"}}, nil),
wantOutputFunc: func(filename string) string {
return fmt.Sprintf("Sign in to NewReleases with your credentials\nto get available API keys and store them in local configuration file.\nEmail: Password: \nUsing auth key: Master.\nConfiguration saved to: %s.\n", filename)
},
wantData: "auth-key: z8jwn5ne0sg5a9b4qOpc9ty6an16rpymcw71\ntimeout: 30s\n",
},
{
name: "single key with existing config flag",
withConfigFlag: true,
input: "me@newreleases.io\n",
authKeysGetter: newMockAuthKeysGetter("me@newreleases.io", "myPassword", []newreleases.AuthKey{{Name: "Master", Secret: "z8jwn5ne0sg5a9b4qOpc9ty6an16rpymcw71"}}, nil),
Expand Down Expand Up @@ -138,12 +150,14 @@ func TestGetAuthKeyCmd(t *testing.T) {
defer os.RemoveAll(dir)

cfgFile := filepath.Join(dir, ".newreleases.yaml")
f, err := os.Create(cfgFile)
if err != nil {
t.Fatal(err)
}
if err := f.Close(); err != nil {
t.Fatal(err)
if !tc.newConfig {
f, err := os.Create(cfgFile)
if err != nil {
t.Fatal(err)
}
if err := f.Close(); err != nil {
t.Fatal(err)
}
}

args := []string{"get-auth-key"}
Expand Down
11 changes: 6 additions & 5 deletions newreleases/cmd/hangouts_chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ func (c *command) initHangoutsChatCmd() (err error) {

return nil
},
PreRunE: c.setHangoutsChatWebhooksService,
}

if err := addClientFlags(cmd, c.config); err != nil {
return err
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := addClientFlags(cmd, c.config); err != nil {
return err
}
return c.setHangoutsChatWebhooksService(cmd, args)
},
}

c.root.AddCommand(cmd)
Expand Down
11 changes: 6 additions & 5 deletions newreleases/cmd/microsoft_teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ func (c *command) initMicrosoftTeamsCmd() (err error) {

return nil
},
PreRunE: c.setMicrosoftTeamsWebhooksService,
}

if err := addClientFlags(cmd, c.config); err != nil {
return err
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := addClientFlags(cmd, c.config); err != nil {
return err
}
return c.setMicrosoftTeamsWebhooksService(cmd, args)
},
}

c.root.AddCommand(cmd)
Expand Down
11 changes: 6 additions & 5 deletions newreleases/cmd/project_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ func (c *command) initProjectAddCmd(projectCmd *cobra.Command) (err error) {
printProject(cmd, project)
return nil
},
PreRunE: c.setProjectsService,
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := addClientFlags(cmd, c.config); err != nil {
return err
}
return c.setProjectsService(cmd, args)
},
}

cmd.Flags().String(optionNameEmail, "none", "frequency of email notifications: hourly, daily, weekly, none")
Expand All @@ -124,10 +129,6 @@ func (c *command) initProjectAddCmd(projectCmd *cobra.Command) (err error) {
cmd.Flags().Bool(optionNameExcludePrereleases, false, "exclude pre-repelases")
cmd.Flags().Bool(optionNameExcludeUpdated, false, "exclude updated")

if err := addClientFlags(cmd, c.config); err != nil {
return err
}

projectCmd.AddCommand(cmd)
return nil
}
11 changes: 6 additions & 5 deletions newreleases/cmd/project_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ func (c *command) initProjectGetCmd(projectCmd *cobra.Command) (err error) {
printProject(cmd, project)
return nil
},
PreRunE: c.setProjectsService,
}

if err := addClientFlags(cmd, c.config); err != nil {
return err
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := addClientFlags(cmd, c.config); err != nil {
return err
}
return c.setProjectsService(cmd, args)
},
}

projectCmd.AddCommand(cmd)
Expand Down
11 changes: 6 additions & 5 deletions newreleases/cmd/project_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,18 @@ func (c *command) initProjectListCmd(projectCmd *cobra.Command) (err error) {

return nil
},
PreRunE: c.setProjectsService,
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := addClientFlags(cmd, c.config); err != nil {
return err
}
return c.setProjectsService(cmd, args)
},
}

cmd.Flags().IntP(optionNamePage, "p", 1, "page number")
cmd.Flags().String(optionNameProvider, "", "filter by provider")
cmd.Flags().String(optionNameOrder, "", "sort projects: updated, added, name; default updated")

if err := addClientFlags(cmd, c.config); err != nil {
return err
}

projectCmd.AddCommand(cmd)
return nil
}
11 changes: 6 additions & 5 deletions newreleases/cmd/project_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ func (c *command) initProjectRemoveCmd(projectCmd *cobra.Command) (err error) {

return err
},
PreRunE: c.setProjectsService,
}

if err := addClientFlags(cmd, c.config); err != nil {
return err
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := addClientFlags(cmd, c.config); err != nil {
return err
}
return c.setProjectsService(cmd, args)
},
}

projectCmd.AddCommand(cmd)
Expand Down
11 changes: 6 additions & 5 deletions newreleases/cmd/project_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,16 @@ func (c *command) initProjectSearchCmd(projectCmd *cobra.Command) (err error) {
printProjectsTable(cmd, projects)
return nil
},
PreRunE: c.setProjectsService,
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := addClientFlags(cmd, c.config); err != nil {
return err
}
return c.setProjectsService(cmd, args)
},
}

cmd.Flags().String(optionNameProvider, "", "filter by provider")

if err := addClientFlags(cmd, c.config); err != nil {
return err
}

projectCmd.AddCommand(cmd)
return nil
}
Loading

0 comments on commit 56ba309

Please sign in to comment.