Migrate to github.com/urfave/cli/v3 (#2951)

This commit is contained in:
6543
2024-07-17 16:26:35 -07:00
committed by GitHub
parent e39345688d
commit cd5f6f71a2
112 changed files with 817 additions and 673 deletions

View File

@@ -1,6 +1,7 @@
package config
import (
"context"
"encoding/json"
"errors"
"os"
@@ -8,7 +9,7 @@ import (
"github.com/adrg/xdg"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
"github.com/zalando/go-keyring"
)
@@ -32,12 +33,12 @@ func (c *Config) MergeIfNotSet(c2 *Config) {
var skipSetupForCommands = []string{"setup", "help", "h", "version", "update", "lint", "exec", ""}
func Load(c *cli.Context) error {
func Load(ctx context.Context, c *cli.Command) error {
if firstArg := c.Args().First(); slices.Contains(skipSetupForCommands, firstArg) {
return nil
}
config, err := Get(c, c.String("config"))
config, err := Get(ctx, c, c.String("config"))
if err != nil {
return err
}
@@ -80,11 +81,11 @@ func getConfigPath(configPath string) (string, error) {
return configPath, nil
}
func Get(ctx *cli.Context, _configPath string) (*Config, error) {
c := &Config{
LogLevel: ctx.String("log-level"),
Token: ctx.String("token"),
ServerURL: ctx.String("server"),
func Get(_ context.Context, c *cli.Command, _configPath string) (*Config, error) {
conf := &Config{
LogLevel: c.String("log-level"),
Token: c.String("token"),
ServerURL: c.String("server"),
}
configPath, err := getConfigPath(_configPath)
@@ -109,33 +110,33 @@ func Get(ctx *cli.Context, _configPath string) (*Config, error) {
if err != nil {
return nil, err
}
c.MergeIfNotSet(configFromFile)
conf.MergeIfNotSet(configFromFile)
log.Debug().Msg("Loaded config from file")
}
// if server or token are explicitly set, use them
if ctx.IsSet("server") || ctx.IsSet("token") {
return c, nil
if c.IsSet("server") || c.IsSet("token") {
return conf, nil
}
// load token from keyring
service := ctx.App.Name
secret, err := keyring.Get(service, c.ServerURL)
service := c.Root().Name
secret, err := keyring.Get(service, conf.ServerURL)
if errors.Is(err, keyring.ErrUnsupportedPlatform) {
log.Warn().Msg("Keyring is not supported on this platform")
return c, nil
return conf, nil
}
if errors.Is(err, keyring.ErrNotFound) {
log.Warn().Msg("Token not found in keyring")
return c, nil
return conf, nil
}
c.Token = secret
conf.Token = secret
return c, nil
return conf, nil
}
func Save(ctx *cli.Context, _configPath string, c *Config) error {
config, err := json.Marshal(c)
func Save(_ context.Context, c *cli.Command, _configPath string, conf *Config) error {
config, err := json.Marshal(conf)
if err != nil {
return err
}
@@ -146,8 +147,8 @@ func Save(ctx *cli.Context, _configPath string, c *Config) error {
}
// save token to keyring
service := ctx.App.Name
err = keyring.Set(service, c.ServerURL, c.Token)
service := c.Root().Name
err = keyring.Set(service, conf.ServerURL, conf.Token)
if err != nil {
return err
}