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

Adding generate-config functionality #95

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ Flags:
--debug-api [ENV: CA_DEBUG_API] Enable Circonus API debug messages
--debug-cgm [ENV: CA_DEBUG_CGM] Enable CGM debug messages
--debug-dump-metrics string [ENV: CA_DEBUG_DUMP_METRICS] Directory to dump sent metrics
--generate-config string Generate config file (json|toml|yaml) and exit
-h, --help help for circonus-agent
--host-etc string [ENV: HOST_ETC] Host /etc directory
--host-proc string [ENV: HOST_PROC] Host /proc directory
Expand Down
28 changes: 28 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ in JSON format.`,
return
}

//
// generate tmp config file in etc/ and exit
//
if viper.GetString(config.KeyGenerateConfig) != "" {
f, err := os.Create(fmt.Sprintf("%s/circonus-agent.%s.tmp", defaults.EtcPath, viper.GetString(config.KeyGenerateConfig)))
if err != nil {
log.Fatal().Err(err).Msg("generate-config")
}
defer f.Close()
if err := config.GenerateConfig(f); err != nil {
log.Fatal().Err(err).Msg("generate-config")
}
return
}

log.Info().
Int("pid", os.Getpid()).
Str("name", release.NAME).
Expand Down Expand Up @@ -1286,6 +1301,19 @@ func init() {
}
}

{
const (
key = config.KeyGenerateConfig
longOpt = "generate-config"
description = "Generate config file (json|toml|yaml) and exit"
)

RootCmd.Flags().String(longOpt, "", description)
if err := viper.BindPFlag(key, RootCmd.Flags().Lookup(longOpt)); err != nil {
bindFlagError(longOpt, err)
}
}

//
// Hidden, deprecated flags so old configs don't break - the flags are just ignored
//
Expand Down
24 changes: 23 additions & 1 deletion etc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ The `etc` directory is used for the main configuration of the Circonus agent, as

File name: `circonus-agent.(json|toml|yaml)`

An example configuration, with default values, can be retrieved using the `--show-config=(json|toml|yaml)`.
An example configuration, with default values, can be retrieved using the `--show-config=(json|toml|yaml)` or `--generate-config=(json|toml|yaml)`.

## Configuration file quick start

### Using show-config

Run one of the following (from the base directory where the agent was installed) and edit the resulting configuration file:


Expand All @@ -29,6 +31,26 @@ sbin\circonus-agentd.exe --show-config=yaml > etc\circonus-agent.yaml.tmp

Edit the resulting file to customize configuration settings. When done, rename file to remove the `.tmp` extension. (e.g. `mv etc/circonus-agent.json.tmp` `etc/circonus-agent.json`)

### Using generate-config

Run one of the following (from the base directory where the agent was installed) and edit the resulting configuration file:

```
sbin/circonus-agentd --generate-config=json
sbin/circonus-agentd --generate-config=toml
sbin/circonus-agentd --generate-config=yaml
```

or, on Windows:

```
sbin\circonus-agentd.exe --generate-config=json
sbin\circonus-agentd.exe --generate-config=toml
sbin\circonus-agentd.exe --generate-config=yaml
```

Edit the resulting file to customize configuration settings. When done, rename file to remove the `.tmp` extension. (e.g. `mv etc/circonus-agent.json.tmp` `etc/circonus-agent.json`)

---

# Builtin Collector Configurations
Expand Down
35 changes: 35 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ const (
// KeyShowConfig - show configuration and exit
KeyShowConfig = "show-config"

// KeyGenerateConfig - generate temporary config file in given format and exit
KeyGenerateConfig = "generate-config"

// KeyShowVersion - show version information and exit
KeyShowVersion = "version"

Expand Down Expand Up @@ -451,3 +454,35 @@ func ShowConfig(w io.Writer) error {
fmt.Fprintln(w, string(data))
return nil
}

// GenerateConfig creates a temp config file in etc/
func GenerateConfig(w io.Writer) error {
var cfg *Config
var err error
var data []byte

cfg, err = getConfig()
if err != nil {
return err
}

format := viper.GetString(KeyGenerateConfig)

switch format {
case "json":
data, err = json.MarshalIndent(cfg, " ", " ")
case "yaml":
data, err = yaml.Marshal(cfg)
case "toml":
data, err = toml.Marshal(*cfg)
default:
return errors.Errorf("unknown config format '%s'", format)
}

if err != nil {
return errors.Wrapf(err, "formatting config (%s)", format)
}

fmt.Fprintln(w, string(data))
return nil
}
32 changes: 32 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,38 @@ func TestShowConfig(t *testing.T) {
}
}

func TestGenerateConfig(t *testing.T) {
t.Log("Testing ShowConfig")
zerolog.SetGlobalLevel(zerolog.Disabled)

t.Log("YAML")
{
viper.Set(KeyGenerateConfig, "yaml")
err := GenerateConfig(ioutil.Discard)
if err != nil {
t.Fatalf("expected no error, got %s", err)
}
}

t.Log("TOML")
{
viper.Set(KeyGenerateConfig, "toml")
err := GenerateConfig(ioutil.Discard)
if err != nil {
t.Fatalf("expected no error, got %s", err)
}
}

t.Log("JSON")
{
viper.Set(KeyGenerateConfig, "json")
err := GenerateConfig(ioutil.Discard)
if err != nil {
t.Fatalf("expected no error, got %s", err)
}
}
}

func TestGetConfig(t *testing.T) {
t.Log("Testing getConfig")
zerolog.SetGlobalLevel(zerolog.Disabled)
Expand Down