-
Notifications
You must be signed in to change notification settings - Fork 289
/
env_dump.go
81 lines (64 loc) · 1.71 KB
/
env_dump.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package clicommand
import (
"context"
"encoding/json"
"fmt"
"os"
"github.com/buildkite/agent/v3/env"
"github.com/urfave/cli"
)
const envDumpHelpDescription = `Usage:
buildkite-agent env dump [options]
Description:
Prints out the environment of the current process as a JSON object, easily
parsable by other programs. Used when executing hooks to discover changes
that hooks make to the environment.
Example:
$ buildkite-agent env dump --format json-pretty`
type EnvDumpConfig struct {
Format string `cli:"format"`
// Global flags
Debug bool `cli:"debug"`
LogLevel string `cli:"log-level"`
NoColor bool `cli:"no-color"`
Experiments []string `cli:"experiment" normalize:"list"`
Profile string `cli:"profile"`
}
var EnvDumpCommand = cli.Command{
Name: "dump",
Usage: "Print the environment of the current process as a JSON object",
Description: envDumpHelpDescription,
Flags: []cli.Flag{
cli.StringFlag{
Name: "format",
Usage: "Output format; json or json-pretty",
EnvVar: "BUILDKITE_AGENT_ENV_DUMP_FORMAT",
Value: "json",
},
// Global flags
NoColorFlag,
DebugFlag,
LogLevelFlag,
ExperimentsFlag,
ProfileFlag,
},
Action: func(c *cli.Context) error {
_, cfg, _, _, done := setupLoggerAndConfig[EnvDumpConfig](context.Background(), c)
defer done()
envn := os.Environ()
envMap := make(map[string]string, len(envn))
for _, e := range envn {
if k, v, ok := env.Split(e); ok {
envMap[k] = v
}
}
enc := json.NewEncoder(c.App.Writer)
if cfg.Format == "json-pretty" {
enc.SetIndent("", " ")
}
if err := enc.Encode(envMap); err != nil {
return fmt.Errorf("error marshalling JSON: %w", err)
}
return nil
},
}