Adds implementation for init command

This commit is contained in:
Miguel de la Cruz 2020-02-29 13:33:03 +01:00
parent a289ed12ad
commit 412d82269b
4 changed files with 40 additions and 11 deletions

21
campaign/campaign.go Normal file
View file

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

View file

@ -1,21 +1,29 @@
package cmd package cmd
import ( import (
"git.ctrlz.es/mgdelacroix/campaigner/campaign"
"git.ctrlz.es/mgdelacroix/campaigner/model"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
func InitCmd() *cobra.Command { func InitCmd() *cobra.Command {
return &cobra.Command{ cmd := &cobra.Command{
Use: "init", Use: "init",
Short: "Creates a new campaign in the current directory", Short: "Creates a new campaign in the current directory",
Args: cobra.NoArgs, 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 { func initCmdF(cmd *cobra.Command, _ []string) {
// creates the campaign.json file epic, _ := cmd.Flags().GetString("epic")
return nil if err := campaign.Save(&model.Campaign{Epic: epic}); err != nil {
ErrorAndExit(cmd, err)
}
} }

View file

@ -8,10 +8,10 @@ import (
func TokenSetJiraCmd() *cobra.Command { func TokenSetJiraCmd() *cobra.Command {
return &cobra.Command{ return &cobra.Command{
Use: "jira USERNAME TOKEN", Use: "jira USERNAME TOKEN",
Short: "Sets the value of the jira token", Short: "Sets the value of the jira token",
Args: cobra.ExactArgs(2), Args: cobra.ExactArgs(2),
RunE: tokenSetJiraCmdF, RunE: tokenSetJiraCmdF,
} }
} }

View file

@ -2,5 +2,5 @@ package model
type Campaign struct { type Campaign struct {
Epic string `json:"epic"` Epic string `json:"epic"`
Tickets []*Ticket `json:"tickets"` Tickets []*Ticket `json:"tickets,omitempty"`
} }