ficsit-cli-flake/cli/context.go

54 lines
982 B
Go
Raw Normal View History

2021-12-02 04:00:33 +00:00
package cli
2022-04-14 01:27:39 +00:00
import (
"github.com/Khan/genqlient/graphql"
"github.com/pkg/errors"
"github.com/satisfactorymodding/ficsit-cli/ficsit"
)
2021-12-02 04:00:33 +00:00
type GlobalContext struct {
Installations *Installations
Profiles *Profiles
2022-04-14 01:27:39 +00:00
APIClient graphql.Client
2021-12-02 04:00:33 +00:00
}
var globalContext *GlobalContext
func InitCLI() (*GlobalContext, error) {
if globalContext != nil {
return globalContext, nil
}
profiles, err := InitProfiles()
if err != nil {
return nil, errors.Wrap(err, "failed to initialize profiles")
}
installations, err := InitInstallations()
if err != nil {
return nil, errors.Wrap(err, "failed to initialize installations")
}
ctx := &GlobalContext{
Installations: installations,
Profiles: profiles,
2022-04-14 01:27:39 +00:00
APIClient: ficsit.InitAPI(),
2021-12-02 04:00:33 +00:00
}
globalContext = ctx
return ctx, nil
}
func (g *GlobalContext) Save() error {
if err := g.Installations.Save(); err != nil {
return err
}
if err := g.Profiles.Save(); err != nil {
return err
}
return nil
}