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
75 changes: 13 additions & 62 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,7 @@ func initViperProfile() {

cfgFile, err := profiles.GetOptionValue(options.RootConfigOption)
if err != nil {
output.Print(output.Opts{
Message: "Failed to get configuration file location",
Result: output.ENUM_RESULT_FAILURE,
FatalMessage: err.Error(),
})
output.SystemError(fmt.Sprintf("Failed to get configuration file location: %v", err), nil)
}

l.Debug().Msgf("Using configuration file location for initialization: %s", cfgFile)
Expand All @@ -82,19 +78,11 @@ func initViperProfile() {

userDefinedProfile, err := profiles.GetOptionValue(options.RootProfileOption)
if err != nil {
output.Print(output.Opts{
Message: "Failed to get user-defined profile",
Result: output.ENUM_RESULT_FAILURE,
FatalMessage: err.Error(),
})
output.SystemError(fmt.Sprintf("Failed to get user-defined profile: %v", err), nil)
}
configFileActiveProfile, err := profiles.GetOptionValue(options.RootActiveProfileOption)
if err != nil {
output.Print(output.Opts{
Message: "Failed to get active profile",
Result: output.ENUM_RESULT_FAILURE,
FatalMessage: err.Error(),
})
output.SystemError(fmt.Sprintf("Failed to get active profile from configuration file: %v", err), nil)
}

if userDefinedProfile != "" {
Expand All @@ -105,20 +93,12 @@ func initViperProfile() {

// Configure the profile viper instance
if err := profiles.GetMainConfig().ChangeActiveProfile(configFileActiveProfile); err != nil {
output.Print(output.Opts{
Message: "Failed to set active profile viper",
Result: output.ENUM_RESULT_FAILURE,
FatalMessage: err.Error(),
})
output.UserFatal(fmt.Sprintf("Failed to set active profile: %v", err), nil)
}

// Validate the configuration
if err := profiles.Validate(); err != nil {
output.Print(output.Opts{
Message: "Failed to validate Ping CLI configuration",
Result: output.ENUM_RESULT_FAILURE,
FatalMessage: err.Error(),
})
output.UserFatal(fmt.Sprintf("Failed to validate Ping CLI configuration: %v", err), nil)
}
}

Expand All @@ -128,43 +108,25 @@ func checkCfgFileLocation(cfgFile string) {
if os.IsNotExist(err) {
// Only create a new configuration file if it is the default configuration file location
if cfgFile == options.RootConfigOption.DefaultValue.String() {
output.Print(output.Opts{
Message: fmt.Sprintf("Ping CLI configuration file '%s' does not exist.", cfgFile),
Result: output.ENUM_RESULT_NOACTION_WARN,
})
output.Warn(fmt.Sprintf("Ping CLI configuration file '%s' does not exist.", cfgFile), nil)

createConfigFile(options.RootConfigOption.DefaultValue.String())
} else {
output.Print(output.Opts{
Message: fmt.Sprintf("Configuration file '%s' does not exist.", cfgFile),
Result: output.ENUM_RESULT_FAILURE,
FatalMessage: fmt.Sprintf("Configuration file '%s' does not exist. Use the default configuration file location or specify a valid configuration file location with the --config flag.", cfgFile),
})
output.UserFatal(fmt.Sprintf("Configuration file '%s' does not exist. Use the default configuration file location or specify a valid configuration file location with the --config flag.", cfgFile), nil)
}
} else if err != nil {
output.Print(output.Opts{
Message: fmt.Sprintf("Failed to check if configuration file '%s' exists", cfgFile),
Result: output.ENUM_RESULT_FAILURE,
FatalMessage: err.Error(),
})
output.SystemError(fmt.Sprintf("Failed to check if configuration file '%s' exists: %v", cfgFile, err), nil)
}

}

func createConfigFile(cfgFile string) {
output.Print(output.Opts{
Message: fmt.Sprintf("Creating new Ping CLI configuration file at: %s", cfgFile),
Result: output.ENUM_RESULT_NIL,
})
output.Message(fmt.Sprintf("Creating new Ping CLI configuration file at: %s", cfgFile), nil)

// MkdirAll does nothing if directories already exist. Create needed directories for config file location.
err := os.MkdirAll(filepath.Dir(cfgFile), os.ModePerm)
if err != nil {
output.Print(output.Opts{
Message: fmt.Sprintf("Failed to make directories needed for new Ping CLI configuration file: %s", cfgFile),
Result: output.ENUM_RESULT_FAILURE,
FatalMessage: err.Error(),
})
output.SystemError(fmt.Sprintf("Failed to make the directory for the new configuration file '%s': %v", cfgFile, err), nil)
}

tempViper := viper.New()
Expand All @@ -173,11 +135,7 @@ func createConfigFile(cfgFile string) {

err = tempViper.WriteConfigAs(cfgFile)
if err != nil {
output.Print(output.Opts{
Message: fmt.Sprintf("Failed to create configuration file at: %s", cfgFile),
Result: output.ENUM_RESULT_FAILURE,
FatalMessage: err.Error(),
})
output.SystemError(fmt.Sprintf("Failed to create configuration file '%s': %v", cfgFile, err), nil)
}
}

Expand Down Expand Up @@ -206,10 +164,7 @@ func initMainViper(cfgFile string) {
}
err := profiles.GetMainConfig().SaveProfile(pName, subViper)
if err != nil {
output.Print(output.Opts{
Message: fmt.Sprintf("Error: Failed to save profile %s.", pName),
Result: output.ENUM_RESULT_FAILURE,
})
output.SystemError(fmt.Sprintf("Failed to save profile '%s': %v", pName, err), nil)
}
}
}
Expand All @@ -223,11 +178,7 @@ func loadMainViperConfig(cfgFile string) {

// If a config file is found, read it in.
if err := mainViper.ReadInConfig(); err != nil {
output.Print(output.Opts{
Message: fmt.Sprintf("Failed to read configuration from file: %s", cfgFile),
Result: output.ENUM_RESULT_FAILURE,
FatalMessage: err.Error(),
})
output.SystemError(fmt.Sprintf("Failed to read configuration from file '%s': %v", cfgFile, err), nil)
} else {
l.Info().Msgf("Using configuration file: %s", mainViper.ConfigFileUsed())
}
Expand Down
15 changes: 3 additions & 12 deletions internal/commands/config/add_profile_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ func RunInternalConfigAddProfile(rc io.ReadCloser) (err error) {
return fmt.Errorf("failed to add profile: %v", err)
}

output.Print(output.Opts{
Message: fmt.Sprintf("Adding new profile '%s'...", newProfileName),
Result: output.ENUM_RESULT_NIL,
})
output.Message(fmt.Sprintf("Adding new profile '%s'...", newProfileName), nil)

subViper := viper.New()
subViper.Set(options.ProfileDescriptionOption.ViperKey, newDescription)
Expand All @@ -35,20 +32,14 @@ func RunInternalConfigAddProfile(rc io.ReadCloser) (err error) {
return fmt.Errorf("failed to add profile: %v", err)
}

output.Print(output.Opts{
Message: fmt.Sprintf("Profile created. Update additional profile attributes via 'pingcli config set' or directly within the config file at '%s'", profiles.GetMainConfig().ViperInstance().ConfigFileUsed()),
Result: output.ENUM_RESULT_SUCCESS,
})
output.Success(fmt.Sprintf("Profile created. Update additional profile attributes via 'pingcli config set' or directly within the config file at '%s'", profiles.GetMainConfig().ViperInstance().ConfigFileUsed()), nil)

if setActive {
if err = profiles.GetMainConfig().ChangeActiveProfile(newProfileName); err != nil {
return fmt.Errorf("failed to set active profile: %v", err)
}

output.Print(output.Opts{
Message: fmt.Sprintf("Profile '%s' set as active.", newProfileName),
Result: output.ENUM_RESULT_SUCCESS,
})
output.Success(fmt.Sprintf("Profile '%s' set as active.", newProfileName), nil)
}

return nil
Expand Down
15 changes: 3 additions & 12 deletions internal/commands/config/delete_profile_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ func RunInternalConfigDeleteProfile(args []string, rc io.ReadCloser) (err error)
}

if !confirmed {
output.Print(output.Opts{
Message: "Profile deletion cancelled.",
Result: output.ENUM_RESULT_NIL,
})
output.Message("Profile deletion cancelled.", nil)

return nil
}
Expand Down Expand Up @@ -70,19 +67,13 @@ func promptUserToConfirmDelete(pName string, rc io.ReadCloser) (confirmed bool,
}

func deleteProfile(pName string) (err error) {
output.Print(output.Opts{
Message: fmt.Sprintf("Deleting profile '%s'...", pName),
Result: output.ENUM_RESULT_NIL,
})
output.Message(fmt.Sprintf("Deleting profile '%s'...", pName), nil)

if err = profiles.GetMainConfig().DeleteProfile(pName); err != nil {
return err
}

output.Print(output.Opts{
Message: fmt.Sprintf("Profile '%s' deleted.", pName),
Result: output.ENUM_RESULT_SUCCESS,
})
output.Success(fmt.Sprintf("Profile '%s' deleted.", pName), nil)

return nil
}
5 changes: 1 addition & 4 deletions internal/commands/config/get_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ func RunInternalConfigGet(viperKey string) (err error) {
return fmt.Errorf("failed to get configuration: %v", err)
}

output.Print(output.Opts{
Message: yamlStr,
Result: output.ENUM_RESULT_NIL,
})
output.Message(yamlStr, nil)

return nil
}
Expand Down
7 changes: 2 additions & 5 deletions internal/commands/config/list_profiles_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,9 @@ func RunInternalConfigListProfiles() {
}

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

output.Print(output.Opts{
Message: listStr,
Result: output.ENUM_RESULT_NIL,
})
output.Message(listStr, nil)
}
10 changes: 2 additions & 8 deletions internal/commands/config/set_active_profile_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,13 @@ func RunInternalConfigSetActiveProfile(args []string, rc io.ReadCloser) (err err
}
}

