ficsit-cli-flake/cli/context.go

131 lines
3 KiB
Go
Raw Permalink Normal View History

2021-12-02 04:00:33 +00:00
package cli
2022-04-14 01:27:39 +00:00
import (
"fmt"
"log/slog"
2022-04-14 01:27:39 +00:00
"github.com/Khan/genqlient/graphql"
"github.com/spf13/viper"
2022-10-14 16:11:16 +00:00
"github.com/satisfactorymodding/ficsit-cli/cli/cache"
"github.com/satisfactorymodding/ficsit-cli/cli/localregistry"
"github.com/satisfactorymodding/ficsit-cli/cli/provider"
2022-04-14 01:27:39 +00:00
"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
Provider provider.Provider
2021-12-02 04:00:33 +00:00
}
var globalContext *GlobalContext
2022-10-14 16:11:16 +00:00
func InitCLI(apiOnly bool) (*GlobalContext, error) {
2021-12-02 04:00:33 +00:00
if globalContext != nil {
return globalContext, nil
}
apiClient := ficsit.InitAPI()
mixedProvider := provider.InitMixedProvider(provider.NewFicsitProvider(apiClient), provider.NewLocalProvider())
if viper.GetBool("offline") {
mixedProvider.Offline = true
}
2022-10-14 16:11:16 +00:00
if !apiOnly {
profiles, err := InitProfiles()
if err != nil {
return nil, fmt.Errorf("failed to initialize profiles: %w", err)
2022-10-14 16:11:16 +00:00
}
installations, err := InitInstallations()
if err != nil {
return nil, fmt.Errorf("failed to initialize installations: %w", err)
2022-10-14 16:11:16 +00:00
}
_, err = cache.LoadCacheMods()
if err != nil {
return nil, fmt.Errorf("failed to load cache: %w", err)
}
err = localregistry.Init()
if err != nil {
return nil, fmt.Errorf("failed to initialize local registry: %w", err)
}
2022-10-14 16:11:16 +00:00
globalContext = &GlobalContext{
Installations: installations,
Profiles: profiles,
APIClient: apiClient,
Provider: mixedProvider,
2022-10-14 16:11:16 +00:00
}
} else {
globalContext = &GlobalContext{
APIClient: apiClient,
Provider: mixedProvider,
2022-10-14 16:11:16 +00:00
}
2021-12-02 04:00:33 +00:00
}
2022-10-14 16:11:16 +00:00
return globalContext, nil
2021-12-02 04:00:33 +00:00
}
// ReInit will initialize the context
//
// Used only by tests
func (g *GlobalContext) ReInit() error {
profiles, err := InitProfiles()
if err != nil {
return fmt.Errorf("failed to initialize profiles: %w", err)
}
installations, err := InitInstallations()
if err != nil {
return fmt.Errorf("failed to initialize installations: %w", err)
}
g.Installations = installations
g.Profiles = profiles
return g.Save()
}
// Wipe will remove any trace of ficsit anywhere
func (g *GlobalContext) Wipe() error {
slog.Info("wiping global context")
// Wipe all installations
for _, installation := range g.Installations.Installations {
if err := installation.Wipe(); err != nil {
return fmt.Errorf("failed wiping installation: %w", err)
}
if err := g.Installations.DeleteInstallation(installation.Path); err != nil {
return fmt.Errorf("failed deleting installation: %w", err)
}
}
// Wipe all profiles
for _, profile := range g.Profiles.Profiles {
if err := g.Profiles.DeleteProfile(profile.Name); err != nil {
return fmt.Errorf("failed deleting profile: %w", err)
}
}
return g.Save()
}
2021-12-02 04:00:33 +00:00
func (g *GlobalContext) Save() error {
if err := g.Installations.Save(); err != nil {
return fmt.Errorf("failed to save installations: %w", err)
2021-12-02 04:00:33 +00:00
}
if err := g.Profiles.Save(); err != nil {
return fmt.Errorf("failed to save profiles: %w", err)
2021-12-02 04:00:33 +00:00
}
return nil
}