diff --git a/campaign/campaign.go b/campaign/campaign.go new file mode 100644 index 0000000..7f5a27a --- /dev/null +++ b/campaign/campaign.go @@ -0,0 +1,21 @@ +package campaign + +import ( + "encoding/json" + "fmt" + "io/ioutil" + + "git.ctrlz.es/mgdelacroix/campaigner/model" +) + +func Save(campaign *model.Campaign) error { + marshaledCampaign, err := json.MarshalIndent(campaign, "", " ") + if err != nil { + return err + } + + if err := ioutil.WriteFile("./campaign.json", marshaledCampaign, 0600); err != nil { + return fmt.Errorf("cannot save campaign: %w", err) + } + return nil +} diff --git a/cmd/init.go b/cmd/init.go index a2eebf1..5069c6c 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -1,21 +1,29 @@ package cmd import ( + "git.ctrlz.es/mgdelacroix/campaigner/campaign" + "git.ctrlz.es/mgdelacroix/campaigner/model" + "github.com/spf13/cobra" ) func InitCmd() *cobra.Command { - return &cobra.Command{ + cmd := &cobra.Command{ Use: "init", Short: "Creates a new campaign in the current directory", Args: cobra.NoArgs, - RunE: initCmdF, + Run: initCmdF, } - // add mandatory flags for epic, tags, etc + cmd.Flags().StringP("epic", "e", "", "the epic id to associate this campaign with") + cmd.MarkFlagRequired("epic") + + return cmd } -func initCmdF(_ *cobra.Command, _ []string) error { - // creates the campaign.json file - return nil +func initCmdF(cmd *cobra.Command, _ []string) { + epic, _ := cmd.Flags().GetString("epic") + if err := campaign.Save(&model.Campaign{Epic: epic}); err != nil { + ErrorAndExit(cmd, err) + } } diff --git a/cmd/token.go b/cmd/token.go index 2bfff0d..6d511d2 100644 --- a/cmd/token.go +++ b/cmd/token.go @@ -8,10 +8,10 @@ import ( func TokenSetJiraCmd() *cobra.Command { return &cobra.Command{ - Use: "jira USERNAME TOKEN", - Short: "Sets the value of the jira token", - Args: cobra.ExactArgs(2), - RunE: tokenSetJiraCmdF, + Use: "jira USERNAME TOKEN", + Short: "Sets the value of the jira token", + Args: cobra.ExactArgs(2), + RunE: tokenSetJiraCmdF, } } diff --git a/model/campaign.go b/model/campaign.go index 369f4dd..b0c2267 100644 --- a/model/campaign.go +++ b/model/campaign.go @@ -2,5 +2,5 @@ package model type Campaign struct { Epic string `json:"epic"` - Tickets []*Ticket `json:"tickets"` + Tickets []*Ticket `json:"tickets,omitempty"` }