Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix config loading of cli #3764

Merged
merged 6 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions cli/common/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ var GlobalFlags = append([]cli.Flag{
Aliases: []string{"c"},
Usage: "path to config file",
},
&cli.StringFlag{
EnvVars: []string{"WOODPECKER_TOKEN"},
Name: "token",
Aliases: []string{"t"},
Usage: "server auth token",
},
&cli.StringFlag{
EnvVars: []string{"WOODPECKER_SERVER"},
Name: "server",
Aliases: []string{"s"},
Usage: "server address",
},
&cli.StringFlag{
EnvVars: []string{"WOODPECKER_TOKEN"},
Name: "token",
Aliases: []string{"t"},
Usage: "server auth token",
},
&cli.BoolFlag{
EnvVars: []string{"WOODPECKER_DISABLE_UPDATE_CHECK"},
Name: "disable-update-check",
Expand Down
92 changes: 56 additions & 36 deletions cli/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ type Config struct {
LogLevel string `json:"log_level"`
}

func (c *Config) MergeIfNotSet(c2 *Config) {
if c.ServerURL == "" {
c.ServerURL = c2.ServerURL
}
if c.Token == "" {
c.Token = c2.Token
}
if c.LogLevel == "" {
c.LogLevel = c2.LogLevel
}
}

var skipSetupForCommands = []string{"setup", "help", "h", "version", "update", "lint", "exec", ""}

func Load(c *cli.Context) error {
Expand All @@ -30,39 +42,27 @@ func Load(c *cli.Context) error {
return err
}

if config == nil {
config = &Config{
LogLevel: "info",
ServerURL: c.String("server-url"),
Token: c.String("token"),
}
if config.ServerURL == "" || config.Token == "" {
log.Info().Msg("The woodpecker-cli is not yet set up. Please run `woodpecker-cli setup` or provide the required environment variables / flags.")
return errors.New("woodpecker-cli is not configured")
}

if !c.IsSet("server") {
err = c.Set("server", config.ServerURL)
if err != nil {
return err
}
err = c.Set("server", config.ServerURL)
if err != nil {
return err
}

if !c.IsSet("token") {
err = c.Set("token", config.Token)
if err != nil {
return err
}
err = c.Set("token", config.Token)
if err != nil {
return err
}

if !c.IsSet("log-level") {
err = c.Set("log-level", config.LogLevel)
if err != nil {
return err
}
err = c.Set("log-level", config.LogLevel)
if err != nil {
return err
}

if config.ServerURL == "" || config.Token == "" {
log.Info().Msg("The woodpecker-cli is not yet set up. Please run `woodpecker-cli setup` or provide the required environment variables / flags.")
return errors.New("woodpecker-cli is not configured")
}
log.Debug().Any("config", config).Msg("Loaded config")

return nil
}
Expand All @@ -81,35 +81,55 @@ func getConfigPath(configPath string) (string, error) {
}

func Get(ctx *cli.Context, _configPath string) (*Config, error) {
c := &Config{
LogLevel: ctx.String("log-level"),
Token: ctx.String("token"),
ServerURL: ctx.String("server"),
}

configPath, err := getConfigPath(_configPath)
if err != nil {
return nil, err
}

log.Debug().Str("configPath", configPath).Msg("Checking for config file")

content, err := os.ReadFile(configPath)
if err != nil && !os.IsNotExist(err) {
switch {
case err != nil && !os.IsNotExist(err):
log.Debug().Err(err).Msg("Failed to read the config file")
return nil, err
} else if err != nil && os.IsNotExist(err) {

case err != nil && os.IsNotExist(err):
log.Debug().Msg("The config file does not exist")
return nil, nil

default:
configFromFile := &Config{}
err = json.Unmarshal(content, configFromFile)
if err != nil {
return nil, err
}
c.MergeIfNotSet(configFromFile)
log.Debug().Msg("Loaded config from file")
}

c := &Config{}
err = json.Unmarshal(content, c)
if err != nil {
return nil, err
// if server or token are explicitly set, use them
if ctx.IsSet("server") || ctx.IsSet("token") {
return c, nil
}

// load token from keyring
service := ctx.App.Name
secret, err := keyring.Get(service, c.ServerURL)
if err != nil && !errors.Is(err, keyring.ErrNotFound) {
return nil, err
if errors.Is(err, keyring.ErrUnsupportedPlatform) {
log.Warn().Msg("Keyring is not supported on this platform")
return c, nil
}
if err == nil {
c.Token = secret
if errors.Is(err, keyring.ErrNotFound) {
log.Warn().Msg("Token not found in keyring")
return c, nil
}
c.Token = secret

return c, nil
}
Expand Down
27 changes: 27 additions & 0 deletions cli/internal/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package config

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestConfigMerge(t *testing.T) {
config := &Config{
ServerURL: "http://localhost:8080",
Token: "1234567890",
LogLevel: "debug",
}

configFromFile := &Config{
ServerURL: "https://ci.woodpecker-ci.org",
Token: "",
LogLevel: "info",
}

config.MergeIfNotSet(configFromFile)

assert.Equal(t, config.ServerURL, "http://localhost:8080")
assert.Equal(t, config.Token, "1234567890")
assert.Equal(t, config.LogLevel, "debug")
}
6 changes: 3 additions & 3 deletions cli/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ var Command = &cli.Command{
Name: "setup",
Usage: "setup the woodpecker-cli for the first time",
Args: true,
ArgsUsage: "[server-url]",
ArgsUsage: "[server]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "server-url",
Name: "server",
Usage: "The URL of the woodpecker server",
},
&cli.StringFlag{
Expand All @@ -46,7 +46,7 @@ func setup(c *cli.Context) error {
}
}

serverURL := c.String("server-url")
serverURL := c.String("server")
if serverURL == "" {
serverURL = c.Args().First()
}
Expand Down
1 change: 1 addition & 0 deletions docker/Dockerfile.cli.alpine.multiarch
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ FROM docker.io/alpine:3.20
ENV CA_CERTIFICATES_VERSION="20240226-r0"
RUN apk add -U --no-cache ca-certificates=${CA_CERTIFICATES_VERSION}
ENV GODEBUG=netdns=go
ENV WOODPECKER_DISABLE_UPDATE_CHECK=true

COPY --from=build /src/dist/woodpecker-cli /bin/

Expand Down
1 change: 1 addition & 0 deletions docker/Dockerfile.cli.multiarch
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ RUN --mount=type=cache,target=/root/.cache/go-build \

FROM scratch
ENV GODEBUG=netdns=go
ENV WOODPECKER_DISABLE_UPDATE_CHECK=true

# copy certs from build image
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
Expand Down