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
12 changes: 12 additions & 0 deletions cmd/output/interactive_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,18 @@ func (p *interactivePrinter) printSyncResult(r SyncResult) error {
}
total := r.Synced + r.Skipped + r.Failed

if r.Connection != nil {
w("%s %s %s %s %s %s\n",
styleFaint.Render("kubeconfig:"), r.Connection.Kubeconfig,
styleFaint.Render("context:"), styleBold.Render(r.Connection.Context),
styleFaint.Render("namespace:"), styleBold.Render(r.Connection.Namespace),
)
if r.WriteTarget != "" {
w("%s %s\n", styleFaint.Render("writing: "), r.WriteTarget)
}
w("\n")
}

// Collect only clusters that need attention.
var issues []ClusterSyncResult
for _, c := range r.Clusters {
Expand Down
9 changes: 9 additions & 0 deletions cmd/output/plain_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ func (p *plainPrinter) Print(v any) error {
case SyncResult:
total := t.Synced + t.Skipped + t.Failed

if t.Connection != nil {
w("kubeconfig: %s context: %s namespace: %s\n",
t.Connection.Kubeconfig, t.Connection.Context, t.Connection.Namespace)
if t.WriteTarget != "" {
w("writing: %s\n", t.WriteTarget)
}
w("\n")
}

// List clusters that need attention first.
for _, c := range t.Clusters {
switch c.Status {
Expand Down
18 changes: 14 additions & 4 deletions cmd/output/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,20 @@ type ClusterSyncResult struct {

// SyncResult is the top-level output of the sync command.
type SyncResult struct {
Clusters []ClusterSyncResult `json:"clusters" yaml:"clusters"`
Synced int `json:"synced" yaml:"synced"`
Skipped int `json:"skipped" yaml:"skipped"`
Failed int `json:"failed" yaml:"failed"`
// Connection describes where the sync connected to (shown in text output header).
Connection *SyncConnection `json:"connection,omitempty" yaml:"connection,omitempty"`
Clusters []ClusterSyncResult `json:"clusters" yaml:"clusters"`
Synced int `json:"synced" yaml:"synced"`
Skipped int `json:"skipped" yaml:"skipped"`
Failed int `json:"failed" yaml:"failed"`
WriteTarget string `json:"writeTarget,omitempty" yaml:"writeTarget,omitempty"`
}

// SyncConnection holds the resolved Greenhouse connection details used for a sync.
type SyncConnection struct {
Kubeconfig string `json:"kubeconfig" yaml:"kubeconfig"`
Context string `json:"context" yaml:"context"`
Namespace string `json:"namespace" yaml:"namespace"`
}

// ClusterVersionResult is the output of the cluster-version command.
Expand Down
31 changes: 27 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,30 @@ func init() {
// viperKey is the viper key for the flag (e.g. "kubeconfig", "greenhouse-cluster-kubeconfig").
// When the key was not explicitly set by the user (via flag, CLOUDCTL_* env var, or config file)
// and the standard KUBECONFIG env var is set, it returns "" so that client-go uses its standard
// multi-file loading rules. An explicitly provided value is always returned as-is.
// multi-file loading rules. An explicitly provided value is always returned as-is, after tilde
// expansion (a leading "~/" is replaced with the user's home directory so that values read from
// ~/.cloudctl.yaml work without shell expansion).
func resolveKubeconfig(viperKey, flagValue string) string {
if viper.IsSet(viperKey) {
return flagValue
return expandTilde(flagValue)
}
if os.Getenv("KUBECONFIG") != "" {
return ""
}
return flagValue
return expandTilde(flagValue)
}

// expandTilde replaces a leading "~/" with the current user's home directory.
// Paths that do not start with "~/" are returned unchanged.
func expandTilde(path string) string {
if !strings.HasPrefix(path, "~/") {
return path
}
home, err := os.UserHomeDir()
if err != nil {
return path
}
return filepath.Join(home, path[2:])
}

// displayKubeconfig returns a human-readable label for the effective kubeconfig source.
Expand Down Expand Up @@ -154,7 +169,10 @@ func setupConfig() error {
// First we are trying config provided as a command line parameter. Fail if there was an error
// during reading configuration from this specified path.
viper.SetConfigFile(configFilePath)
return viper.ReadInConfig()
if err := viper.ReadInConfig(); err != nil {
return fmt.Errorf("failed to read config file %q: %w", configFilePath, err)
}
return nil
} else {
// Phase 2
// Then we are searching for ".cloudctl.yaml" in current or home directory
Expand Down Expand Up @@ -194,6 +212,11 @@ func setupConfig() error {
}
}

if err != nil {
if cf := viper.ConfigFileUsed(); cf != "" {
return fmt.Errorf("failed to read config file %q: %w", cf, err)
}
}
return err
}

Expand Down
Loading
Loading