Skip to content

Commit

Permalink
feat: add version command and allow config path
Browse files Browse the repository at this point in the history
This commit add a new command to the app in the form of the `version`
command that allows to easily see what version of the package we're
using. Previously this was only visible when the server was starting.

It also add a new parameter that would allow the user to specify the
configuration folder to use. The default value is still `conf/`, but now
we can also specify the path we want.
  • Loading branch information
redat00 authored and oxyno-zeta committed Mar 20, 2024
1 parent c44ace2 commit ec545c4
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 39 deletions.
38 changes: 36 additions & 2 deletions cmd/s3-proxy/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import (
"fmt"
"os"

"github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/config"
"github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/log"
"github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/metrics"
Expand All @@ -9,20 +12,21 @@ import (
"github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/tracing"
"github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/version"
"github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/webhook"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
)

// Main package

func main() {
func startServer(mainConfDir string) {
// Create new logger
logger := log.NewLogger()

// Create configuration manager
cfgManager := config.NewManager(logger)

// Load configuration
err := cfgManager.Load()
err := cfgManager.Load(mainConfDir)
if err != nil {
logger.Fatal(err)
}
Expand Down Expand Up @@ -133,3 +137,33 @@ func main() {
logger.Fatal(err)
}
}

func main() {
var configFolder string

var rootCmd = &cobra.Command{
Use: "s3-proxy",
Short: "S3 Reverse Proxy",
Long: "S3 Reverse Proxy with GET, PUT and DELETE methods and authentication (OpenID Connect and Basic Auth)",
Run: func(_ *cobra.Command, _ []string) {
startServer(configFolder)
},
}

var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number of s3-proxy",
Run: func(_ *cobra.Command, _ []string) {
v := version.GetVersion()
fmt.Printf("version: %s (git commit: %s) built on %s", v.Version, v.GitCommit, v.BuildDate)
},
}

rootCmd.AddCommand(versionCmd)
rootCmd.PersistentFlags().StringVar(&configFolder, "config", "conf/", "Config folder (default is <Current Working Directory>/conf/)")

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/opentracing/opentracing-go v1.2.0
github.com/prometheus/client_golang v1.18.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.8.4
github.com/thoas/go-funk v0.9.3
Expand Down Expand Up @@ -50,6 +51,7 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/huandu/xstrings v1.4.0 // indirect
github.com/imdario/mergo v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/coreos/go-oidc/v3 v3.9.0 h1:0J/ogVOd4y8P0f0xUh8l9t07xRP/d8tccvjHl2dcsSo=
github.com/coreos/go-oidc/v3 v3.9.0/go.mod h1:rTKz2PYwftcrtoCzV5g5kvfJoWcm0Mk8AF8y1iAQro4=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -82,6 +83,8 @@ github.com/huandu/xstrings v1.4.0 h1:D17IlohoQq4UcpqD7fDk80P7l+lwAmlFaBHgOipl2FU
github.com/huandu/xstrings v1.4.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
Expand Down Expand Up @@ -128,6 +131,7 @@ github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3c
github.com/rafaeljusto/redigomock v0.0.0-20190202135759-257e089e14a1/go.mod h1:JaY6n2sDr+z2WTsXkOmNRUfDy6FN0L6Nk7x06ndm4tY=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryszard/goskiplist v0.0.0-20150312221310-2dfbae5fcf46 h1:GHRpF1pTW19a8tTFrMLUcfWwyC0pnifVo2ClaLq+hP8=
github.com/ryszard/goskiplist v0.0.0-20150312221310-2dfbae5fcf46/go.mod h1:uAQ5PCi+MFsC7HjREoAz1BU+Mq60+05gifQSsHSDG/8=
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
Expand All @@ -150,6 +154,8 @@ github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNo
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ=
Expand Down
2 changes: 1 addition & 1 deletion pkg/s3-proxy/config/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import "github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/log"
//go:generate mockgen -destination=./mocks/mock_Manager.go -package=mocks github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/config Manager
type Manager interface {
// Load configuration
Load() error
Load(mainConfDir string) error
// Get configuration object
GetConfig() *Config
// Add on change hook for configuration change
Expand Down
13 changes: 5 additions & 8 deletions pkg/s3-proxy/config/managerimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ import (
"github.com/thoas/go-funk"
)

// Main configuration folder path.
var mainConfigFolderPath = "conf/"

var validate = validator.New()

type managerimpl struct {
Expand All @@ -37,15 +34,15 @@ func (impl *managerimpl) AddOnChangeHook(hook func()) {
impl.onChangeHooks = append(impl.onChangeHooks, hook)
}

func (impl *managerimpl) Load() error {
func (impl *managerimpl) Load(mainConfDir string) error {
// List files
files, err := os.ReadDir(mainConfigFolderPath)
files, err := os.ReadDir(mainConfDir)
if err != nil {
return errors.WithStack(err)
}

// Generate viper instances for static configs
impl.configs = generateViperInstances(files)
impl.configs = generateViperInstances(files, mainConfDir)

// Load configuration
err = impl.loadConfiguration()
Expand Down Expand Up @@ -191,7 +188,7 @@ func (*managerimpl) loadDefaultConfigurationValues(vip *viper.Viper) {
vip.SetDefault("templates.delete.status", DefaultTemplateStatusNoContent)
}

func generateViperInstances(files []os.DirEntry) []*viper.Viper {
func generateViperInstances(files []os.DirEntry, mainConfDir string) []*viper.Viper {
list := make([]*viper.Viper, 0)
// Loop over static files to create viper instance for them
funk.ForEach(files, func(file os.DirEntry) {
Expand All @@ -205,7 +202,7 @@ func generateViperInstances(files []os.DirEntry) []*viper.Viper {
// Set config name
vip.SetConfigName(cfgFileName)
// Add configuration path
vip.AddConfigPath(mainConfigFolderPath)
vip.AddConfigPath(mainConfDir)
// Append it
list = append(list, vip)
}
Expand Down
30 changes: 6 additions & 24 deletions pkg/s3-proxy/config/managerimpl_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1520,15 +1520,12 @@ targets:
defer os.Remove(k)
}

// Change var for main configuration file
mainConfigFolderPath = dir

ctx := &managerimpl{
logger: log.NewLogger(),
}

// Load config
err = ctx.Load()
err = ctx.Load(dir)

if (err != nil) != tt.wantErr {
t.Errorf("Load() error = %v, wantErr %v", err, tt.wantErr)
Expand Down Expand Up @@ -1601,9 +1598,6 @@ targets:
defer os.Remove(k)
}

// Change var for main configuration file
mainConfigFolderPath = dir

ctx := &managerimpl{
logger: log.NewLogger(),
}
Expand All @@ -1613,7 +1607,7 @@ targets:
})

// Load config
err = ctx.Load()
err = ctx.Load(dir)
assert.NoError(t, err)
// Get configuration
res := ctx.GetConfig()
Expand Down Expand Up @@ -1806,9 +1800,6 @@ targets:
defer os.Remove(k)
}

// Change var for main configuration file
mainConfigFolderPath = dir

ctx := &managerimpl{
logger: log.NewLogger(),
}
Expand All @@ -1818,7 +1809,7 @@ targets:
})

// Load config
err = ctx.Load()
err = ctx.Load(dir)
assert.NoError(t, err)
// Get configuration
res := ctx.GetConfig()
Expand Down Expand Up @@ -2008,9 +1999,6 @@ targets:
defer os.Remove(k)
}

// Change var for main configuration file
mainConfigFolderPath = dir

ctx := &managerimpl{
logger: log.NewLogger(),
}
Expand All @@ -2020,7 +2008,7 @@ targets:
})

// Load config
err = ctx.Load()
err = ctx.Load(dir)
assert.NoError(t, err)
// Get configuration
res := ctx.GetConfig()
Expand Down Expand Up @@ -2217,9 +2205,6 @@ targets:
defer os.Remove(k)
}

// Change var for main configuration file
mainConfigFolderPath = dir

ctx := &managerimpl{
logger: log.NewLogger(),
}
Expand All @@ -2229,7 +2214,7 @@ targets:
})

// Load config
err = ctx.Load()
err = ctx.Load(dir)
assert.NoError(t, err)
// Get configuration
res := ctx.GetConfig()
Expand Down Expand Up @@ -2451,9 +2436,6 @@ targets:
defer os.Remove(k)
}

// Change var for main configuration file
mainConfigFolderPath = dir

ctx := &managerimpl{
logger: log.NewLogger(),
}
Expand All @@ -2463,7 +2445,7 @@ targets:
})

// Load config
err = ctx.Load()
err = ctx.Load(dir)
assert.NoError(t, err)
// Get configuration
res := ctx.GetConfig()
Expand Down
8 changes: 4 additions & 4 deletions pkg/s3-proxy/config/mocks/mock_Manager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ec545c4

Please sign in to comment.