Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task: remove the unused output format for SDK migration #2750

Merged
merged 1 commit into from Mar 11, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 0 additions & 44 deletions internal/cli/output_opts.go
Expand Up @@ -15,14 +15,12 @@
package cli

import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strings"

"github.com/PaesslerAG/jsonpath"
"github.com/mongodb/mongodb-atlas-cli/mongocli/v2/internal/config"
"github.com/mongodb/mongodb-atlas-cli/mongocli/v2/internal/jsonpathwriter"
"github.com/mongodb/mongodb-atlas-cli/mongocli/v2/internal/jsonwriter"
Expand Down Expand Up @@ -131,48 +129,6 @@ func (opts *OutputOpts) Print(o interface{}) error {
return err
}

func (opts *OutputOpts) PrintForCompactResultsResponse(o interface{}) error {
if opts.ConfigOutput() == jsonFormat {
compactResponse, err := mapReduceResults(o)
if err == nil {
return jsonwriter.Print(opts.ConfigWriter(), compactResponse)
}
}

outputType, val := opts.outputTypeAndValue()
if outputType == jsonPath {
compactResponse, err := mapReduceResults(o)
if err == nil {
return jsonpathwriter.Print(opts.ConfigWriter(), val, compactResponse)
}
}

t, err := template(outputType, val)
if err != nil {
return err
}

if t != "" {
return templatewriter.Print(opts.ConfigWriter(), t, o)
}
_, err = fmt.Fprintln(opts.ConfigWriter(), o)
return err
}

func mapReduceResults(o interface{}) (interface{}, error) {
jsonString, err := json.Marshal(o)
if err != nil {
return nil, err
}

var val interface{}
if e := json.Unmarshal(jsonString, &val); e != nil {
return nil, e
}

return jsonpath.Get("results", val)
}

// outputTypeAndValue returns the output type and the associated value
// Current available output types are:
// "go-template=Template string",
Expand Down
34 changes: 0 additions & 34 deletions internal/cli/output_opts_test.go
Expand Up @@ -18,11 +18,7 @@ package cli

import (
"io"
"reflect"
"testing"

"github.com/mongodb/mongodb-atlas-cli/mongocli/v2/internal/pointer"
atlasv2 "go.mongodb.org/atlas-sdk/v20231115007/admin"
)

func TestOutputOpts_outputTypeAndValue(t *testing.T) {
Expand Down Expand Up @@ -68,33 +64,3 @@ func TestOutputOpts_outputTypeAndValue(t *testing.T) {
})
}
}

func TestOutputOpts_mapReduceResults(t *testing.T) {
t.Run("when results present", func(t *testing.T) {
input := *atlasv2.NewPaginatedTeam()
wantID := "123"
wantName := "Team A"
input.Results = &[]atlasv2.TeamResponse{
{
Id: pointer.Get(wantID),
Name: pointer.Get(wantName),
},
}

compactResults, err := mapReduceResults(input)
if err != nil {
t.Fatalf("mapReduceResults() unexpected error: %v", err)
}

mapArrayResponse := reflect.ValueOf(compactResults).Interface().([]interface{})
mapResponse := mapArrayResponse[0].(map[string]interface{})
gotID := mapResponse["id"]
gotName := mapResponse["name"]
if gotID != wantID {
t.Errorf("mapReduceResults() got = %v, want %v", gotID, wantID)
}
if gotName != wantName {
t.Errorf("mapReduceResults() got = %v, want %v", gotName, wantName)
}
})
}