Skip to content

Commit

Permalink
config: add support for environment variables
Browse files Browse the repository at this point in the history
CLI flags are still respected.

Env vars will be prefixed with DROPLET_AGENT_ ; for example, DROPLET_AGENT_DEBUG maps to the --debug CLI flag.

This makes it easier to drive config with systemd drop-in files with the `Environment=` directive, for example.
  • Loading branch information
anitgandhi committed Oct 9, 2024
1 parent cedd74b commit 690a712
Show file tree
Hide file tree
Showing 15 changed files with 934 additions and 34 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ go 1.22
require (
github.com/fsnotify/fsnotify v1.7.0
github.com/opencontainers/selinux v1.11.0
github.com/peterbourgon/ff/v3 v3.4.0
go.uber.org/mock v0.4.0
golang.org/x/crypto v0.16.0
golang.org/x/net v0.19.0
golang.org/x/sync v0.5.0
golang.org/x/sys v0.15.0
golang.org/x/time v0.3.0
)

require go.uber.org/mock v0.4.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/opencontainers/selinux v1.11.0 h1:+5Zbo97w3Lbmb3PeqQtpmTkMwsW5nRI3YaLpt7tQ7oU=
github.com/opencontainers/selinux v1.11.0/go.mod h1:E5dMC3VPuVvVHDYmi78qvhJp8+M586T4DlDRYpFkyec=
github.com/peterbourgon/ff/v3 v3.4.0 h1:QBvM/rizZM1cB0p0lGMdmR7HxZeI/ZrBWB4DqLkMUBc=
github.com/peterbourgon/ff/v3 v3.4.0/go.mod h1:zjJVUhx+twciwfDl0zBcFzl4dW8axCRyXE/eKY9RztQ=
go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU=
go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY=
Expand Down
25 changes: 0 additions & 25 deletions internal/config/cliargs.go

This file was deleted.

28 changes: 21 additions & 7 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

package config

import "time"
import (
"flag"
"os"
"time"

"github.com/peterbourgon/ff/v3"
)

const (
AppFullName = "DigitalOcean Droplet Agent (code name: DOTTY)"
Expand All @@ -26,12 +32,20 @@ type Conf struct {

// Init initializes the agent's configuration
func Init() *Conf {
cliArgs := parseCLIArgs()
return &Conf{
UseSyslog: cliArgs.useSyslog,
DebugMode: cliArgs.debugMode,
CustomSSHDPort: cliArgs.sshdPort,
CustomSSHDCfgFile: cliArgs.sshdCfgFile,
cfg := Conf{
AuthorizedKeysCheckInterval: backgroundJobInterval,
}

fs := flag.NewFlagSet("droplet-agent", flag.ExitOnError)

fs.BoolVar(&cfg.UseSyslog, "syslog", false, "Use syslog service for logging")
fs.BoolVar(&cfg.DebugMode, "debug", false, "Turn on debug mode")
fs.IntVar(&cfg.CustomSSHDPort, "sshd_port", 0, "The port sshd is binding to")
fs.StringVar(&cfg.CustomSSHDCfgFile, "sshd_config", "", "The location of sshd_config")

ff.Parse(fs, os.Args[1:],
ff.WithEnvVarPrefix("DROPLET_AGENT"),
)

return &cfg
}
14 changes: 14 additions & 0 deletions vendor/github.com/peterbourgon/ff/v3/.gitignore

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

201 changes: 201 additions & 0 deletions vendor/github.com/peterbourgon/ff/v3/LICENSE

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

Loading

0 comments on commit 690a712

Please sign in to comment.