Skip to content

Commit

Permalink
feat(config): remove --env-file flag feat
Browse files Browse the repository at this point in the history
  • Loading branch information
neelayu committed May 4, 2023
1 parent c20c683 commit e6f05c4
Show file tree
Hide file tree
Showing 8 changed files with 5 additions and 55 deletions.
5 changes: 0 additions & 5 deletions cmd/telegraf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ func runApp(args []string, outputBuffer io.Writer, pprof Server, c TelegrafConfi
g := GlobalFlags{
config: cCtx.StringSlice("config"),
configDir: cCtx.StringSlice("config-directory"),
dotEnvFile: cCtx.StringSlice("env-file"),
testWait: cCtx.Int("test-wait"),
watchConfig: cCtx.String("watch-config"),
pidFile: cCtx.String("pidfile"),
Expand Down Expand Up @@ -261,10 +260,6 @@ func runApp(args []string, outputBuffer io.Writer, pprof Server, c TelegrafConfi
Name: "config-directory",
Usage: "directory containing additional *.conf files",
},
&cli.StringSliceFlag{
Name: "env-file",
Usage: ".env file path",
},
// Int flags
&cli.IntFlag{
Name: "test-wait",
Expand Down
2 changes: 0 additions & 2 deletions cmd/telegraf/telegraf.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ var stop chan struct{}
type GlobalFlags struct {
config []string
configDir []string
dotEnvFile []string
testWait int
watchConfig string
pidFile string
Expand Down Expand Up @@ -216,7 +215,6 @@ func (t *Telegraf) loadConfiguration() (*config.Config, error) {
c.OutputFilters = t.outputFilters
c.InputFilters = t.inputFilters
c.SecretStoreFilters = t.secretstoreFilters
c.EnvFiles = t.dotEnvFile

var configFiles []string

Expand Down
20 changes: 4 additions & 16 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"sync"
"time"

"github.com/compose-spec/compose-go/cli"
"github.com/compose-spec/compose-go/template"
"github.com/compose-spec/compose-go/utils"
"github.com/coreos/go-semver/semver"
Expand Down Expand Up @@ -68,7 +67,6 @@ type Config struct {
SecretStoreFilters []string

SecretStores map[string]telegraf.SecretStore
EnvFiles []string

Agent *AgentConfig
Inputs []*models.RunningInput
Expand Down Expand Up @@ -484,7 +482,7 @@ func (c *Config) LoadAll(configFiles ...string) error {

// LoadConfigData loads TOML-formatted config data
func (c *Config) LoadConfigData(data []byte) error {
tbl, err := parseConfig(data, c.EnvFiles)
tbl, err := parseConfig(data)
if err != nil {
return fmt.Errorf("error parsing data: %w", err)
}
Expand Down Expand Up @@ -773,15 +771,15 @@ func fetchConfig(u *url.URL) ([]byte, error) {
// parseConfig loads a TOML configuration from a provided path and
// returns the AST produced from the TOML parser. When loading the file, it
// will find environment variables and replace them.
func parseConfig(contents []byte, envFiles []string) (*ast.Table, error) {
func parseConfig(contents []byte) (*ast.Table, error) {
contents = trimBOM(contents)
var err error
contents, err = removeComments(contents)
if err != nil {
return nil, err
}
// Convert the output buffer to a byte slice
outputBytes, err := substituteEnvironment(contents, envFiles)
outputBytes, err := substituteEnvironment(contents)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -867,18 +865,8 @@ func checkHashInsideString(line []byte, idx int) bool {
return dq%2 == 1 || sq%2 == 1
}

func substituteEnvironment(contents []byte, envFiles []string) ([]byte, error) {
func substituteEnvironment(contents []byte) ([]byte, error) {
envMap := utils.GetAsEqualsMap(os.Environ())
// envFiles has a higher precedence over os.Environ()
// but if envFile is non empty, then we use os.Environ() as a base
// to expand variables in the envFile itself
if len(envFiles) != 0 {
var err error
envMap, err = cli.GetEnvFromFile(envMap, ".", envFiles)
if err != nil {
return nil, err
}
}
retVal, err := template.Substitute(string(contents), func(k string) (string, bool) {
if v, ok := envMap[k]; ok {
return v, ok
Expand Down
15 changes: 1 addition & 14 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ func Test_envSub(t *testing.T) {
tests := []struct {
name string
setEnv func(*testing.T)
envFile []string
contents string
expected string
wantErr bool
Expand Down Expand Up @@ -181,26 +180,14 @@ func Test_envSub(t *testing.T) {
contents: "Should output ${NOT_SET:-${FALLBACK}}",
expected: "Should output my-fallback",
},
{
name: "env from file",
envFile: func(t *testing.T) []string {
tmp := t.TempDir()
env := filepath.Join(tmp, ".env")
err := os.WriteFile(env, []byte("envFile1=val1\nenvFile2=val2"), 0640)
require.NoError(t, err)
return []string{env}
}(t),
contents: "$envFile1 and $envFile2 come from envFile",
expected: "val1 and val2 come from envFile",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.setEnv != nil {
tt.setEnv(t)
}
actual, err := substituteEnvironment([]byte(tt.contents), tt.envFile)
actual, err := substituteEnvironment([]byte(tt.contents))
if tt.wantErr {
require.ErrorContains(t, err, tt.errSubstring)
return
Expand Down
1 change: 0 additions & 1 deletion docs/COMMANDS_AND_FLAGS.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ telegraf help
Here are some commonly used flags that users should be aware of:

* `--config-directory`: Read all config files from a directory
* `--env-file`: Path to a `.env` file
* `--debug`: Enable additional debug logging
* `--once`: Run one collection and flush interval then exit
* `--test`: Run only inputs, output to stdout, and exit
Expand Down
5 changes: 0 additions & 5 deletions docs/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,6 @@ parsed:
bucket = "replace_with_your_bucket_name"
```

Telegraf also supports dotenv`(.env)` file which can be passed in the `--env-file` flag. Multiple file
paths can be passed via multiple flags and Telegraf will pick those values for environment substitution.
**NOTE:** If `.env` files are passed, then Telegraf will only consider the files as a source of values.
It will not consider the values fetched via `os.Environ()`. However, the `.env` file itself can reference values defined in the environment.

## Secret-store secrets

Additional or instead of environment variables, you can use secret-stores
Expand Down
5 changes: 0 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,6 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/devigned/tab v0.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/distribution/distribution/v3 v3.0.0-20230214150026-36d8c594d7aa // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
Expand Down Expand Up @@ -347,7 +346,6 @@ require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-ieproxy v0.0.1 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-shellwords v1.0.12 // indirect
github.com/mdlayher/genetlink v1.2.0 // indirect
github.com/mdlayher/netlink v1.6.0 // indirect
github.com/mdlayher/socket v0.2.3 // indirect
Expand Down Expand Up @@ -413,9 +411,6 @@ require (
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/xdg/stringprep v1.0.3 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
github.com/yuin/gopher-lua v0.0.0-20200816102855-ee81675732da // indirect
Expand Down
7 changes: 0 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,6 @@ github.com/digitalocean/go-libvirt v0.0.0-20220811165305-15feff002086 h1:FTREXo+
github.com/digitalocean/go-libvirt v0.0.0-20220811165305-15feff002086/go.mod h1:yhKBkgJm/PWVHCFHLlFwqhIzS7FcutIYmS/fmzex5LQ=
github.com/dimchansky/utfbom v1.1.1 h1:vV6w1AhK4VMnhBno/TPVCoK9U/LP0PkLCS9tbxHdi/U=
github.com/dimchansky/utfbom v1.1.1/go.mod h1:SxdoEBH5qIqFocHMyGOXVAybYJdr71b1Q/j0mACtrfE=
github.com/distribution/distribution/v3 v3.0.0-20230214150026-36d8c594d7aa h1:L9Ay/slwQ4ERSPaurC+TVkZrM0K98GNrEEo1En3e8as=
github.com/distribution/distribution/v3 v3.0.0-20230214150026-36d8c594d7aa/go.mod h1:WHNsWjnIn2V1LYOrME7e8KxSeKunYHsxEm4am0BUtcI=
github.com/djherbis/times v1.5.0 h1:79myA211VwPhFTqUk8xehWrsEO+zcIZj0zT8mXPVARU=
github.com/djherbis/times v1.5.0/go.mod h1:5q7FDLvbNg1L/KaBmPcWlVR9NmoKo3+ucqUA3ijQhA0=
github.com/dnaeon/go-vcr v1.1.0/go.mod h1:M7tiix8f0r6mKKJ3Yq/kqU1OYf3MnfmBWVbPx/yU9ko=
Expand Down Expand Up @@ -1502,8 +1500,6 @@ github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzp
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.8/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-shellwords v1.0.12 h1:M2zGm7EW6UQJvDeQxo4T51eKPurbeFbe8WtebGE2xrk=
github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y=
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
github.com/mattn/goveralls v0.0.2/go.mod h1:8d1ZMHsd7fW6IRPKQh46F2WRpyib5/X4FOpevwGNQEw=
Expand Down Expand Up @@ -2177,11 +2173,8 @@ github.com/xdg/stringprep v0.0.0-20180714160509-73f8eece6fdc/go.mod h1:Jhud4/sHM
github.com/xdg/stringprep v1.0.0/go.mod h1:Jhud4/sHMO4oL310DaZAKk9ZaJ08SJfe+sJh0HrGL1Y=
github.com/xdg/stringprep v1.0.3 h1:cmL5Enob4W83ti/ZHuZLuKD/xqJfus4fVPwE+/BDm+4=
github.com/xdg/stringprep v1.0.3/go.mod h1:Jhud4/sHMO4oL310DaZAKk9ZaJ08SJfe+sJh0HrGL1Y=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8/go.mod h1:HUYIGzjTL3rfEspMxjDjgmT5uz5wzYJKVo23qUhYTos=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
Expand Down

0 comments on commit e6f05c4

Please sign in to comment.