diff --git a/cmd/config.go b/cmd/config.go index dd52b0fb..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) }, } 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/deploytargetconfig.go b/cmd/deploytargetconfig.go index a033bef1..84864777 100644 --- a/cmd/deploytargetconfig.go +++ b/cmd/deploytargetconfig.go @@ -249,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 }, diff --git a/cmd/environment.go b/cmd/environment.go index bdd8a3e6..f77a06fc 100644 --- a/cmd/environment.go +++ b/cmd/environment.go @@ -293,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/list.go b/cmd/list.go index a7ba92f9..d08bc247 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -1104,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 }, } 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/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") } }