diff --git a/.gitignore b/.gitignore index 601d262..72d5a56 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -campaigner \ No newline at end of file +campaigner +campaign.json diff --git a/campaign/campaign.go b/campaign/campaign.go index 7f5a27a..3447825 100644 --- a/campaign/campaign.go +++ b/campaign/campaign.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "io/ioutil" + "os" "git.ctrlz.es/mgdelacroix/campaigner/model" ) @@ -19,3 +20,21 @@ func Save(campaign *model.Campaign) error { } return nil } + +func Read() (*model.Campaign, error) { + if _, err := os.Stat("."); err != nil { + return nil, fmt.Errorf("cannot read campaign: %w", err) + } + + fileContents, err := ioutil.ReadFile("./campaign.json") + if err != nil { + return nil, fmt.Errorf("there was a problem reading the campaign file: %w", err) + } + + var campaign model.Campaign + if err := json.Unmarshal(fileContents, &campaign); err != nil { + return nil, fmt.Errorf("there was a problem parsing the campaign file: %w", err) + } + + return &campaign, nil +} diff --git a/cmd/add.go b/cmd/add.go index f515e3f..8c49c13 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -8,6 +8,7 @@ import ( "github.com/spf13/cobra" + "git.ctrlz.es/mgdelacroix/campaigner/campaign" "git.ctrlz.es/mgdelacroix/campaigner/model" ) @@ -95,7 +96,15 @@ func addCmdF(cmd *cobra.Command, _ []string) { ErrorAndExit(cmd, err) } - for _, ticket := range tickets { - fmt.Printf("%+v\n", ticket) + cmp, err := campaign.Read() + if err != nil { + ErrorAndExit(cmd, err) + } + + // ToDo: make this skip duplicates + cmp.Tickets = append(cmp.Tickets, tickets...) + + if err := campaign.Save(cmp); err != nil { + ErrorAndExit(cmd, err) } }