Compare commits

..

4 Commits

4 changed files with 77 additions and 1 deletions

View File

@@ -266,6 +266,14 @@ All CLI commands support JSON output for easier parsing and integration with scr
./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:
```bash

View File

@@ -67,7 +67,7 @@ func run(args []string) error {
rootFS.SetOutput(os.Stderr)
rootFS.Usage = printRootUsage
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 errors.Is(err, flag.ErrHelp) {

View File

@@ -15,6 +15,7 @@ type Config struct {
SMTP SMTPConfig
App AppConfig
RateLimit RateLimitConfig
CLI CLIConfig
LogDir string
PIDDir string
}
@@ -81,6 +82,10 @@ type RateLimitConfig struct {
TrustProxyHeaders bool
}
type CLIConfig struct {
JSONOutputDefault bool
}
func Load() (*Config, error) {
config := &Config{
Database: DatabaseConfig{
@@ -137,6 +142,9 @@ func Load() (*Config, error) {
MetricsLimit: getEnvAsInt("RATE_LIMIT_METRICS", 20),
TrustProxyHeaders: getEnvAsBool("RATE_LIMIT_TRUST_PROXY", false),
},
CLI: CLIConfig{
JSONOutputDefault: getEnvAsBool("CLI_JSON_OUTPUT", false),
},
LogDir: getEnv("LOG_DIR", "/var/log/"),
PIDDir: getEnv("PID_DIR", "/run"),
}

View File

@@ -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)
}
})
}
}