output.Print(output.Opts{
Message: fmt.Sprintf("Setting active profile to '%s'...", pName),
Result: output.ENUM_RESULT_NIL,
})
output.Message(fmt.Sprintf("Setting active profile to '%s'...", pName), nil)

if err = profiles.GetMainConfig().ChangeActiveProfile(pName); err != nil {
return fmt.Errorf("failed to set active profile: %v", err)
}

output.Print(output.Opts{
Message: fmt.Sprintf("Active profile set to '%s'", pName),
Result: output.ENUM_RESULT_SUCCESS,
})
output.Success(fmt.Sprintf("Active profile set to '%s'", pName), nil)

return nil
}
Expand Down
11 changes: 2 additions & 9 deletions internal/commands/config/set_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,8 @@ func RunInternalConfigSet(kvPair string) (err error) {
return fmt.Errorf("failed to set configuration: %v", err)
}

output.Print(output.Opts{
Message: "Configuration set successfully",
Result: output.ENUM_RESULT_SUCCESS,
})

output.Print(output.Opts{
Message: yamlStr,
Result: output.ENUM_RESULT_NIL,
})
output.Success("Configuration set successfully", nil)
output.Message(yamlStr, nil)

return nil
}
Expand Down
11 changes: 2 additions & 9 deletions internal/commands/config/unset_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,8 @@ func RunInternalConfigUnset(viperKey string) (err error) {
return fmt.Errorf("failed to unset configuration: %v", err)
}

