Add init command

This commit is contained in:
Miguel de la Cruz 2020-02-29 00:49:55 +01:00
parent d7a4b25950
commit 573ea9e136
5 changed files with 39 additions and 9 deletions

21
cmd/init.go Normal file
View file

@ -0,0 +1,21 @@
package cmd
import (
"github.com/spf13/cobra"
)
func InitCmd() *cobra.Command {
return &cobra.Command{
Use: "init",
Short: "Creates a new campaign in the current directory",
Args: cobra.NoArgs,
RunE: initCmdF,
}
// add mandatory flags for epic, tags, etc
}
func initCmdF(_ *cobra.Command, _ []string) error {
// creates the campaign.json file
return nil
}

View file

@ -14,6 +14,7 @@ func RootCmd() *cobra.Command {
} }
cmd.AddCommand( cmd.AddCommand(
InitCmd(),
TokenCmd(), TokenCmd(),
) )

View file

@ -6,12 +6,9 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"os/user" "os/user"
)
type Config struct { "git.ctrlz.es/mgdelacroix/campaigner/model"
GithubToken string `json:"github_token"` )
JiraToken string `json:"jira_token"`
}
func getConfigPath() (string, error) { func getConfigPath() (string, error) {
user, err := user.Current() user, err := user.Current()
@ -21,14 +18,14 @@ func getConfigPath() (string, error) {
return user.HomeDir + "/.campaigner", nil return user.HomeDir + "/.campaigner", nil
} }
func ReadConfig() (*Config, error) { func ReadConfig() (*model.Config, error) {
configPath, err := getConfigPath() configPath, err := getConfigPath()
if err != nil { if err != nil {
return nil, err return nil, err
} }
if _, err := os.Stat(configPath); err != nil { if _, err := os.Stat(configPath); err != nil {
return &Config{}, nil return &model.Config{}, nil
} }
fileContents, err := ioutil.ReadFile(configPath) fileContents, err := ioutil.ReadFile(configPath)
@ -36,7 +33,7 @@ func ReadConfig() (*Config, error) {
return nil, fmt.Errorf("there was a problem reading the config file: %w", err) return nil, fmt.Errorf("there was a problem reading the config file: %w", err)
} }
var config Config var config model.Config
if err := json.Unmarshal(fileContents, &config); err != nil { if err := json.Unmarshal(fileContents, &config); err != nil {
return nil, fmt.Errorf("there was a problem parsing the config file: %w", err) return nil, fmt.Errorf("there was a problem parsing the config file: %w", err)
} }
@ -44,7 +41,7 @@ func ReadConfig() (*Config, error) {
return &config, nil return &config, nil
} }
func SaveConfig(config *Config) error { func SaveConfig(config *model.Config) error {
configPath, err := getConfigPath() configPath, err := getConfigPath()
if err != nil { if err != nil {
return err return err

5
model/campaign.go Normal file
View file

@ -0,0 +1,5 @@
package model
type Campaign struct {
Epic string `json:"epic"`
}

6
model/config.go Normal file
View file

@ -0,0 +1,6 @@
package model
type Config struct {
GithubToken string `json:"github_token"`
JiraToken string `json:"jira_token"`
}