Skip to content

Commit

Permalink
Updates additional commands and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CGoodwin90 authored and shreddedbacon committed Aug 5, 2024
1 parent 36c1fd7 commit 140a5a0
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 90 deletions.
3 changes: 2 additions & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
17 changes: 13 additions & 4 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
},
Expand Down Expand Up @@ -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
},
Expand Down Expand Up @@ -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
},
Expand Down Expand Up @@ -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
},
Expand Down
6 changes: 5 additions & 1 deletion cmd/deploytargetconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
4 changes: 3 additions & 1 deletion cmd/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"context"
"fmt"
"github.com/uselagoon/lagoon-cli/pkg/output"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -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
},
Expand Down
135 changes: 54 additions & 81 deletions pkg/output/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit 140a5a0

Please sign in to comment.