campaigner/app/app.go

79 lines
1.5 KiB
Go
Raw Permalink Normal View History

2020-04-29 18:52:15 +01:00
package app
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
2020-09-24 10:13:52 +01:00
"github.com/google/go-github/v32/github"
2020-04-29 18:52:15 +01:00
jira "gopkg.in/andygrunwald/go-jira.v1"
"git.ctrlz.es/mgdelacroix/campaigner/model"
)
type App struct {
Path string
2020-04-29 21:54:49 +01:00
JiraClient *jira.Client
GithubClient *github.Client
2020-04-29 18:52:15 +01:00
Campaign *model.Campaign
}
func SaveCampaign(campaign *model.Campaign, path string) error {
marshaledCampaign, err := json.MarshalIndent(campaign, "", " ")
if err != nil {
return err
}
if err := ioutil.WriteFile(path, marshaledCampaign, 0600); err != nil {
return fmt.Errorf("cannot save campaign: %w", err)
}
return nil
}
func (a *App) Save() error {
return SaveCampaign(a.Campaign, a.Path)
}
func (a *App) Load() error {
if _, err := os.Stat("."); err != nil {
return fmt.Errorf("cannot read campaign: %w", err)
}
fileContents, err := ioutil.ReadFile(a.Path)
if err != nil {
return 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 fmt.Errorf("there was a problem parsing the campaign file: %w", err)
}
a.Campaign = &campaign
return nil
}
func (a *App) InitClients() error {
if err := a.InitGithubClient(); err != nil {
return err
}
if err := a.InitJiraClient(); err != nil {
return err
}
return nil
}
func NewApp(path string) (*App, error) {
app := &App{Path: path}
if err := app.Load(); err != nil {
return nil, err
}
2020-04-29 19:47:13 +01:00
if err := app.InitClients(); err != nil {
return nil, err
}
2020-04-29 18:52:15 +01:00
return app, nil
}