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
21 changes: 20 additions & 1 deletion internal/commands/config/list_profiles_internal.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
package config_internal

import (
"github.com/fatih/color"
"github.com/pingidentity/pingcli/internal/logger"
"github.com/pingidentity/pingcli/internal/output"
"github.com/pingidentity/pingcli/internal/profiles"
)

func RunInternalConfigListProfiles() {
l := logger.Get()

profileNames := profiles.GetMainConfig().ProfileNames()
activeProfile := profiles.GetMainConfig().ActiveProfile().Name()

listStr := "Profiles:\n"

// We need to enable/disable colorize before applying the color to the string below.
output.SetColorize()
activeFmt := color.New(color.Bold, color.FgGreen).SprintFunc()

for _, profileName := range profileNames {
if profileName == activeProfile {
listStr += "- " + profileName + " (active)\n"
listStr += "- " + profileName + activeFmt(" (active)") + " \n"
} else {
listStr += "- " + profileName + "\n"
}

description, err := profiles.GetMainConfig().ProfileViperValue(profileName, "description")
if err != nil {
l.Warn().Msgf("Cannot retrieve profile description for profile %s: %v", profileName, err)
continue
}

if description != "" {
listStr += " " + description + "\n"
}
}

output.Print(output.Opts{
Expand Down
7 changes: 6 additions & 1 deletion internal/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const (
ENUM_RESULT_FAILURE Result = "Failure"
)

func Print(output Opts) {
func SetColorize() {
colorizeOutput, err := profiles.GetOptionValue(options.RootColorOption)
if err != nil {
color.NoColor = false
Expand All @@ -51,6 +51,11 @@ func Print(output Opts) {
color.NoColor = !colorizeOutputBool
}
}
}

func Print(output Opts) {

SetColorize()

outputFormat, err := profiles.GetOptionValue(options.RootOutputFormatOption)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions internal/profiles/viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ func (m MainConfig) DeleteProfile(pName string) (err error) {
}

// Get all profile names from config.yaml configuration file
// Returns a sorted slice of profile names
func (m MainConfig) ProfileNames() (profileNames []string) {
keySet := make(map[string]struct{})
mainViperKeys := m.ViperInstance().AllKeys()
Expand All @@ -180,6 +181,8 @@ func (m MainConfig) ProfileNames() (profileNames []string) {
}
}

slices.Sort(profileNames)

return profileNames
}

Expand Down