output.Print(output.Opts{
Message: "Configuration unset successfully",
Result: output.ENUM_RESULT_SUCCESS,
})

output.Print(output.Opts{
Message: yamlStr,
Result: output.ENUM_RESULT_NIL,
})
output.Success("Configuration unset successfully", nil)
output.Message(yamlStr, nil)

return nil
}
Expand Down
5 changes: 1 addition & 4 deletions internal/commands/config/view_profile_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ func RunInternalConfigViewProfile(args []string) (err error) {

profileStr = fmt.Sprintf("Profile: %s\n\n%s", pName, profileStr)

output.Print(output.Opts{
Message: profileStr,
Result: output.ENUM_RESULT_NIL,
})
output.Message(profileStr, nil)

return nil
}
5 changes: 1 addition & 4 deletions internal/commands/feedback/feedback_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,5 @@ Open an issue on the project's GitHub repository's issue tracker:

// Print the feedback message
func PrintFeedbackMessage() {
output.Print(output.Opts{
Message: FeedbackMessage,
Result: output.ENUM_RESULT_NIL,
})
output.Message(FeedbackMessage, nil)
}
25 changes: 5 additions & 20 deletions internal/commands/platform/export_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,7 @@ func RunInternalExport(ctx context.Context, commandVersion string) (err error) {
return err
}

