From 167dc7aae83cc89d83139a23c56adf4a507e1719 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Sat, 29 Feb 2020 00:34:17 +0100 Subject: [PATCH] Add set jira and set github token commands --- cmd/token.go | 32 ++++++++++++++++++++++++++++++++ cmd/utils.go | 12 ++++++++++++ config/config.go | 2 +- 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 cmd/utils.go diff --git a/cmd/token.go b/cmd/token.go index 230d4a0..610d095 100644 --- a/cmd/token.go +++ b/cmd/token.go @@ -1,6 +1,8 @@ package cmd import ( + "git.ctrlz.es/mgdelacroix/campaigner/config" + "github.com/spf13/cobra" ) @@ -8,6 +10,8 @@ func TokenSetJiraCmd() *cobra.Command { return &cobra.Command{ Use: "jira", Short: "Sets the value of the jira token", + Args: cobra.ExactArgs(1), + RunE: tokenSetJiraCmdF, } } @@ -15,6 +19,8 @@ func TokenSetGithubCmd() *cobra.Command { return &cobra.Command{ Use: "github", Short: "Sets the value of the github token", + Args: cobra.ExactArgs(1), + RunE: tokenSetGithubCmdF, } } @@ -44,3 +50,29 @@ func TokenCmd() *cobra.Command { return cmd } + +func tokenSetJiraCmdF(cmd *cobra.Command, args []string) error { + cfg, err := config.ReadConfig() + if err != nil { + ErrorAndExit(cmd, err) + } + + cfg.JiraToken = args[0] + if err := config.SaveConfig(cfg); err != nil { + ErrorAndExit(cmd, err) + } + return nil +} + +func tokenSetGithubCmdF(cmd *cobra.Command, args []string) error { + cfg, err := config.ReadConfig() + if err != nil { + ErrorAndExit(cmd, err) + } + + cfg.GithubToken = args[0] + if err := config.SaveConfig(cfg); err != nil { + ErrorAndExit(cmd, err) + } + return nil +} diff --git a/cmd/utils.go b/cmd/utils.go new file mode 100644 index 0000000..40ca1ca --- /dev/null +++ b/cmd/utils.go @@ -0,0 +1,12 @@ +package cmd + +import ( + "os" + + "github.com/spf13/cobra" +) + +func ErrorAndExit(cmd *cobra.Command, err error) { + cmd.PrintErrln(err) + os.Exit(1) +} diff --git a/config/config.go b/config/config.go index d640007..2cc7285 100644 --- a/config/config.go +++ b/config/config.go @@ -28,7 +28,7 @@ func ReadConfig() (*Config, error) { } if _, err := os.Stat(configPath); err != nil { - return nil, fmt.Errorf("cannot read campaigner config file: %w", err) + return &Config{}, nil } fileContents, err := ioutil.ReadFile(configPath)