ficsit-cli-flake/cli/context.go
Vilsol 2672b17f44
feat: sftp (#51)
* feat: sftp
test: add tests for ftp and sftp

* chore: ci fixes

* chore: potential race fix

* fix: simplify existence checks

* fix: split path differently for ftp

* fix: 🤷

* chore: add debug print

* chore: lint

* chore: idk dude

* chore: ?

* chore: more logs

* chore: wipe mods before tests

* chore: logs

* chore: wat

* chore: wait?

* chore: no errors

* chore: gh actions are 💩

* fix: always sync after copy

* chore: remove some test logs

* chore: remove test progress watcher

* refactor: change progress to writer

* chore: logs

* chore: different logs

* chore: whoops

* chore: moar logs

* chore: even moar logs

* chore: what is life

* chore: why are we here

* chore: we are just bags of water floating through space

* chore: are you real?

* chore: ?

* chore: if you get a single update now I call bs

* chore: ok what if we just do one?

* chore: ok what if we do two?

* chore: this should not work

* chore: wait no, this one

* chore: fml

* chore: remove logs

* chore: what if we just wait a little

* chore: retry

* chore: move error

* chore: verbose log

* chore: remove explicit sleep

* chore: remove debug

* fix: linux pathing on windows

* fix: clean paths properly

* fix: fuck ftp

* fix: send update on vanilla

* feat: parallel ftp

* fix: remove potential credential leak
2023-12-28 02:13:09 +02:00

124 lines
2.8 KiB
Go

package cli
import (
"fmt"
"log/slog"
"github.com/Khan/genqlient/graphql"
"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.Provider
}
var globalContext *GlobalContext
func InitCLI(apiOnly bool) (*GlobalContext, error) {
if globalContext != nil {
return globalContext, nil
}
apiClient := ficsit.InitAPI()
mixedProvider := provider.InitMixedProvider(provider.NewFicsitProvider(apiClient), provider.NewLocalProvider())
if viper.GetBool("offline") {
mixedProvider.Offline = true
}
if !apiOnly {
profiles, err := InitProfiles()
if err != nil {
return nil, fmt.Errorf("failed to initialize profiles: %w", err)
}
installations, err := InitInstallations()
if err != nil {
return nil, fmt.Errorf("failed to initialize installations: %w", err)
}
_, err = cache.LoadCache()
if err != nil {
return nil, fmt.Errorf("failed to load cache: %w", err)
}
globalContext = &GlobalContext{
Installations: installations,
Profiles: profiles,
APIClient: apiClient,
Provider: mixedProvider,
}
} else {
globalContext = &GlobalContext{
APIClient: apiClient,
Provider: mixedProvider,
}
}
return globalContext, nil
}
// 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()
}
func (g *GlobalContext) Save() error {
if err := g.Installations.Save(); err != nil {
return fmt.Errorf("failed to save installations: %w", err)
}
if err := g.Profiles.Save(); err != nil {
return fmt.Errorf("failed to save profiles: %w", err)
}
return nil
}