Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,6 @@ func buildRootCmd() *cobra.Command {
// Declare ALL flag variables locally within this function
var configDir string
var debug bool
var output string
var projectID string
var serviceID string
var analytics bool
Expand All @@ -429,15 +428,13 @@ func buildRootCmd() *cobra.Command {
cobra.OnInitialize(initConfigFunc)
cmd.PersistentFlags().StringVar(&configDir, "config-dir", config.GetDefaultConfigDir(), "config directory")
cmd.PersistentFlags().BoolVar(&debug, "debug", false, "enable debug logging")
cmd.PersistentFlags().VarP((*outputFlag)(&output), "output", "o", "output format (json, yaml, table)")
cmd.PersistentFlags().StringVar(&projectID, "project-id", "", "project ID")
cmd.PersistentFlags().StringVar(&serviceID, "service-id", "", "service ID")
cmd.PersistentFlags().BoolVar(&analytics, "analytics", true, "enable/disable usage analytics")
cmd.PersistentFlags().StringVar(&passwordStorage, "password-storage", config.DefaultPasswordStorage, "password storage method (keyring, pgpass, none)")

// Bind flags to viper
viper.BindPFlag("debug", cmd.PersistentFlags().Lookup("debug"))
viper.BindPFlag("output", cmd.PersistentFlags().Lookup("output"))
// ... bind remaining flags

// Add all subcommands (complete tree building)
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ These flags are available on all commands and take precedence over both environm
- `--config-dir <path>` - Path to configuration directory (default: `~/.config/tiger`)
- `--project-id <id>` - Specify project ID
- `--service-id <id>` - Specify service ID
- `-o, --output <format>` - Output format: `json`, `yaml`, or `table`
- `--analytics` - Enable/disable analytics
- `--password-storage <method>` - Password storage method: `keyring`, `pgpass`, or `none`
- `--debug` - Enable/disable debug logging
Expand Down
156 changes: 94 additions & 62 deletions internal/tiger/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"encoding/json"
"fmt"

"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"
"gopkg.in/yaml.v3"

Expand All @@ -13,7 +15,11 @@ import (
)

func buildConfigShowCmd() *cobra.Command {
return &cobra.Command{
var output string
var noDefaults bool
var withEnv bool

cmd := &cobra.Command{
Use: "show",
Short: "Show current configuration",
Long: `Display the current CLI configuration settings`,
Expand All @@ -26,16 +32,55 @@ func buildConfigShowCmd() *cobra.Command {
return fmt.Errorf("failed to load config: %w", err)
}

switch cfg.Output {
// Use flag value if provided, otherwise use config value
outputFormat := cfg.Output
if cmd.Flags().Changed("output") {
outputFormat = output
}

configFile, err := cfg.EnsureConfigDir()
if err != nil {
return err
}

// a new viper, free from env and cli flags
v := viper.New()
v.SetConfigFile(configFile)
if withEnv {
config.ApplyEnvOverrides(v)
}
if !noDefaults {
config.ApplyDefaults(v)
}
if err := config.ReadInConfig(v); err != nil {
return err
}

cfgOut, err := config.ForOutputFromViper(v)
if err != nil {
return err
}

if *cfgOut.ConfigDir == config.GetDefaultConfigDir() {
cfgOut.ConfigDir = nil
}

switch outputFormat {
case "json":
return outputJSON(cfg, cmd)
return outputJSON(cfgOut, cmd)
case "yaml":
return outputYAML(cfg, cmd)
return outputYAML(cfgOut, cmd)
default:
return outputTable(cfg, cmd)
return outputTable(cfgOut, cmd)
}
},
}

cmd.Flags().VarP((*outputFlag)(&output), "output", "o", "output format (json, yaml, table)")
cmd.Flags().BoolVar(&noDefaults, "no-defaults", false, "do not show default values for unset fields")
cmd.Flags().BoolVar(&withEnv, "with-env", false, "apply environment variable overrides")

return cmd
}

func buildConfigSetCmd() *cobra.Command {
Expand Down Expand Up @@ -132,69 +177,56 @@ func buildConfigCmd() *cobra.Command {
return cmd
}

func outputTable(cfg *config.Config, cmd *cobra.Command) error {
out := cmd.OutOrStdout()
fmt.Fprintln(out, "Current Configuration:")
fmt.Fprintf(out, " api_url: %s\n", cfg.APIURL)
fmt.Fprintf(out, " console_url: %s\n", cfg.ConsoleURL)
fmt.Fprintf(out, " gateway_url: %s\n", cfg.GatewayURL)
fmt.Fprintf(out, " docs_mcp: %t\n", cfg.DocsMCP)
fmt.Fprintf(out, " docs_mcp_url: %s\n", cfg.DocsMCPURL)
fmt.Fprintf(out, " project_id: %s\n", valueOrEmpty(cfg.ProjectID))
fmt.Fprintf(out, " service_id: %s\n", valueOrEmpty(cfg.ServiceID))
fmt.Fprintf(out, " output: %s\n", cfg.Output)
fmt.Fprintf(out, " analytics: %t\n", cfg.Analytics)
fmt.Fprintf(out, " password_storage: %s\n", cfg.PasswordStorage)
fmt.Fprintf(out, " debug: %t\n", cfg.Debug)
fmt.Fprintf(out, " config_dir: %s\n", cfg.ConfigDir)
return nil
}

func outputJSON(cfg *config.Config, cmd *cobra.Command) error {
data := map[string]interface{}{
"api_url": cfg.APIURL,
"console_url": cfg.ConsoleURL,
"gateway_url": cfg.GatewayURL,
"docs_mcp": cfg.DocsMCP,
"docs_mcp_url": cfg.DocsMCPURL,
"project_id": cfg.ProjectID,
"service_id": cfg.ServiceID,
"output": cfg.Output,
"analytics": cfg.Analytics,
"password_storage": cfg.PasswordStorage,
"debug": cfg.Debug,
"config_dir": cfg.ConfigDir,
func outputTable(cfg *config.ConfigOutput, cmd *cobra.Command) error {
table := tablewriter.NewWriter(cmd.OutOrStdout())
table.Header("PROPERTY", "VALUE")
if cfg.APIURL != nil {
table.Append("api_url", *cfg.APIURL)
}
if cfg.ConsoleURL != nil {
table.Append("console_url", *cfg.ConsoleURL)
}
if cfg.GatewayURL != nil {
table.Append("gateway_url", *cfg.GatewayURL)
}
if cfg.DocsMCP != nil {
table.Append("docs_mcp", fmt.Sprintf("%t", *cfg.DocsMCP))
}
if cfg.DocsMCPURL != nil {
table.Append("docs_mcp_url", *cfg.DocsMCPURL)
}
if cfg.ProjectID != nil {
table.Append("project_id", *cfg.ProjectID)
}
if cfg.ServiceID != nil {
table.Append("service_id", *cfg.ServiceID)
}
if cfg.Output != nil {
table.Append("output", *cfg.Output)
}
if cfg.Analytics != nil {
table.Append("analytics", fmt.Sprintf("%t", *cfg.Analytics))
}
if cfg.PasswordStorage != nil {
table.Append("password_storage", *cfg.PasswordStorage)
}
if cfg.Debug != nil {
table.Append("debug", fmt.Sprintf("%t", *cfg.Debug))
}
if cfg.ConfigDir != nil {
table.Append("config_dir", *cfg.ConfigDir)
}
return table.Render()
}

func outputJSON(cfg *config.ConfigOutput, cmd *cobra.Command) error {
encoder := json.NewEncoder(cmd.OutOrStdout())
encoder.SetIndent("", " ")
return encoder.Encode(data)
return encoder.Encode(cfg)
}

func outputYAML(cfg *config.Config, cmd *cobra.Command) error {
data := map[string]interface{}{
"api_url": cfg.APIURL,
"console_url": cfg.ConsoleURL,
"gateway_url": cfg.GatewayURL,
"docs_mcp": cfg.DocsMCP,
"docs_mcp_url": cfg.DocsMCPURL,
"project_id": cfg.ProjectID,
"service_id": cfg.ServiceID,
"output": cfg.Output,
"analytics": cfg.Analytics,
"password_storage": cfg.PasswordStorage,
"debug": cfg.Debug,
"config_dir": cfg.ConfigDir,
}

func outputYAML(cfg *config.ConfigOutput, cmd *cobra.Command) error {
encoder := yaml.NewEncoder(cmd.OutOrStdout())
defer encoder.Close()
return encoder.Encode(data)
}

func valueOrEmpty(s string) string {
if s == "" {
return "(not set)"
}
return s
return encoder.Encode(cfg)
}
115 changes: 71 additions & 44 deletions internal/tiger/cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,26 @@ password_storage: pgpass
lines := strings.Split(output, "\n")

// Check table output contains all expected key:value lines
expectedLines := []string{
" api_url: https://test.api.com/v1",
" console_url: https://console.cloud.timescale.com",
" gateway_url: https://console.cloud.timescale.com/api",
" docs_mcp: true",
" docs_mcp_url: https://mcp.tigerdata.com/docs",
" project_id: test-project",
" service_id: test-service",
" output: table",
" analytics: false",
" password_storage: pgpass",
" debug: false",
" config_dir: " + tmpDir,
}

for _, expectedLine := range expectedLines {
if !slices.Contains(lines, expectedLine) {
t.Errorf("Output should contain line '%s', got: %s", expectedLine, output)
expectedLines := map[string]string{
"api_url": "https://test.api.com/v1",
"console_url": "https://console.cloud.timescale.com",
"gateway_url": "https://console.cloud.timescale.com/api",
"docs_mcp": "true",
"docs_mcp_url": "https://mcp.tigerdata.com/docs",
"project_id": "test-project",
"service_id": "test-service",
"output": "table",
"analytics": "false",
"password_storage": "pgpass",
"debug": "false",
"config_dir": tmpDir,
}

for key, expectedLine := range expectedLines {
if !slices.ContainsFunc(lines, func(line string) bool {
return strings.Contains(line, key) && strings.Contains(line, expectedLine)
}) {
t.Errorf("Output should contain line '%s':'%s', got: %s", key, expectedLine, output)
}
}
}
Expand Down Expand Up @@ -209,26 +211,70 @@ password_storage: keyring
}
}

func TestConfigShow_EmptyValues(t *testing.T) {
func TestConfigShow_OutputValueUnaffectedByCliArg(t *testing.T) {
tmpDir, _ := setupConfigTest(t)

// Create minimal config (only defaults)
configContent := `output: table
// Create config file with table as default output
configContent := `api_url: https://test.api.com/v1
project_id: test-project
output: table
analytics: true
`
configFile := config.GetConfigFile(tmpDir)
if err := os.WriteFile(configFile, []byte(configContent), 0644); err != nil {
t.Fatalf("Failed to write config file: %v", err)
}

// Test that -o json flag overrides config file setting for output format, but not the config value itself
output, err := executeConfigCommand("config", "show", "-o", "json")
if err != nil {
t.Fatalf("Command failed: %v", err)
}

// Should be valid JSON, not table format
var result map[string]interface{}
if err := json.Unmarshal([]byte(output), &result); err != nil {
t.Fatalf("Expected JSON output but got: %v\nOutput was: %s", err, output)
}

if result["output"] != "table" {
t.Errorf("Expected output 'table' in JSON output, got %v", result["output"])
}
}

func TestConfigShow_OutputValueUnaffectedByEnvVar(t *testing.T) {
tmpDir, _ := setupConfigTest(t)

// Create config file with table as default output
configContent := `api_url: https://test.api.com/v1
project_id: test-project
output: table
analytics: true
`
configFile := config.GetConfigFile(tmpDir)
if err := os.WriteFile(configFile, []byte(configContent), 0644); err != nil {
t.Fatalf("Failed to write config file: %v", err)
}

// Test that env overrides config file setting for output format, but not the config value itself
os.Setenv("TIGER_OUTPUT", "json")
defer func() {
os.Unsetenv("TIGER_OUTPUT")
}()

output, err := executeConfigCommand("config", "show")
if err != nil {
t.Fatalf("Command failed: %v", err)
}

// Check that empty values show "(not set)"
if !strings.Contains(output, "(not set)") {
t.Error("Output should contain '(not set)' for empty values")
// Should be valid JSON, not table format
var result map[string]interface{}
if err := json.Unmarshal([]byte(output), &result); err != nil {
t.Fatalf("Expected JSON output but got: %v\nOutput was: %s", err, output)
}

if result["output"] != "table" {
t.Errorf("Expected output 'table' in JSON output, got %v", result["output"])
}
}

Expand Down Expand Up @@ -582,26 +628,6 @@ func TestConfigReset(t *testing.T) {
}
}

func TestValueOrEmpty(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"", "(not set)"},
{"value", "value"},
{"test-string", "test-string"},
}

for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
result := valueOrEmpty(tt.input)
if result != tt.expected {
t.Errorf("valueOrEmpty(%q) = %q, expected %q", tt.input, result, tt.expected)
}
})
}
}

func TestConfigCommands_Integration(t *testing.T) {
_, _ = setupConfigTest(t)

Expand Down Expand Up @@ -646,6 +672,7 @@ func TestConfigCommands_Integration(t *testing.T) {
t.Fatalf("Failed to show config after unset: %v", err)
}

result = make(map[string]any)
json.Unmarshal([]byte(showOutput), &result)
if result["project_id"] != "" {
t.Errorf("Expected empty project_id after unset, got %v", result["project_id"])
Expand Down
Loading