output.Print(output.Opts{
Message: fmt.Sprintf("Export to directory '%s' complete.", outputDir),
Result: output.ENUM_RESULT_SUCCESS,
})
output.Success(fmt.Sprintf("Export to directory '%s' complete.", outputDir), nil)

return nil
}
Expand Down Expand Up @@ -366,20 +363,14 @@ func createOrValidateOutputDir(outputDir string, overwriteExport bool) (err erro
l.Debug().Msgf("Validating export output directory '%s'", outputDir)
_, err = os.Stat(outputDir)
if err != nil {
output.Print(output.Opts{
Message: fmt.Sprintf("failed to find 'platform export' output directory. creating new output directory at filepath '%s'", outputDir),
Result: output.ENUM_RESULT_NOACTION_WARN,
})
output.Warn(fmt.Sprintf("failed to find 'platform export' output directory. creating new output directory at filepath '%s'", outputDir), nil)

err = os.MkdirAll(outputDir, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create 'platform export' output directory '%s': %s", outputDir, err.Error())
}

output.Print(output.Opts{
Message: fmt.Sprintf("new 'platform export' output directory '%s' created", outputDir),
Result: output.ENUM_RESULT_SUCCESS,
})
output.Success(fmt.Sprintf("new 'platform export' output directory '%s' created", outputDir), nil)
} else {
// Check if the output directory is empty
// If not, default behavior is to exit and not overwrite.
Expand Down Expand Up @@ -414,10 +405,7 @@ func getPingOneExportEnvID() (err error) {
return fmt.Errorf("failed to determine pingone export environment ID.")
}

output.Print(output.Opts{
Message: "No target PingOne export environment ID specified. Defaulting export environment ID to the Worker App environment ID.",
Result: output.ENUM_RESULT_NOACTION_WARN,
})
output.Warn("No target PingOne export environment ID specified. Defaulting export environment ID to the Worker App environment ID.", nil)
}

return nil
Expand Down Expand Up @@ -483,10 +471,7 @@ func exportConnectors(exportableConnectors *[]connector.Exportable, exportFormat

// Loop through user defined exportable connectors and export them
for _, connector := range *exportableConnectors {
output.Print(output.Opts{
Message: fmt.Sprintf("Exporting %s service...", connector.ConnectorServiceName()),
Result: output.ENUM_RESULT_NIL,
})
output.Message(fmt.Sprintf("Exporting %s service...", connector.ConnectorServiceName()), nil)

err := connector.Export(exportFormat, outputDir, overwriteExport)
if err != nil {
Expand Down
Loading