024b11b1e8
* refactor: separate resolving tests * feat: use pubgrub to resolve dependencies * feat: show friendly mod name in error message * feat: show single version in error message when only one matches * ci: update go version to match go.mod * feat: format FactoryGame incompatibility and term * chore: fetch all necessary data of the version at once * chore: upgrade pubgrub * chore: upgrade pubgrub * ci: update golangci-lint version for go 1.21 * chore: lint * chore: update go version in readme
58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
package cli
|
|
|
|
import (
|
|
"github.com/Khan/genqlient/graphql"
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/satisfactorymodding/ficsit-cli/ficsit"
|
|
)
|
|
|
|
type GlobalContext struct {
|
|
Installations *Installations
|
|
Profiles *Profiles
|
|
APIClient graphql.Client
|
|
}
|
|
|
|
var globalContext *GlobalContext
|
|
|
|
func InitCLI(apiOnly bool) (*GlobalContext, error) {
|
|
if globalContext != nil {
|
|
return globalContext, nil
|
|
}
|
|
|
|
if !apiOnly {
|
|
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")
|
|
}
|
|
|
|
globalContext = &GlobalContext{
|
|
Installations: installations,
|
|
Profiles: profiles,
|
|
APIClient: ficsit.InitAPI(),
|
|
}
|
|
} else {
|
|
globalContext = &GlobalContext{
|
|
APIClient: ficsit.InitAPI(),
|
|
}
|
|
}
|
|
|
|
return globalContext, nil
|
|
}
|
|
|
|
func (g *GlobalContext) Save() error {
|
|
if err := g.Installations.Save(); err != nil {
|
|
return errors.Wrap(err, "failed to save installations")
|
|
}
|
|
|
|
if err := g.Profiles.Save(); err != nil {
|
|
return errors.Wrap(err, "failed to save profiles")
|
|
}
|
|
|
|
return nil
|
|
}
|