Compare commits
4 Commits
7fca1f78dc
...
0b1241d371
| Author | SHA1 | Date | |
|---|---|---|---|
| 0b1241d371 | |||
| b300fc2f5e | |||
| f49bea4138 | |||
| 79e072fe6b |
@@ -266,6 +266,14 @@ All CLI commands support JSON output for easier parsing and integration with scr
|
|||||||
./bin/goyco --json status | jq '.status'
|
./bin/goyco --json status | jq '.status'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
You can also set JSON output as the default by setting the `CLI_JSON_OUTPUT` environment variable to `true` in your `.env` file:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
CLI_JSON_OUTPUT=true
|
||||||
|
```
|
||||||
|
|
||||||
|
When set, all CLI commands will output JSON by default. You can still override this by using the `--json` flag explicitly or by setting it to `false` in the environment variable.
|
||||||
|
|
||||||
**Note for destructive operations**: When using `--json` with `prune` commands, you must also use the `--yes` flag to skip interactive confirmation prompts:
|
**Note for destructive operations**: When using `--json` with `prune` commands, you must also use the `--yes` flag to skip interactive confirmation prompts:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ func run(args []string) error {
|
|||||||
rootFS.SetOutput(os.Stderr)
|
rootFS.SetOutput(os.Stderr)
|
||||||
rootFS.Usage = printRootUsage
|
rootFS.Usage = printRootUsage
|
||||||
showHelp := rootFS.Bool("help", false, "show this help message")
|
showHelp := rootFS.Bool("help", false, "show this help message")
|
||||||
jsonOutput := rootFS.Bool("json", false, "output results in JSON format")
|
jsonOutput := rootFS.Bool("json", cfg.CLI.JSONOutputDefault, "output results in JSON format")
|
||||||
|
|
||||||
if err := rootFS.Parse(args); err != nil {
|
if err := rootFS.Parse(args); err != nil {
|
||||||
if errors.Is(err, flag.ErrHelp) {
|
if errors.Is(err, flag.ErrHelp) {
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ type Config struct {
|
|||||||
SMTP SMTPConfig
|
SMTP SMTPConfig
|
||||||
App AppConfig
|
App AppConfig
|
||||||
RateLimit RateLimitConfig
|
RateLimit RateLimitConfig
|
||||||
|
CLI CLIConfig
|
||||||
LogDir string
|
LogDir string
|
||||||
PIDDir string
|
PIDDir string
|
||||||
}
|
}
|
||||||
@@ -81,6 +82,10 @@ type RateLimitConfig struct {
|
|||||||
TrustProxyHeaders bool
|
TrustProxyHeaders bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type CLIConfig struct {
|
||||||
|
JSONOutputDefault bool
|
||||||
|
}
|
||||||
|
|
||||||
func Load() (*Config, error) {
|
func Load() (*Config, error) {
|
||||||
config := &Config{
|
config := &Config{
|
||||||
Database: DatabaseConfig{
|
Database: DatabaseConfig{
|
||||||
@@ -137,6 +142,9 @@ func Load() (*Config, error) {
|
|||||||
MetricsLimit: getEnvAsInt("RATE_LIMIT_METRICS", 20),
|
MetricsLimit: getEnvAsInt("RATE_LIMIT_METRICS", 20),
|
||||||
TrustProxyHeaders: getEnvAsBool("RATE_LIMIT_TRUST_PROXY", false),
|
TrustProxyHeaders: getEnvAsBool("RATE_LIMIT_TRUST_PROXY", false),
|
||||||
},
|
},
|
||||||
|
CLI: CLIConfig{
|
||||||
|
JSONOutputDefault: getEnvAsBool("CLI_JSON_OUTPUT", false),
|
||||||
|
},
|
||||||
LogDir: getEnv("LOG_DIR", "/var/log/"),
|
LogDir: getEnv("LOG_DIR", "/var/log/"),
|
||||||
PIDDir: getEnv("PID_DIR", "/run"),
|
PIDDir: getEnv("PID_DIR", "/run"),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -995,3 +995,63 @@ func TestLoadWithInvalidBcryptCost(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCLIConfigJSONOutput(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
envValue string
|
||||||
|
expectedOutput bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "default false when not set",
|
||||||
|
envValue: "",
|
||||||
|
expectedOutput: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "true when set to true",
|
||||||
|
envValue: "true",
|
||||||
|
expectedOutput: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "false when set to false",
|
||||||
|
envValue: "false",
|
||||||
|
expectedOutput: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "true when set to 1",
|
||||||
|
envValue: "1",
|
||||||
|
expectedOutput: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "false when set to 0",
|
||||||
|
envValue: "0",
|
||||||
|
expectedOutput: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
t.Setenv("DB_PASSWORD", "password")
|
||||||
|
t.Setenv("SMTP_HOST", "smtp.example.com")
|
||||||
|
t.Setenv("SMTP_PORT", "2525")
|
||||||
|
t.Setenv("SMTP_FROM", "no-reply@example.com")
|
||||||
|
t.Setenv("ADMIN_EMAIL", "admin@example.com")
|
||||||
|
t.Setenv("JWT_SECRET", "this-is-a-very-secure-jwt-secret-key-that-is-long-enough")
|
||||||
|
|
||||||
|
if tt.envValue != "" {
|
||||||
|
t.Setenv("CLI_JSON_OUTPUT", tt.envValue)
|
||||||
|
} else {
|
||||||
|
os.Unsetenv("CLI_JSON_OUTPUT")
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg, err := Load()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.CLI.JSONOutputDefault != tt.expectedOutput {
|
||||||
|
t.Fatalf("expected CLI.JSONOutputDefault to be %v, got %v", tt.expectedOutput, cfg.CLI.JSONOutputDefault)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user