Add command now stores the tickets

This commit is contained in:
Miguel de la Cruz 2020-02-29 13:40:39 +01:00
parent 412d82269b
commit 44ab808223
3 changed files with 32 additions and 3 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
campaigner campaigner
campaign.json

View file

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os"
"git.ctrlz.es/mgdelacroix/campaigner/model" "git.ctrlz.es/mgdelacroix/campaigner/model"
) )
@ -19,3 +20,21 @@ func Save(campaign *model.Campaign) error {
} }
return nil 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
}

View file

@ -8,6 +8,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"git.ctrlz.es/mgdelacroix/campaigner/campaign"
"git.ctrlz.es/mgdelacroix/campaigner/model" "git.ctrlz.es/mgdelacroix/campaigner/model"
) )
@ -95,7 +96,15 @@ func addCmdF(cmd *cobra.Command, _ []string) {
ErrorAndExit(cmd, err) ErrorAndExit(cmd, err)
} }
for _, ticket := range tickets { cmp, err := campaign.Read()
fmt.Printf("%+v\n", ticket) 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)
} }
} }