Skip to content

Commit

Permalink
feat: write out parsed configs (#707)
Browse files Browse the repository at this point in the history
## Which problem is this PR solving?

We have often been asked for the ability to see what refinery thinks its
configurations are. We support this with a debug command, but it
requires a running refinery configured to allow debug queries, and a
configuration that allows connecting to it.

This PR adds command-line switches to read the configs, apply defaults
and environment variables, and write the data back out to a file. If you
use this feature, refinery doesn't start, it just writes the file and
exits.

## Short description of the changes

- Add function to write YAML out
- Add cmdline parameters to control it

Dependent on #706
  • Loading branch information
kentquirk authored Jun 9, 2023
1 parent 7e4386d commit 1d82214
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
2 changes: 2 additions & 0 deletions config/cmdenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type CmdEnv struct {
InterfaceNames bool `long:"interface-names" description:"Print system's network interface names and exit."`
Validate bool `short:"V" long:"validate" description:"Validate the configuration files, writing results to stdout, and exit with 0 if valid, 1 if invalid."`
NoValidate bool `long:"no-validate" description:"Do not attempt to validate the configuration files. Makes --validate meaningless."`
WriteConfig string `long:"write-config" description:"After applying defaults, environment variables, and command line values, write the loaded configuration to the specified file as YAML and exit."`
WriteRules string `long:"write-rules" description:"After applying defaults, write the loaded rules to the specified file as YAML and exit."`
}

func NewCmdEnvOptions(args []string) (*CmdEnv, error) {
Expand Down
37 changes: 35 additions & 2 deletions config/file_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package config

import (
"errors"
"fmt"
"net"
"os"
"strings"
"sync"
"time"

"gopkg.in/yaml.v3"
)

// In order to be able to unmarshal "15s" etc. into time.Duration, we need to
Expand Down Expand Up @@ -295,9 +299,22 @@ func newFileConfig(opts *CmdEnv) (*fileConfig, error) {
return cfg, nil
}

// writeYAMLToFile renders the given data item to a YAML file
func writeYAMLToFile(data any, filename string) error {
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
encoder := yaml.NewEncoder(f)
encoder.SetIndent(2)
return encoder.Encode(data)
}

// NewConfig creates a new Config object from the given arguments; if args is
// nil, it uses the command line arguments. Because errors may be caused by
// validation problems, it returns the config even in the case of errors.
// nil, it uses the command line arguments.
// It also dumps the config and rules to the given files, if specified, which
// will cause the program to exit.
func NewConfig(opts *CmdEnv, errorCallback func(error)) (Config, error) {
cfg, err := newFileConfig(opts)
// only exit if we have no config at all; if it fails validation, we'll
Expand All @@ -306,6 +323,22 @@ func NewConfig(opts *CmdEnv, errorCallback func(error)) (Config, error) {
return nil, err
}

if opts.WriteConfig != "" {
if err := writeYAMLToFile(cfg.mainConfig, opts.WriteConfig); err != nil {
fmt.Printf("Error writing config: %s\n", err)
os.Exit(1)
}
}
if opts.WriteRules != "" {
if err := writeYAMLToFile(cfg.rulesConfig, opts.WriteRules); err != nil {
fmt.Printf("Error writing rules: %s\n", err)
os.Exit(1)
}
}
if opts.WriteConfig != "" || opts.WriteRules != "" {
os.Exit(0)
}

cfg.callbacks = make([]func(), 0)
cfg.errorCallback = errorCallback

Expand Down
2 changes: 1 addition & 1 deletion config_complete.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ LegacyMetrics:
## metrics.
##
## Not eligible for live reload.
APIKey: abcd1234
APIKey: SetThisToAHoneycombKey

## Dataset is the Honeycomb dataset to which metrics will be sent.
##
Expand Down

0 comments on commit 1d82214

Please sign in to comment.