-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from sullivtr/main
output wrapper lib
- Loading branch information
Showing
3 changed files
with
164 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package output | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"buf.build/go/protoyaml" | ||
"github.com/rodaine/table" | ||
"google.golang.org/protobuf/encoding/protojson" | ||
"google.golang.org/protobuf/proto" | ||
) | ||
|
||
type TableFormatterFunc func() (ColumnFormatter, RowFormatterFunc) | ||
type RowFormatterFunc func() RowFormatter | ||
type RowFormatter [][]any | ||
type ColumnFormatter []any | ||
|
||
func CLIPrint(w io.Writer, format string, data proto.Message, tableFormatterFunc TableFormatterFunc) error { | ||
switch format { | ||
case "yaml": | ||
return printYAML(w, data) | ||
case "json": | ||
return printJSON(w, data) | ||
case "table": | ||
headers, rowDataFunc := tableFormatterFunc() | ||
if headers == nil { | ||
return fmt.Errorf("headers must be provided for table output") | ||
} | ||
return printTable(w, headers, rowDataFunc()) | ||
default: | ||
return fmt.Errorf("unsupported format: %s", format) | ||
} | ||
} | ||
|
||
func printYAML(w io.Writer, data proto.Message) error { | ||
marshaller := protoyaml.MarshalOptions{ | ||
Indent: 2, | ||
} | ||
|
||
output, err := marshaller.Marshal(data) | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal data to YAML: %w", err) | ||
} | ||
fmt.Fprint(w, string(output)) | ||
return nil | ||
} | ||
|
||
func printJSON(w io.Writer, data proto.Message) error { | ||
output, err := protojson.Marshal(data) | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal data to JSON: %w", err) | ||
} | ||
fmt.Fprint(w, string(output)) | ||
return nil | ||
} | ||
|
||
func printTable(w io.Writer, headers ColumnFormatter, rowData [][]any) error { | ||
t := table.New(headers...) | ||
t.WithWriter(w) | ||
for _, row := range rowData { | ||
t.AddRow(row) | ||
} | ||
t.Print() | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package output | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
"testing" | ||
|
||
resourcemanagerv1alpha "buf.build/gen/go/datum-cloud/datum-os/protocolbuffers/go/datum/os/resourcemanager/v1alpha" | ||
"google.golang.org/protobuf/proto" | ||
) | ||
|
||
func TestCLIPrint(t *testing.T) { | ||
|
||
testOrgProto := &resourcemanagerv1alpha.Organization{ | ||
DisplayName: "Test Organization", | ||
OrganizationId: "1234", | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
format string | ||
data proto.Message | ||
headers []any | ||
rowData [][]any | ||
wantErr bool | ||
wantOutput string | ||
}{ | ||
{ | ||
name: "Print YAML", | ||
format: "yaml", | ||
data: testOrgProto, | ||
wantErr: false, | ||
wantOutput: "organizationId: \"1234\"\ndisplayName: Test Organization\n", | ||
}, | ||
{ | ||
name: "Print JSON", | ||
format: "json", | ||
data: testOrgProto, | ||
wantErr: false, | ||
wantOutput: "{\"organizationId\":\"1234\",\"displayName\":\"Test Organization\"}", | ||
}, | ||
{ | ||
name: "Print Table", | ||
format: "table", | ||
headers: []any{"Header1", "Header2"}, | ||
rowData: [][]any{{"Row1Col1", "Row1Col2"}, {"Row2Col1", "Row2Col2"}}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Unsupported Format", | ||
format: "unsupported", | ||
data: testOrgProto, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "Table without headers and rowData", | ||
format: "table", | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var buf bytes.Buffer | ||
|
||
err := CLIPrint(&buf, tt.format, tt.data, func() (ColumnFormatter, RowFormatterFunc) { | ||
return tt.headers, func() RowFormatter { | ||
return tt.rowData | ||
} | ||
}) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("CLIPrint() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
|
||
if !tt.wantErr { | ||
if tt.format == "table" { | ||
out := buf.String() | ||
if !strings.Contains(out, tt.headers[0].(string)) || !strings.Contains(out, tt.headers[1].(string)) { | ||
t.Errorf("CLIPrint() output = %v, does not have correct headers", out) | ||
} | ||
} else { | ||
if gotOutput := buf.String(); gotOutput != tt.wantOutput { | ||
t.Errorf("CLIPrint() output = \n%v, want \n%v", gotOutput, tt.wantOutput) | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} |