ficsit-cli-flake/cli/context.go
mircearoata e4b02a792d
feat: offline mode (#14)
* chore: move mod downloading to cli/cache

* feat: data providers, ficsit and local

* feat: keep cache in memory, load on init

* feat: log invalid cache files instead of returning error

* chore: make linter happy

* feat: fill cached mod Authors field from CreatedBy

* chore: make linter happy again

* feat: add icon and size to cached mods

* feat: cache the cached file hashes

* fix: change to new provider access style

---------

Co-authored-by: Vilsol <me@vil.so>
2023-12-06 06:47:41 +02:00

77 lines
1.6 KiB
Go

package cli
import (
"github.com/Khan/genqlient/graphql"
"github.com/pkg/errors"
"github.com/spf13/viper"
"github.com/satisfactorymodding/ficsit-cli/cli/cache"
"github.com/satisfactorymodding/ficsit-cli/cli/provider"
"github.com/satisfactorymodding/ficsit-cli/ficsit"
)
type GlobalContext struct {
Installations *Installations
Profiles *Profiles
APIClient graphql.Client
Provider *provider.MixedProvider
}
var globalContext *GlobalContext
func InitCLI(apiOnly bool) (*GlobalContext, error) {
if globalContext != nil {
return globalContext, nil
}
apiClient := ficsit.InitAPI()
mixedProvider := provider.InitMixedProvider(apiClient)
if viper.GetBool("offline") {
mixedProvider.Offline = true
}
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")
}
_, err = cache.LoadCache()
if err != nil {
return nil, errors.Wrap(err, "failed to load cache")
}
globalContext = &GlobalContext{
Installations: installations,
Profiles: profiles,
APIClient: apiClient,
Provider: mixedProvider,
}
} else {
globalContext = &GlobalContext{
APIClient: apiClient,
Provider: mixedProvider,
}
}
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
}