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: override configuration from flags only if set #865

Merged
merged 6 commits into from
Mar 30, 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
34 changes: 27 additions & 7 deletions internal/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"context"
"strings"

"github.com/go-shiori/shiori/internal/config"
"github.com/go-shiori/shiori/internal/http"
"github.com/go-shiori/shiori/internal/model"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

func newServerCommand() *cobra.Command {
Expand All @@ -27,6 +29,12 @@ func newServerCommand() *cobra.Command {
return cmd
}

func setIfFlagChanged(flagName string, flags *pflag.FlagSet, cfg *config.Config, fn func(cfg *config.Config)) {
if flags.Changed(flagName) {
fn(cfg)
}
}

func newServerCommandHandler() func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
ctx := context.Background()
Expand Down Expand Up @@ -54,13 +62,25 @@ func newServerCommandHandler() func(cmd *cobra.Command, args []string) {
rootPath += "/"
}

// Override configuration from flags
cfg.Http.Port = port
cfg.Http.Address = address + ":"
cfg.Http.RootPath = rootPath
cfg.Http.AccessLog = accessLog
cfg.Http.ServeWebUI = serveWebUI
cfg.Http.SecretKey = secretKey
// Override configuration from flags if needed
setIfFlagChanged("port", cmd.Flags(), cfg, func(cfg *config.Config) {
cfg.Http.Port = port
})
setIfFlagChanged("address", cmd.Flags(), cfg, func(cfg *config.Config) {
cfg.Http.Address = address + ":"
})
setIfFlagChanged("webroot", cmd.Flags(), cfg, func(cfg *config.Config) {
cfg.Http.RootPath = rootPath
})
setIfFlagChanged("access-log", cmd.Flags(), cfg, func(cfg *config.Config) {
cfg.Http.AccessLog = accessLog
})
setIfFlagChanged("serve-web-ui", cmd.Flags(), cfg, func(cfg *config.Config) {
cfg.Http.ServeWebUI = serveWebUI
})
setIfFlagChanged("secret-key", cmd.Flags(), cfg, func(cfg *config.Config) {
cfg.Http.SecretKey = secretKey
})

dependencies.Log.Infof("Starting Shiori v%s", model.BuildVersion)

Expand Down
72 changes: 72 additions & 0 deletions internal/cmd/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package cmd

import (
"testing"

"github.com/go-shiori/shiori/internal/config"
"github.com/spf13/pflag"
"github.com/stretchr/testify/require"
)

func Test_setIfFlagChanged(t *testing.T) {
type args struct {
flagName string
flags func() *pflag.FlagSet
cfg *config.Config
fn func(cfg *config.Config)
}
tests := []struct {
name string
args args
assertFn func(t *testing.T, cfg *config.Config)
}{
{
name: "Flag didn't change",
args: args{
flagName: "port",
flags: func() *pflag.FlagSet {
return &pflag.FlagSet{}
},
cfg: &config.Config{
Http: &config.HttpConfig{
Port: 8080,
},
},
fn: func(cfg *config.Config) {
cfg.Http.Port = 9999
},
},
assertFn: func(t *testing.T, cfg *config.Config) {
require.Equal(t, cfg.Http.Port, 8080)
},
},
{
name: "Flag changed",
args: args{
flagName: "port",
flags: func() *pflag.FlagSet {
pf := &pflag.FlagSet{}
pf.IntP("port", "p", 8080, "Port used by the server")
pf.Set("port", "9999")
return pf
},
cfg: &config.Config{
Http: &config.HttpConfig{
Port: 8080,
},
},
fn: func(cfg *config.Config) {
cfg.Http.Port = 9999
},
},
assertFn: func(t *testing.T, cfg *config.Config) {
require.Equal(t, cfg.Http.Port, 9999)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
setIfFlagChanged(tt.args.flagName, tt.args.flags(), tt.args.cfg, tt.args.fn)
})
}
}
Loading