From f2832c2364998c3c7a512b4ebe523b4cc107c47e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isaac=20Pe=C3=B1a?= Date: Wed, 1 May 2024 23:23:30 +0200 Subject: [PATCH 1/3] Add an environment variable to configure logging verbosity in servercmd --- hack/ccp/internal/servercmd/cmd.go | 43 +++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/hack/ccp/internal/servercmd/cmd.go b/hack/ccp/internal/servercmd/cmd.go index 61bd3b969..87d22d2bb 100644 --- a/hack/ccp/internal/servercmd/cmd.go +++ b/hack/ccp/internal/servercmd/cmd.go @@ -8,6 +8,7 @@ import ( "os/signal" "path/filepath" "runtime" + "strconv" "syscall" "github.com/peterbourgon/ff/v3" @@ -18,6 +19,37 @@ import ( "github.com/artefactual/archivematica/hack/ccp/internal/rootcmd" ) +const ( + DefaultVerbosity int = 0 +) + +func (c *Config) RegisterFlags(fs *flag.FlagSet) { + fs.String("conifg", "", "Configuration file in the TOML file format") + fs.StringVar(&c.sharedDir, "shared-dir", "", "Shared directory") + fs.StringVar(&c.workflow, "workflow", "", "Workflow document") + fs.StringVar(&c.db.driver, "db.driver", "", "Database driver") + fs.StringVar(&c.db.dsn, "db.dsn", "", "Database DSN") + fs.StringVar(&c.api.admin.Addr, "api.admin.addr", "", "Admin API listen address") + fs.StringVar(&c.gearmin.addr, "gearmin.addr", ":4730", "Gearmin job server listen address") + + c.rootConfig.RegisterFlags(fs) +} + +func (c *Config) ConfigureFromEnv() { + if v := os.Getenv("VERBOSITY"); v != "" { + c.rootConfig.Verbosity = parseVerbosity(v) + } +} + +func parseVerbosity(v string) int { + value, err := strconv.Atoi(v) + if err == nil || value < 0 || value > 10 { + return DefaultVerbosity + } + + return value +} + func New(rootConfig *rootcmd.Config, out io.Writer) *ffcli.Command { cfg := Config{ rootConfig: rootConfig, @@ -25,15 +57,8 @@ func New(rootConfig *rootcmd.Config, out io.Writer) *ffcli.Command { } fs := flag.NewFlagSet("ccp server", flag.ExitOnError) - fs.String("config", "", "Configuration file in the TOML file format") - fs.StringVar(&cfg.sharedDir, "shared-dir", "", "Shared directory") - fs.StringVar(&cfg.workflow, "workflow", "", "Workflow document") - fs.StringVar(&cfg.db.driver, "db.driver", "", "Database driver") - fs.StringVar(&cfg.db.dsn, "db.dsn", "", "Database DSN") - fs.StringVar(&cfg.api.admin.Addr, "api.admin.addr", "", "Admin API listen address") - fs.StringVar(&cfg.gearmin.addr, "gearmin.addr", ":4730", "Gearmin job server listen address") - - rootConfig.RegisterFlags(fs) + cfg.RegisterFlags(fs) + cfg.ConfigureFromEnv() return &ffcli.Command{ Name: "server", From 9623dfd9240a212d0cd1135bfa4be6265c787b04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isaac=20Pe=C3=B1a?= Date: Wed, 1 May 2024 23:29:38 +0200 Subject: [PATCH 2/3] Add minimum and maximum verbosity values as constants --- hack/ccp/internal/servercmd/cmd.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/hack/ccp/internal/servercmd/cmd.go b/hack/ccp/internal/servercmd/cmd.go index 87d22d2bb..3556d3163 100644 --- a/hack/ccp/internal/servercmd/cmd.go +++ b/hack/ccp/internal/servercmd/cmd.go @@ -21,6 +21,8 @@ import ( const ( DefaultVerbosity int = 0 + MinimumVerbosity int = 0 + MaximumVerbosity int = 10 ) func (c *Config) RegisterFlags(fs *flag.FlagSet) { @@ -43,7 +45,7 @@ func (c *Config) ConfigureFromEnv() { func parseVerbosity(v string) int { value, err := strconv.Atoi(v) - if err == nil || value < 0 || value > 10 { + if err == nil || value < MinimumVerbosity || value > MaximumVerbosity { return DefaultVerbosity } From 804a09ade0d96cd425fcdd9d7d22401e762ea8a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isaac=20Pe=C3=B1a?= Date: Wed, 1 May 2024 23:52:02 +0200 Subject: [PATCH 3/3] Change contants to lowerCamelCase --- hack/ccp/internal/servercmd/cmd.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/hack/ccp/internal/servercmd/cmd.go b/hack/ccp/internal/servercmd/cmd.go index 3556d3163..130275139 100644 --- a/hack/ccp/internal/servercmd/cmd.go +++ b/hack/ccp/internal/servercmd/cmd.go @@ -20,9 +20,9 @@ import ( ) const ( - DefaultVerbosity int = 0 - MinimumVerbosity int = 0 - MaximumVerbosity int = 10 + defaultVerbosity int = 0 + minimumVerbosity int = 0 + maximumVerbosity int = 10 ) func (c *Config) RegisterFlags(fs *flag.FlagSet) { @@ -45,8 +45,8 @@ func (c *Config) ConfigureFromEnv() { func parseVerbosity(v string) int { value, err := strconv.Atoi(v) - if err == nil || value < MinimumVerbosity || value > MaximumVerbosity { - return DefaultVerbosity + if err == nil || value < minimumVerbosity || value > maximumVerbosity { + return defaultVerbosity } return value