ef7b89cc03
this changes globalFlags to be a ulext.External interface value that is passed to each command. rather than have the ulext.External have a Setup call in the way that the projectProvider used to we make all of the state arguments to the functions and have the commands call setup themselves. the reason it is in its own package is so that cmd/uplinkng can import cmd/uplinkng/ultest but cmd/uplinkng/ultest needs to refer to whatever the interface type is to call the function that creates the commands. there's also quite a bit of shuffling around of code and names. sorry if that makes it tricky to review. there should be no logic changes, though. a side benefit is there's no longer a need to do a type assertion in ultest to make it set the fake filesystem to use. that can be passed in directly now. additionally, this makes the access commands much easier to test. Change-Id: I29cf6a2144248a58b7a605a7ae0a5ada5cfd57b6
168 lines
5.0 KiB
Go
168 lines
5.0 KiB
Go
// Copyright (C) 2021 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/zeebo/clingy"
|
|
"github.com/zeebo/errs"
|
|
)
|
|
|
|
type external struct {
|
|
interactive bool // controls if interactive input is allowed
|
|
|
|
dirs struct {
|
|
loaded bool // true if Setup has been called
|
|
current string // current config directory
|
|
legacy string // old config directory
|
|
}
|
|
|
|
migration struct {
|
|
migrated bool // true if a migration has been attempted
|
|
err error // any error from the migration attempt
|
|
}
|
|
|
|
config struct {
|
|
loaded bool // true if the existing config file is successfully loaded
|
|
values map[string][]string // the existing configuration
|
|
}
|
|
|
|
access struct {
|
|
loaded bool // true if we've successfully loaded access.json
|
|
defaultName string // default access name to use from accesses
|
|
accesses map[string]string // map of all of the stored accesses
|
|
}
|
|
}
|
|
|
|
func newExternal() *external {
|
|
return &external{}
|
|
}
|
|
|
|
func (ex *external) Setup(f clingy.Flags) {
|
|
ex.interactive = f.Flag(
|
|
"interactive", "Controls if interactive input is allowed", true,
|
|
clingy.Transform(strconv.ParseBool),
|
|
clingy.Advanced,
|
|
).(bool)
|
|
|
|
ex.dirs.current = f.Flag(
|
|
"config-dir", "Directory that stores the configuration",
|
|
appDir(false, "storj", "uplink"),
|
|
).(string)
|
|
|
|
ex.dirs.legacy = f.Flag(
|
|
"legacy-config-dir", "Directory that stores legacy configuration. Only used during migration",
|
|
appDir(true, "storj", "uplink"),
|
|
clingy.Advanced,
|
|
).(string)
|
|
|
|
ex.dirs.loaded = true
|
|
}
|
|
|
|
func (ex *external) accessFile() string { return filepath.Join(ex.dirs.current, "access.json") }
|
|
func (ex *external) configFile() string { return filepath.Join(ex.dirs.current, "config.ini") }
|
|
func (ex *external) legacyConfigFile() string { return filepath.Join(ex.dirs.legacy, "config.yaml") }
|
|
|
|
// Dynamic is called by clingy to look up values for global flags not specified on the command
|
|
// line. This call lets us fill in values from config files or environment variables.
|
|
func (ex *external) Dynamic(name string) (vals []string, err error) {
|
|
key := "UPLINK_" + strings.ToUpper(strings.ReplaceAll(name, "-", "_"))
|
|
if val, ok := os.LookupEnv(key); ok {
|
|
return []string{val}, nil
|
|
}
|
|
|
|
// if we have not yet loaded the directories, we should not try to migrate
|
|
// and load the current config.
|
|
if !ex.dirs.loaded {
|
|
return nil, nil
|
|
}
|
|
|
|
// allow errors from migration and configuration loading so that calls to
|
|
// `uplink setup` can happen and write out a new configuration.
|
|
if err := ex.migrate(); err != nil {
|
|
return nil, nil //nolint
|
|
}
|
|
if err := ex.loadConfig(); err != nil {
|
|
return nil, nil //nolint
|
|
}
|
|
|
|
return ex.config.values[name], nil
|
|
}
|
|
|
|
// Wrap is called by clingy with the command to be executed.
|
|
func (ex *external) Wrap(ctx clingy.Context, cmd clingy.Command) error {
|
|
if err := ex.migrate(); err != nil {
|
|
// TODO(jeff): prompt for initial setup?
|
|
return err
|
|
}
|
|
if !ex.config.loaded {
|
|
// TODO(jeff): prompt for initial config setup
|
|
_ = false
|
|
}
|
|
return cmd.Execute(ctx)
|
|
}
|
|
|
|
// PromptInput gets a line of input text from the user and returns an error if
|
|
// interactive mode is disabled.
|
|
func (ex *external) PromptInput(ctx clingy.Context, prompt string) (input string, err error) {
|
|
if !ex.interactive {
|
|
return "", errs.New("required user input in non-interactive setting")
|
|
}
|
|
fmt.Fprint(ctx.Stdout(), prompt, " ")
|
|
_, err = fmt.Fscanln(ctx.Stdin(), &input)
|
|
return input, err
|
|
}
|
|
|
|
// appDir returns best base directory for the currently running operating system. It
|
|
// has a legacy bool to have it return the same values that storj.io/common/fpath.ApplicationDir
|
|
// would have returned.
|
|
func appDir(legacy bool, subdir ...string) string {
|
|
for i := range subdir {
|
|
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
|
|
subdir[i] = strings.Title(subdir[i])
|
|
} else {
|
|
subdir[i] = strings.ToLower(subdir[i])
|
|
}
|
|
}
|
|
var appdir string
|
|
home := os.Getenv("HOME")
|
|
|
|
switch runtime.GOOS {
|
|
case "windows":
|
|
// Windows standards: https://msdn.microsoft.com/en-us/library/windows/apps/hh465094.aspx?f=255&MSPPError=-2147217396
|
|
for _, env := range []string{"AppData", "AppDataLocal", "UserProfile", "Home"} {
|
|
val := os.Getenv(env)
|
|
if val != "" {
|
|
appdir = val
|
|
break
|
|
}
|
|
}
|
|
case "darwin":
|
|
// Mac standards: https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/MacOSXDirectories/MacOSXDirectories.html
|
|
appdir = filepath.Join(home, "Library", "Application Support")
|
|
case "linux":
|
|
fallthrough
|
|
default:
|
|
// Linux standards: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
|
if legacy {
|
|
appdir = os.Getenv("XDG_DATA_HOME")
|
|
if appdir == "" && home != "" {
|
|
appdir = filepath.Join(home, ".local", "share")
|
|
}
|
|
} else {
|
|
appdir = os.Getenv("XDG_CONFIG_HOME")
|
|
if appdir == "" && home != "" {
|
|
appdir = filepath.Join(home, ".config")
|
|
}
|
|
}
|
|
}
|
|
return filepath.Join(append([]string{appdir}, subdir...)...)
|
|
}
|