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

Feature logging verbosity via env in servercmd #34

Closed
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
45 changes: 36 additions & 9 deletions hack/ccp/internal/servercmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os/signal"
"path/filepath"
"runtime"
"strconv"
"syscall"

"github.com/peterbourgon/ff/v3"
Expand All @@ -18,22 +19,48 @@ import (
"github.com/artefactual/archivematica/hack/ccp/internal/rootcmd"
)

const (
defaultVerbosity int = 0
minimumVerbosity int = 0
maximumVerbosity int = 10
)

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 < minimumVerbosity || value > maximumVerbosity {
return defaultVerbosity
}

return value
}

func New(rootConfig *rootcmd.Config, out io.Writer) *ffcli.Command {
cfg := Config{
rootConfig: rootConfig,
out: out,
}

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",
Expand Down