diff --git a/cmd/config.go b/cmd/config.go index 75fb4660..b0c43c89 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -82,7 +82,8 @@ var configDefaultCmd = &cobra.Command{ "default-lagoon": lagoonConfig.Lagoon, }, } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) }, } @@ -129,10 +130,11 @@ var configLagoonsCmd = &cobra.Command{ tableHeader = append(tableHeader, "Kibana-URL") } tableHeader = append(tableHeader, "SSH-Key") - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: tableHeader, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -176,7 +178,7 @@ var configAddCmd = &cobra.Command{ if err := writeLagoonConfig(&lagoonCLIConfig, filepath.Join(configFilePath, configName+configExtension)); err != nil { return fmt.Errorf("couldn't write config: %v", err) } - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: []string{ "Name", "GraphQL", @@ -198,6 +200,7 @@ var configAddCmd = &cobra.Command{ }, }, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } else { return fmt.Errorf("must have Hostname, Port, and GraphQL endpoint") } diff --git a/cmd/deploy.go b/cmd/deploy.go index 8d038b8c..c3b99d5b 100644 --- a/cmd/deploy.go +++ b/cmd/deploy.go @@ -3,6 +3,7 @@ package cmd import ( "context" "fmt" + "github.com/uselagoon/lagoon-cli/pkg/output" "strconv" lclient "github.com/uselagoon/machinery/api/lagoon/client" @@ -80,7 +81,9 @@ use 'lagoon deploy latest' instead`, if err != nil { return err } - fmt.Println(result.DeployEnvironmentBranch) + resultData := output.Result{Result: result.DeployEnvironmentBranch} + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -143,7 +146,9 @@ var deployPromoteCmd = &cobra.Command{ if err != nil { return err } - fmt.Println(result.DeployEnvironmentPromote) + resultData := output.Result{Result: result.DeployEnvironmentPromote} + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -204,7 +209,9 @@ This environment should already exist in lagoon. It is analogous with the 'Deplo if err != nil { return err } - fmt.Println(result.DeployEnvironmentLatest) + resultData := output.Result{Result: result.DeployEnvironmentLatest} + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -291,7 +298,9 @@ This pullrequest may not already exist as an environment in lagoon.`, if err != nil { return err } - fmt.Println(result.DeployEnvironmentPullrequest) + resultData := output.Result{Result: result.DeployEnvironmentPullrequest} + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, diff --git a/cmd/deploytarget.go b/cmd/deploytarget.go index 66bcc259..6fe0704f 100644 --- a/cmd/deploytarget.go +++ b/cmd/deploytarget.go @@ -120,7 +120,7 @@ var addDeployTargetCmd = &cobra.Command{ returnNonEmptyString(fmt.Sprintf("%v", addDeployTargetResponse.Created)), returnNonEmptyString(fmt.Sprintf("%v", addDeployTargetResponse.MonitoringConfig)), }) - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: []string{ "ID", "Name", @@ -138,6 +138,7 @@ var addDeployTargetCmd = &cobra.Command{ }, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -252,7 +253,7 @@ var updateDeployTargetCmd = &cobra.Command{ returnNonEmptyString(fmt.Sprintf("%v", updateDeployTargetResponse.Created)), returnNonEmptyString(fmt.Sprintf("%v", updateDeployTargetResponse.MonitoringConfig)), }) - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: []string{ "ID", "Name", @@ -270,6 +271,7 @@ var updateDeployTargetCmd = &cobra.Command{ }, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -319,7 +321,8 @@ var deleteDeployTargetCmd = &cobra.Command{ resultData := output.Result{ Result: deleteDeployTargetResponse.DeleteDeployTarget, } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -383,7 +386,8 @@ var addDeployTargetToOrganizationCmd = &cobra.Command{ "Organization Name": deployTargetResponse.Name, }, } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -447,7 +451,8 @@ var removeDeployTargetFromOrganizationCmd = &cobra.Command{ "Organization Name": organizationName, }, } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, diff --git a/cmd/deploytargetconfig.go b/cmd/deploytargetconfig.go index cb4465f3..84864777 100644 --- a/cmd/deploytargetconfig.go +++ b/cmd/deploytargetconfig.go @@ -90,7 +90,7 @@ var addDeployTargetConfigCmd = &cobra.Command{ returnNonEmptyString(fmt.Sprintf("%v", deployTargetConfig.DeployTarget.CloudProvider)), returnNonEmptyString(fmt.Sprintf("%v", deployTargetConfig.DeployTarget.CloudRegion)), }) - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: []string{ "ID", "Weight", @@ -103,6 +103,7 @@ var addDeployTargetConfigCmd = &cobra.Command{ }, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -184,7 +185,7 @@ var updateDeployTargetConfigCmd = &cobra.Command{ returnNonEmptyString(fmt.Sprintf("%v", deployTargetConfig.DeployTarget.CloudProvider)), returnNonEmptyString(fmt.Sprintf("%v", deployTargetConfig.DeployTarget.CloudRegion)), }) - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: []string{ "ID", "Weight", @@ -197,6 +198,7 @@ var updateDeployTargetConfigCmd = &cobra.Command{ }, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -247,7 +249,11 @@ var deleteDeployTargetConfigCmd = &cobra.Command{ if err != nil { return err } - fmt.Println(result.DeleteDeployTargetConfig) + resultData := output.Result{ + Result: result.DeleteDeployTargetConfig, + } + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -306,7 +312,7 @@ var listDeployTargetConfigsCmd = &cobra.Command{ returnNonEmptyString(fmt.Sprintf("%v", deployTargetConfig.DeployTarget.CloudRegion)), }) } - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: []string{ "ID", "Weight", @@ -319,6 +325,7 @@ var listDeployTargetConfigsCmd = &cobra.Command{ }, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } diff --git a/cmd/environment.go b/cmd/environment.go index 56aca2d6..f77a06fc 100644 --- a/cmd/environment.go +++ b/cmd/environment.go @@ -50,7 +50,8 @@ var deleteEnvCmd = &cobra.Command{ resultData := output.Result{ Result: environment.DeleteEnvironment, } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -172,7 +173,8 @@ var updateEnvironmentCmd = &cobra.Command{ "Environment Name": result.Name, }, } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -233,7 +235,7 @@ var listBackupsCmd = &cobra.Command{ returnNonEmptyString(fmt.Sprintf("%v", backup.Restore.Status)), }) } - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: []string{ "BackupID", "Source", @@ -243,6 +245,7 @@ var listBackupsCmd = &cobra.Command{ }, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -290,7 +293,9 @@ This returns a direct URL to the backup, this is a signed download link with a l for _, backup := range backupsResult.Backups { if backup.BackupID == backupID { if backup.Restore.RestoreLocation != "" { - fmt.Println(backup.Restore.RestoreLocation) + resultData := output.Result{Result: backup.Restore.RestoreLocation} + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil } status = backup.Restore.Status diff --git a/cmd/get.go b/cmd/get.go index 5d5ca03d..33126dc3 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -64,7 +64,8 @@ var getProjectCmd = &cobra.Command{ if project.Name == "" { outputOptions.Error = fmt.Sprintf("No details for project '%s'\n", cmdProjectName) - output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions) + r := output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil } @@ -126,7 +127,8 @@ var getProjectCmd = &cobra.Command{ Header: projHeader, Data: data, } - output.RenderOutput(dataMain, outputOptions) + r := output.RenderOutput(dataMain, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -177,7 +179,8 @@ This returns information about a deployment, the logs of this build can also be }, }, } - output.RenderOutput(dataMain, outputOptions) + r := output.RenderOutput(dataMain, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil } dataMain := output.Table{ @@ -202,7 +205,8 @@ This returns information about a deployment, the logs of this build can also be }, }, } - output.RenderOutput(dataMain, outputOptions) + r := output.RenderOutput(dataMain, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -278,7 +282,8 @@ var getEnvironmentCmd = &cobra.Command{ Header: envHeader, Data: data, } - output.RenderOutput(dataMain, outputOptions) + r := output.RenderOutput(dataMain, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -328,14 +333,16 @@ var getProjectKeyCmd = &cobra.Command{ if len(dataMain.Data) == 0 { outputOptions.Error = fmt.Sprintf("No project-key for project '%s'", cmdProjectName) - output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions) + r := output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil } if projectKey.PrivateKey != "" { dataMain.Header = append(dataMain.Header, "PrivateKey") } - output.RenderOutput(dataMain, outputOptions) + r := output.RenderOutput(dataMain, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -405,7 +412,8 @@ var getOrganizationCmd = &cobra.Command{ Data: data, } - output.RenderOutput(dataMain, outputOptions) + r := output.RenderOutput(dataMain, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } diff --git a/cmd/groups.go b/cmd/groups.go index b1ac7c39..a337a9cc 100644 --- a/cmd/groups.go +++ b/cmd/groups.go @@ -89,7 +89,8 @@ var addGroupCmd = &cobra.Command{ if organizationName != "" { resultData.ResultData["Organization"] = organizationName } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -162,7 +163,8 @@ var addUserToGroupCmd = &cobra.Command{ resultData := output.Result{ Result: "success", } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -224,7 +226,8 @@ var addProjectToGroupCmd = &cobra.Command{ resultData := output.Result{ Result: "success", } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -280,7 +283,8 @@ var deleteUserFromGroupCmd = &cobra.Command{ "id": result.ID, }, } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -345,7 +349,8 @@ var deleteProjectFromGroupCmd = &cobra.Command{ resultData := output.Result{ Result: "success", } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -384,7 +389,8 @@ var deleteGroupCmd = &cobra.Command{ resultData := output.Result{ Result: "success", } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, diff --git a/cmd/list.go b/cmd/list.go index 9483e882..d08bc247 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -82,8 +82,8 @@ var listProjectsCmd = &cobra.Command{ Header: []string{"ID", "ProjectName", "GitUrl", "ProductionEnvironment", "ProductionRoute", "DevEnvironments"}, Data: data, } - - output.RenderOutput(dataMain, outputOptions) + r := output.RenderOutput(dataMain, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -132,7 +132,7 @@ var listDeployTargetsCmd = &cobra.Command{ }) } outputOptions.MultiLine = true - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: []string{ "ID", "Name", @@ -150,6 +150,7 @@ var listDeployTargetsCmd = &cobra.Command{ }, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -194,7 +195,8 @@ var listGroupsCmd = &cobra.Command{ Header: []string{"ID", "Name"}, Data: data, } - output.RenderOutput(dataMain, outputOptions) + r := output.RenderOutput(dataMain, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -268,7 +270,8 @@ var listGroupProjectsCmd = &cobra.Command{ } else { outputOptions.Error = "There are no projects in any groups\n" } - output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions) + r := output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil } @@ -279,7 +282,8 @@ var listGroupProjectsCmd = &cobra.Command{ if listAllProjects { dataMain.Header = append(dataMain.Header, "GroupName") } - output.RenderOutput(dataMain, outputOptions) + r := output.RenderOutput(dataMain, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -315,7 +319,8 @@ var listEnvironmentsCmd = &cobra.Command{ if len(*environments) == 0 { outputOptions.Error = fmt.Sprintf("No environments found for project '%s'\n", cmdProjectName) - output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions) + r := output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil } @@ -339,7 +344,8 @@ var listEnvironmentsCmd = &cobra.Command{ Header: []string{"ID", "Name", "DeployType", "EnvironmentType", "Namespace", "Route", "DeployTarget"}, Data: data, } - output.RenderOutput(dataMain, outputOptions) + r := output.RenderOutput(dataMain, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -414,13 +420,15 @@ var listVariablesCmd = &cobra.Command{ } else { outputOptions.Error = fmt.Sprintf("There are no variables for project '%s'\n", cmdProjectName) } - output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions) + r := output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil } - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: header, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -475,14 +483,16 @@ var listDeploymentsCmd = &cobra.Command{ if len(data) == 0 { outputOptions.Error = fmt.Sprintf("There are no deployments for environment '%s' in project '%s'\n", cmdProjectEnvironment, cmdProjectName) - output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions) + r := output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil } dataMain := output.Table{ Header: []string{"ID", "RemoteID", "Name", "Status", "Created", "Started", "Completed"}, Data: data, } - output.RenderOutput(dataMain, outputOptions) + r := output.RenderOutput(dataMain, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -538,14 +548,16 @@ var listTasksCmd = &cobra.Command{ if len(data) == 0 { outputOptions.Error = fmt.Sprintf("There are no tasks for environment '%s' in project '%s'\n", cmdProjectEnvironment, cmdProjectName) - output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions) + r := output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil } dataMain := output.Table{ Header: []string{"ID", "RemoteID", "Name", "Status", "Created", "Started", "Completed", "Service"}, Data: data, } - output.RenderOutput(dataMain, outputOptions) + r := output.RenderOutput(dataMain, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -613,7 +625,8 @@ Without a group name, this query may time out in large Lagoon instalschema.`, Header: []string{"ID", "GroupName", "Email", "Role"}, Data: data, } - output.RenderOutput(dataMain, outputOptions) + r := output.RenderOutput(dataMain, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -664,7 +677,8 @@ This query can take a long time to run if there are a lot of users.`, Header: []string{"ID", "Email", "FirstName", "LastName", "Comment"}, Data: data, } - output.RenderOutput(dataMain, outputOptions) + r := output.RenderOutput(dataMain, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -714,7 +728,8 @@ var listUsersGroupsCmd = &cobra.Command{ Header: []string{"ID", "Email", "GroupName", "GroupRole"}, Data: data, } - output.RenderOutput(dataMain, outputOptions) + r := output.RenderOutput(dataMain, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -764,14 +779,16 @@ var listInvokableTasks = &cobra.Command{ if len(data) == 0 { outputOptions.Error = "There are no user defined tasks for this environment\n" - output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions) + r := output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil } dataMain := output.Table{ Header: []string{"Task Name", "Description"}, Data: data, } - output.RenderOutput(dataMain, outputOptions) + r := output.RenderOutput(dataMain, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -816,7 +833,8 @@ var listProjectGroupsCmd = &cobra.Command{ if len(projectGroups.Groups) == 0 { outputOptions.Error = fmt.Sprintf("There are no groups for project '%s'\n", cmdProjectName) - output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions) + r := output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil } @@ -836,7 +854,8 @@ var listProjectGroupsCmd = &cobra.Command{ Header: []string{"Group ID", "Group Name", "Organization"}, Data: data, } - output.RenderOutput(dataMain, outputOptions) + r := output.RenderOutput(dataMain, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -884,7 +903,8 @@ var listOrganizationProjectsCmd = &cobra.Command{ if len(*orgProjects) == 0 { outputOptions.Error = fmt.Sprintf("No associated projects found for organization '%s'\n", organizationName) - output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions) + r := output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil } @@ -900,7 +920,8 @@ var listOrganizationProjectsCmd = &cobra.Command{ Header: []string{"ID", "Name", "Group Count"}, Data: data, } - output.RenderOutput(dataMain, outputOptions) + r := output.RenderOutput(dataMain, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -947,7 +968,8 @@ var listOrganizationGroupsCmd = &cobra.Command{ } if len(*orgGroups) == 0 { outputOptions.Error = fmt.Sprintf("No associated groups found for organization '%s'\n", organizationName) - output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions) + r := output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil } @@ -964,7 +986,8 @@ var listOrganizationGroupsCmd = &cobra.Command{ Header: []string{"ID", "Name", "Type", "Member Count"}, Data: data, } - output.RenderOutput(dataMain, outputOptions) + r := output.RenderOutput(dataMain, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -1007,7 +1030,8 @@ var listOrganizationDeployTargetsCmd = &cobra.Command{ } if len(*deployTargets) == 0 { outputOptions.Error = fmt.Sprintf("No associated deploy targets found for organization '%s'\n", organizationName) - output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions) + r := output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil } @@ -1027,7 +1051,8 @@ var listOrganizationDeployTargetsCmd = &cobra.Command{ Header: []string{"ID", "Name", "Router Pattern", "Cloud Region", "Cloud Provider", "SSH Host", "SSH Port"}, Data: data, } - output.RenderOutput(dataMain, outputOptions) + r := output.RenderOutput(dataMain, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -1079,7 +1104,8 @@ var ListOrganizationUsersCmd = &cobra.Command{ Header: []string{"ID", "Email", "First Name", "LastName", "Comment"}, Data: data, } - output.RenderOutput(dataMain, outputOptions) + r := output.RenderOutput(dataMain, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -1135,7 +1161,8 @@ var ListOrganizationAdminsCmd = &cobra.Command{ Header: []string{"ID", "Email", "First Name", "LastName", "OrganizationRole"}, Data: data, } - output.RenderOutput(dataMain, outputOptions) + r := output.RenderOutput(dataMain, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -1184,7 +1211,8 @@ var listOrganizationsCmd = &cobra.Command{ Header: []string{"ID", "Name", "Description", "Project Quota", "Group Quota", "Notification Quota", "Environment Quota", "Route Quota"}, Data: data, } - output.RenderOutput(dataMain, outputOptions) + r := output.RenderOutput(dataMain, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } diff --git a/cmd/notificationsemail.go b/cmd/notificationsemail.go index bdd62a1a..13d037ac 100644 --- a/cmd/notificationsemail.go +++ b/cmd/notificationsemail.go @@ -78,7 +78,7 @@ It does not configure a project to send notifications to email though, you need notificationData = append(notificationData, "-") } data = append(data, notificationData) - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: []string{ "ID", "Name", @@ -87,6 +87,7 @@ It does not configure a project to send notifications to email though, you need }, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -135,7 +136,8 @@ This command is used to add an existing email notification in Lagoon to a projec resultData := output.Result{ Result: "success", } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -185,13 +187,14 @@ var listProjectEmailsCmd = &cobra.Command{ }) } } - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: []string{ "Name", "EmailAddress", }, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -233,7 +236,7 @@ var listAllEmailsCmd = &cobra.Command{ } } } - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: []string{ "Project", "Name", @@ -241,6 +244,7 @@ var listAllEmailsCmd = &cobra.Command{ }, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -285,7 +289,8 @@ var deleteProjectEmailNotificationCmd = &cobra.Command{ resultData := output.Result{ Result: "success", } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -326,7 +331,8 @@ var deleteEmailNotificationCmd = &cobra.Command{ resultData := output.Result{ Result: result.DeleteNotification, } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -392,7 +398,7 @@ var updateEmailNotificationCmd = &cobra.Command{ returnNonEmptyString(fmt.Sprintf("%v", result.EmailAddress)), }, } - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: []string{ "ID", "Name", @@ -400,6 +406,7 @@ var updateEmailNotificationCmd = &cobra.Command{ }, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, diff --git a/cmd/notificationsrocketchat.go b/cmd/notificationsrocketchat.go index f1130028..5d507802 100644 --- a/cmd/notificationsrocketchat.go +++ b/cmd/notificationsrocketchat.go @@ -85,7 +85,7 @@ It does not configure a project to send notifications to RocketChat though, you notificationData = append(notificationData, "-") } data = append(data, notificationData) - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: []string{ "ID", "Name", @@ -95,6 +95,7 @@ It does not configure a project to send notifications to RocketChat though, you }, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -144,7 +145,8 @@ This command is used to add an existing RocketChat notification in Lagoon to a p resultData := output.Result{ Result: "success", } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -195,7 +197,7 @@ var listProjectRocketChatsCmd = &cobra.Command{ }) } } - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: []string{ "Name", "Webhook", @@ -203,6 +205,7 @@ var listProjectRocketChatsCmd = &cobra.Command{ }, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -245,7 +248,7 @@ var listAllRocketChatsCmd = &cobra.Command{ } } } - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: []string{ "Project", "Name", @@ -254,6 +257,7 @@ var listAllRocketChatsCmd = &cobra.Command{ }, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -298,7 +302,8 @@ var deleteProjectRocketChatNotificationCmd = &cobra.Command{ resultData := output.Result{ Result: "success", } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -339,7 +344,8 @@ var deleteRocketChatNotificationCmd = &cobra.Command{ resultData := output.Result{ Result: result.DeleteNotification, } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -411,7 +417,7 @@ var updateRocketChatNotificationCmd = &cobra.Command{ returnNonEmptyString(fmt.Sprintf("%v", result.Channel)), }, } - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: []string{ "ID", "Name", @@ -420,6 +426,7 @@ var updateRocketChatNotificationCmd = &cobra.Command{ }, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, diff --git a/cmd/notificationsslack.go b/cmd/notificationsslack.go index 7032f3a2..7113d32e 100644 --- a/cmd/notificationsslack.go +++ b/cmd/notificationsslack.go @@ -85,7 +85,7 @@ It does not configure a project to send notifications to Slack though, you need notificationData = append(notificationData, "-") } data = append(data, notificationData) - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: []string{ "ID", "Name", @@ -95,6 +95,7 @@ It does not configure a project to send notifications to Slack though, you need }, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -142,7 +143,8 @@ This command is used to add an existing Slack notification in Lagoon to a projec resultData := output.Result{ Result: "success", } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -193,7 +195,7 @@ var listProjectSlacksCmd = &cobra.Command{ }) } } - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: []string{ "Name", "Webhook", @@ -201,6 +203,7 @@ var listProjectSlacksCmd = &cobra.Command{ }, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -243,7 +246,7 @@ var listAllSlacksCmd = &cobra.Command{ } } } - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: []string{ "Project", "Name", @@ -252,6 +255,7 @@ var listAllSlacksCmd = &cobra.Command{ }, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -296,7 +300,8 @@ var deleteProjectSlackNotificationCmd = &cobra.Command{ resultData := output.Result{ Result: "success", } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -337,7 +342,8 @@ var deleteSlackNotificationCmd = &cobra.Command{ resultData := output.Result{ Result: result.DeleteNotification, } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -409,7 +415,7 @@ var updateSlackNotificationCmd = &cobra.Command{ returnNonEmptyString(fmt.Sprintf("%v", result.Channel)), }, } - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: []string{ "ID", "Name", @@ -418,6 +424,7 @@ var updateSlackNotificationCmd = &cobra.Command{ }, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, diff --git a/cmd/notificationsteams.go b/cmd/notificationsteams.go index d267fb66..49cb0ef5 100644 --- a/cmd/notificationsteams.go +++ b/cmd/notificationsteams.go @@ -79,7 +79,7 @@ It does not configure a project to send notifications to Microsoft Teams though, notificationData = append(notificationData, "-") } data = append(data, notificationData) - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: []string{ "ID", "Name", @@ -88,6 +88,7 @@ It does not configure a project to send notifications to Microsoft Teams though, }, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -135,7 +136,8 @@ This command is used to add an existing Microsoft Teams notification in Lagoon t resultData := output.Result{ Result: "success", } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -185,13 +187,14 @@ var listProjectMicrosoftTeamsCmd = &cobra.Command{ }) } } - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: []string{ "Name", "Webhook", }, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -233,7 +236,7 @@ var listAllMicrosoftTeamsCmd = &cobra.Command{ } } } - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: []string{ "Project", "Name", @@ -241,6 +244,7 @@ var listAllMicrosoftTeamsCmd = &cobra.Command{ }, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -285,7 +289,8 @@ var deleteProjectMicrosoftTeamsNotificationCmd = &cobra.Command{ resultData := output.Result{ Result: "success", } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -326,7 +331,8 @@ var deleteMicrosoftTeamsNotificationCmd = &cobra.Command{ resultData := output.Result{ Result: result.DeleteNotification, } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -392,7 +398,7 @@ var updateMicrosoftTeamsNotificationCmd = &cobra.Command{ returnNonEmptyString(fmt.Sprintf("%v", result.Webhook)), }, } - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: []string{ "ID", "Name", @@ -400,6 +406,7 @@ var updateMicrosoftTeamsNotificationCmd = &cobra.Command{ }, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, diff --git a/cmd/notificationswebhook.go b/cmd/notificationswebhook.go index 58eee15a..04125a0b 100644 --- a/cmd/notificationswebhook.go +++ b/cmd/notificationswebhook.go @@ -79,7 +79,7 @@ It does not configure a project to send notifications to webhook though, you nee notificationData = append(notificationData, "-") } data = append(data, notificationData) - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: []string{ "ID", "Name", @@ -88,6 +88,7 @@ It does not configure a project to send notifications to webhook though, you nee }, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -135,7 +136,8 @@ This command is used to add an existing webhook notification in Lagoon to a proj resultData := output.Result{ Result: "success", } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -185,13 +187,14 @@ var listProjectWebhooksCmd = &cobra.Command{ }) } } - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: []string{ "Name", "Webhook", }, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -233,7 +236,7 @@ var listAllWebhooksCmd = &cobra.Command{ } } } - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: []string{ "Project", "Name", @@ -241,6 +244,7 @@ var listAllWebhooksCmd = &cobra.Command{ }, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -285,7 +289,8 @@ var deleteProjectWebhookNotificationCmd = &cobra.Command{ resultData := output.Result{ Result: "success", } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -326,7 +331,8 @@ var deleteWebhookNotificationCmd = &cobra.Command{ resultData := output.Result{ Result: result.DeleteNotification, } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -392,7 +398,7 @@ var updateWebhookNotificationCmd = &cobra.Command{ returnNonEmptyString(fmt.Sprintf("%v", result.Webhook)), }, } - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: []string{ "ID", "Name", @@ -400,6 +406,7 @@ var updateWebhookNotificationCmd = &cobra.Command{ }, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, diff --git a/cmd/organization.go b/cmd/organization.go index 1685a2b9..5ef92789 100644 --- a/cmd/organization.go +++ b/cmd/organization.go @@ -90,7 +90,8 @@ var addOrganizationCmd = &cobra.Command{ "Organization Name": organizationName, }, } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -139,7 +140,8 @@ var deleteOrganizationCmd = &cobra.Command{ resultData := output.Result{ Result: organization.Name, } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -229,7 +231,8 @@ var updateOrganizationCmd = &cobra.Command{ "Organization Name": result.Name, }, } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } diff --git a/cmd/project.go b/cmd/project.go index c6432f57..5d01dd48 100644 --- a/cmd/project.go +++ b/cmd/project.go @@ -50,7 +50,8 @@ var deleteProjectCmd = &cobra.Command{ resultData := output.Result{ Result: "success", } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -203,7 +204,8 @@ var addProjectCmd = &cobra.Command{ if organizationName != "" { resultData.ResultData["Organization"] = organizationName } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -391,7 +393,8 @@ var updateProjectCmd = &cobra.Command{ "Project Name": projectUpdate.Name, }, } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -460,10 +463,11 @@ var listProjectByMetadata = &cobra.Command{ if showMetadata { header = append(header, "Metadata") } - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: header, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -510,10 +514,11 @@ var getProjectMetadata = &cobra.Command{ "Key", "Value", } - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: header, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -565,7 +570,7 @@ var updateProjectMetadata = &cobra.Command{ returnNonEmptyString(fmt.Sprintf("%v", projectResult.Name)), returnNonEmptyString(fmt.Sprintf("%v", string(metaData))), }) - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: []string{ "ID", "Name", @@ -573,6 +578,7 @@ var updateProjectMetadata = &cobra.Command{ }, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -621,7 +627,7 @@ var deleteProjectMetadataByKey = &cobra.Command{ returnNonEmptyString(fmt.Sprintf("%v", projectResult.Name)), returnNonEmptyString(fmt.Sprintf("%v", string(metaData))), }) - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: []string{ "ID", "Name", @@ -629,6 +635,7 @@ var deleteProjectMetadataByKey = &cobra.Command{ }, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -694,7 +701,8 @@ var removeProjectFromOrganizationCmd = &cobra.Command{ "Organization Name": organizationName, }, } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, diff --git a/cmd/retrieve.go b/cmd/retrieve.go index eec279bb..89e29a48 100644 --- a/cmd/retrieve.go +++ b/cmd/retrieve.go @@ -3,6 +3,7 @@ package cmd import ( "context" "fmt" + "github.com/uselagoon/lagoon-cli/pkg/output" "strings" "github.com/spf13/cobra" @@ -59,7 +60,9 @@ You can check the status of the backup using the list backups or get backup comm } return err } - fmt.Println("successfully created restore with ID:", result.ID) + resultData := output.Result{Result: fmt.Sprintf("successfully created restore with ID: %d", result.ID)} + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, diff --git a/cmd/tasks.go b/cmd/tasks.go index 3b28c0e4..02272424 100644 --- a/cmd/tasks.go +++ b/cmd/tasks.go @@ -79,7 +79,8 @@ var getTaskByID = &cobra.Command{ dataMain.Header = append(dataMain.Header, "Logs") dataMain.Data[0] = append(dataMain.Data[0], returnNonEmptyString(result.Logs)) } - output.RenderOutput(dataMain, outputOptions) + r := output.RenderOutput(dataMain, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -181,7 +182,8 @@ var runDrushArchiveDump = &cobra.Command{ Result: "success", ResultData: resultMap["taskDrushArchiveDump"].(map[string]interface{}), } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } else { return fmt.Errorf("unable to determine status of task") } @@ -249,7 +251,8 @@ var runDrushSQLDump = &cobra.Command{ Result: "success", ResultData: resultMap["taskDrushSqlDump"].(map[string]interface{}), } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } else { return fmt.Errorf("unable to determine status of task") } @@ -317,7 +320,8 @@ var runDrushCacheClear = &cobra.Command{ Result: "success", ResultData: resultMap["taskDrushCacheClear"].(map[string]interface{}), } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } else { return fmt.Errorf("unable to determine status of task") } @@ -388,7 +392,8 @@ Direct: "status": taskResult.Status, }, } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -489,7 +494,8 @@ Path: "id": taskResult.ID, }, } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -549,7 +555,8 @@ var uploadFilesToTask = &cobra.Command{ }, }, } - output.RenderOutput(dataMain, outputOptions) + r := output.RenderOutput(dataMain, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } diff --git a/cmd/users.go b/cmd/users.go index 8b0d05b9..73baaa7a 100644 --- a/cmd/users.go +++ b/cmd/users.go @@ -121,7 +121,8 @@ var addUserCmd = &cobra.Command{ "id": user.ID, }, } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -199,7 +200,8 @@ Add key by defining key value, but not specifying a key name (will default to tr "ID": result.ID, }, } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -241,7 +243,8 @@ var deleteSSHKeyCmd = &cobra.Command{ resultData := output.Result{ Result: "success", } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -287,7 +290,8 @@ var deleteUserCmd = &cobra.Command{ resultData := output.Result{ Result: "success", } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -362,7 +366,8 @@ var updateUserCmd = &cobra.Command{ "ID": user.ID, }, } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -423,7 +428,8 @@ var getUserKeysCmd = &cobra.Command{ } outputOptions.MultiLine = true - output.RenderOutput(dataMain, outputOptions) + r := output.RenderOutput(dataMain, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -492,7 +498,8 @@ var getAllUserKeysCmd = &cobra.Command{ Data: data, } outputOptions.MultiLine = true - output.RenderOutput(dataMain, outputOptions) + r := output.RenderOutput(dataMain, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -568,7 +575,8 @@ var addAdministratorToOrganizationCmd = &cobra.Command{ "Organization Name": organizationName, }, } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) return nil }, } @@ -635,7 +643,8 @@ var removeAdministratorFromOrganizationCmd = &cobra.Command{ "Organization Name": organizationName, }, } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, @@ -682,7 +691,8 @@ var resetPasswordCmd = &cobra.Command{ resultData := output.Result{ Result: "success", } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, diff --git a/cmd/variables.go b/cmd/variables.go index 88e6bc5a..c6a210ea 100644 --- a/cmd/variables.go +++ b/cmd/variables.go @@ -85,10 +85,11 @@ var addVariableCmd = &cobra.Command{ header = append(header, "Scope") header = append(header, "Name") header = append(header, "Value") - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: header, Data: data, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } else { output.RenderInfo(fmt.Sprintf("variable %s remained unchanged", varName), outputOptions) } @@ -142,7 +143,8 @@ var deleteVariableCmd = &cobra.Command{ resultData := output.Result{ Result: deleteResult.DeleteEnvVar, } - output.RenderResult(resultData, outputOptions) + r := output.RenderResult(resultData, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil }, diff --git a/cmd/whoami.go b/cmd/whoami.go index d8193548..ff0387b8 100644 --- a/cmd/whoami.go +++ b/cmd/whoami.go @@ -86,12 +86,13 @@ This is useful if you have multiple keys or accounts in multiple lagoons and nee } keys = append(keys, keyData) } - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: header, Data: keys, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } else { - output.RenderOutput(output.Table{ + r := output.RenderOutput(output.Table{ Header: []string{ "ID", "Email", @@ -109,6 +110,7 @@ This is useful if you have multiple keys or accounts in multiple lagoons and nee }, }, }, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) } return nil diff --git a/pkg/output/main.go b/pkg/output/main.go index e40e24f0..35388a33 100644 --- a/pkg/output/main.go +++ b/pkg/output/main.go @@ -1,6 +1,7 @@ package output import ( + "bytes" "encoding/json" "fmt" "os" @@ -39,7 +40,7 @@ type Result struct { } // RenderJSON . -func RenderJSON(data interface{}, opts Options) { +func RenderJSON(data interface{}, opts Options) string { var jsonBytes []byte var err error if opts.Pretty { @@ -53,7 +54,7 @@ func RenderJSON(data interface{}, opts Options) { panic(err) } } - fmt.Println(string(jsonBytes)) + return string(jsonBytes) } // RenderError . @@ -64,8 +65,7 @@ func RenderError(errorMsg string, opts Options) { } RenderJSON(jsonData, opts) } else { - //fmt.Println(fmt.Sprintf("Error: %s", aurora.Yellow(trimQuotes(errorMsg)))) - fmt.Println("Error:", trimQuotes(errorMsg)) + os.Stderr.WriteString(fmt.Sprintf("Error: %s", trimQuotes(errorMsg))) } } @@ -77,38 +77,40 @@ func RenderInfo(infoMsg string, opts Options) { } RenderJSON(jsonData, opts) } else { - fmt.Println("Info:", trimQuotes(infoMsg)) + os.Stderr.WriteString(fmt.Sprintf("Info: %s", trimQuotes(infoMsg))) } } // RenderResult . -func RenderResult(result Result, opts Options) { +func RenderResult(result Result, opts Options) string { + var out bytes.Buffer if opts.JSON { - RenderJSON(result, opts) + return RenderJSON(result, opts) } else { if trimQuotes(result.Result) == "success" { - fmt.Printf("Result: %s\n", aurora.Green(trimQuotes(result.Result))) + out.WriteString(fmt.Sprintf("Result: %s\n", aurora.Green(trimQuotes(result.Result)))) if len(result.ResultData) != 0 { for k, v := range result.ResultData { - fmt.Printf("%s: %v\n", k, v) + out.WriteString(fmt.Sprintf("%s: %v\n", k, v)) } } } else { fmt.Printf("Result: %s\n", aurora.Yellow(trimQuotes(result.Result))) if len(result.ResultData) != 0 { for k, v := range result.ResultData { - fmt.Printf("%s: %v\n", k, v) + out.WriteString(fmt.Sprintf("%s: %v\n", k, v)) } } } } - + return out.String() } // RenderOutput . -func RenderOutput(data Table, opts Options) { +func RenderOutput(data Table, opts Options) string { + var out bytes.Buffer if opts.Debug { - fmt.Printf("%s\n", aurora.Yellow("Final result:")) + out.WriteString(fmt.Sprintf("%s\n", aurora.Yellow("Final result:"))) } if opts.JSON { // really basic tabledata to json implementation @@ -124,7 +126,7 @@ func RenderOutput(data Table, opts Options) { returnedData := map[string]interface{}{ "data": rawData, } - RenderJSON(returnedData, opts) + return RenderJSON(returnedData, opts) } else { // otherwise render a table if opts.Error != "" { @@ -139,7 +141,7 @@ func RenderOutput(data Table, opts Options) { } t.AppendHeader(hRow) } - t.SetOutputMirror(os.Stdout) + t.SetOutputMirror(&out) for _, rowData := range data.Data { var dRow table.Row for _, k := range rowData { @@ -164,9 +166,10 @@ func RenderOutput(data Table, opts Options) { if opts.CSV { t.RenderCSV() - return + return out.String() } t.Render() + return out.String() } } diff --git a/pkg/output/main_test.go b/pkg/output/main_test.go index 37a1e886..2c295957 100644 --- a/pkg/output/main_test.go +++ b/pkg/output/main_test.go @@ -20,132 +20,105 @@ func checkEqual(t *testing.T, got, want interface{}, msgs ...interface{}) { } } +func TestRenderJSON(t *testing.T) { + var testData = `Error Message` + var testSuccess = `{ + "error": "Error Message" +}` + outputOptions := Options{ + Header: false, + CSV: false, + JSON: true, + Pretty: true, + } + + jsonData := Result{ + Error: trimQuotes(testData), + } + output := RenderJSON(jsonData, outputOptions) + if output != testSuccess { + checkEqual(t, output, testSuccess, " render error json processing failed") + } +} + func TestRenderError(t *testing.T) { var testData = `Error Message` - var testSuccess1 = `{"error":"Error Message"} -` - var testSuccess2 = `Error: Error Message -` + var testSuccess = `Error: Error Message` outputOptions := Options{ Header: false, CSV: false, - JSON: true, + JSON: false, Pretty: false, } - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - RenderError(testData, outputOptions) - w.Close() - out, _ := io.ReadAll(r) - os.Stdout = rescueStdout - if string(out) != testSuccess1 { - checkEqual(t, string(out), testSuccess1, " render error json processing failed") - } - outputOptions.JSON = false - rescueStdout = os.Stdout - r, w, _ = os.Pipe() - os.Stdout = w + rescueStdout := os.Stderr + r, w, _ := os.Pipe() + defer func() { + os.Stderr = rescueStdout + }() + os.Stderr = w RenderError(testData, outputOptions) w.Close() - out, _ = io.ReadAll(r) - os.Stdout = rescueStdout - if string(out) != testSuccess2 { - checkEqual(t, string(out), testSuccess2, " render error stdout processing failed") + var out bytes.Buffer + io.Copy(&out, r) + if out.String() != testSuccess { + checkEqual(t, out.String(), testSuccess, " render error stdout processing failed") } } func TestRenderInfo(t *testing.T) { var testData = `Info Message` - var testSuccess1 = `{"info":"Info Message"} -` - var testSuccess2 = `Info: Info Message -` + var testSuccess1 = `Info: Info Message` outputOptions := Options{ Header: false, CSV: false, - JSON: true, + JSON: false, Pretty: false, } - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - RenderInfo(testData, outputOptions) - w.Close() - out, _ := io.ReadAll(r) - os.Stdout = rescueStdout - if string(out) != testSuccess1 { - checkEqual(t, string(out), testSuccess1, " render info json processing failed") - } - outputOptions.JSON = false - rescueStdout = os.Stdout - r, w, _ = os.Pipe() - os.Stdout = w + rescueStdout := os.Stderr + r, w, _ := os.Pipe() + defer func() { + os.Stderr = rescueStdout + }() + os.Stderr = w RenderInfo(testData, outputOptions) w.Close() - out, _ = io.ReadAll(r) - os.Stdout = rescueStdout - if string(out) != testSuccess2 { - checkEqual(t, string(out), testSuccess2, " render info stdout processing failed") + var out bytes.Buffer + io.Copy(&out, r) + if out.String() != testSuccess1 { + checkEqual(t, out.String(), testSuccess1, " render info stdout processing failed") } } func TestRenderOutput(t *testing.T) { var testData = `{"header":["NID","NotificationName","Channel","Webhook"],"data":[["1","amazeeio--lagoon-local-ci","lagoon-local-ci","https://amazeeio.rocket.chat/hooks/ikF5XMohDZK7KpsZf/c9BFBt2ch8oMMuycoERJQMSLTPo8nmZhg2Hf2ny68ZpuD4Kn"]]}` - var testSuccess1 = `{"data":[{"channel":"lagoon-local-ci","nid":"1","notificationname":"amazeeio--lagoon-local-ci","webhook":"https://amazeeio.rocket.chat/hooks/ikF5XMohDZK7KpsZf/c9BFBt2ch8oMMuycoERJQMSLTPo8nmZhg2Hf2ny68ZpuD4Kn"}]} -` - var testSuccess2 = `NID NOTIFICATIONNAME CHANNEL WEBHOOK + var testSuccess1 = `NID NOTIFICATIONNAME CHANNEL WEBHOOK 1 amazeeio--lagoon-local-ci lagoon-local-ci https://amazeeio.rocket.chat/hooks/ikF5XMohDZK7KpsZf/c9BFBt2ch8oMMuycoERJQMSLTPo8nmZhg2Hf2ny68ZpuD4Kn ` - var testSuccess3 = `1 amazeeio--lagoon-local-ci lagoon-local-ci https://amazeeio.rocket.chat/hooks/ikF5XMohDZK7KpsZf/c9BFBt2ch8oMMuycoERJQMSLTPo8nmZhg2Hf2ny68ZpuD4Kn + var testSuccess2 = `1 amazeeio--lagoon-local-ci lagoon-local-ci https://amazeeio.rocket.chat/hooks/ikF5XMohDZK7KpsZf/c9BFBt2ch8oMMuycoERJQMSLTPo8nmZhg2Hf2ny68ZpuD4Kn ` outputOptions := Options{ Header: false, CSV: false, - JSON: true, + JSON: false, Pretty: false, } var dataMain Table json.Unmarshal([]byte(testData), &dataMain) - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - RenderOutput(dataMain, outputOptions) - w.Close() - out, _ := io.ReadAll(r) - os.Stdout = rescueStdout - if string(out) != testSuccess1 { - checkEqual(t, string(out), testSuccess1, " render output json processing failed") - } - - outputOptions.JSON = false - rescueStdout = os.Stdout - r, w, _ = os.Pipe() - os.Stdout = w - RenderOutput(dataMain, outputOptions) - w.Close() - out, _ = io.ReadAll(r) - os.Stdout = rescueStdout - if string(out) != testSuccess2 { - checkEqual(t, string(out), testSuccess2, " render output table stdout processing failed") + output := RenderOutput(dataMain, outputOptions) + if output != testSuccess1 { + checkEqual(t, output, testSuccess1, " render output table stdout processing failed") } outputOptions.Header = true - rescueStdout = os.Stdout - r, w, _ = os.Pipe() - os.Stdout = w - RenderOutput(dataMain, outputOptions) - w.Close() - out, _ = io.ReadAll(r) - os.Stdout = rescueStdout - if string(out) != testSuccess3 { - checkEqual(t, string(out), testSuccess3, " render output table stdout no header processing failed") + output = RenderOutput(dataMain, outputOptions) + if output != testSuccess2 { + checkEqual(t, output, testSuccess2, " render output table stdout no header processing failed") } }