Add set jira and set github token commands

This commit is contained in:
Miguel de la Cruz 2020-02-29 00:34:17 +01:00
parent 6bd0a99a32
commit 167dc7aae8
3 changed files with 45 additions and 1 deletions

View file

@ -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
}

12
cmd/utils.go Normal file
View file

@ -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)
}

View file

@ -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)