-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Support nomad CLI output with JSON and template format #1503
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6c69401
Support JSON and template format with nomad CLI
nak3 5bd39f8
fix go panic
nak3 02613e1
Stop using format option and support json and t option
nak3 8f03eb9
Add doc about -json and -t options
nak3 4a3f63f
Update help and error message
nak3 4edc906
Support JSON and template data output list when no args specified
nak3 7d804a4
Add test to check both -json and -t are not specified
nak3 5aceb8e
Update after another review
nak3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 command | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"text/template" | ||
) | ||
|
||
//DataFormatter is a transformer of the data. | ||
type DataFormatter interface { | ||
// TransformData should return transformed string data. | ||
TransformData(interface{}) (string, error) | ||
} | ||
|
||
// DataFormat returns the data formatter specified format. | ||
func DataFormat(format, tmpl string) (DataFormatter, error) { | ||
switch format { | ||
case "json": | ||
if len(tmpl) > 0 { | ||
return nil, fmt.Errorf("json format does not support template option.") | ||
} | ||
return &JSONFormat{}, nil | ||
case "template": | ||
return &TemplateFormat{tmpl}, nil | ||
} | ||
return nil, fmt.Errorf("Unsupported format is specified.") | ||
} | ||
|
||
type JSONFormat struct { | ||
} | ||
|
||
// TransformData returns JSON format string data. | ||
func (p *JSONFormat) TransformData(data interface{}) (string, error) { | ||
out, err := json.MarshalIndent(&data, "", " ") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(out), nil | ||
} | ||
|
||
type TemplateFormat struct { | ||
tmpl string | ||
} | ||
|
||
// TransformData returns template format string data. | ||
func (p *TemplateFormat) TransformData(data interface{}) (string, error) { | ||
var out io.Writer = new(bytes.Buffer) | ||
if len(p.tmpl) == 0 { | ||
return "", fmt.Errorf("template needs to be specified the golang templates.") | ||
} | ||
|
||
t, err := template.New("format").Parse(p.tmpl) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
err = t.Execute(out, data) | ||
if err != nil { | ||
return "", err | ||
} | ||
return fmt.Sprint(out), 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,64 @@ | ||
package command | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
type testData struct { | ||
Region string | ||
ID string | ||
Name string | ||
} | ||
|
||
const expectJSON = `{ | ||
"Region": "global", | ||
"ID": "1", | ||
"Name": "example" | ||
}` | ||
|
||
var ( | ||
tData = testData{"global", "1", "example"} | ||
testFormat = map[string]string{"json": "", "template": "{{.Region}}"} | ||
expectOutput = map[string]string{"json": expectJSON, "template": "global"} | ||
) | ||
|
||
func TestDataFormat(t *testing.T) { | ||
for k, v := range testFormat { | ||
fm, err := DataFormat(k, v) | ||
if err != nil { | ||
t.Fatalf("err: %v", err) | ||
} | ||
|
||
result, err := fm.TransformData(tData) | ||
if err != nil { | ||
t.Fatalf("err: %v", err) | ||
} | ||
|
||
if result != expectOutput[k] { | ||
t.Fatalf("expected output: %s, actual: %s", expectOutput[k], result) | ||
} | ||
} | ||
} | ||
|
||
func TestInvalidJSONTemplate(t *testing.T) { | ||
// Invalid template {{.foo}} | ||
fm, err := DataFormat("template", "{{.foo}}") | ||
if err != nil { | ||
t.Fatalf("err: %v", err) | ||
} | ||
_, err = fm.TransformData(tData) | ||
if !strings.Contains(err.Error(), "foo is not a field of struct type command.testData") { | ||
t.Fatalf("expected invalid template error, got: %s", err.Error()) | ||
} | ||
|
||
// No template is specified | ||
fm, err = DataFormat("template", "") | ||
if err != nil { | ||
t.Fatalf("err: %v", err) | ||
} | ||
_, err = fm.TransformData(tData) | ||
if !strings.Contains(err.Error(), "template needs to be specified the golang templates.") { | ||
t.Fatalf("expected not specified template error, got: %s", err.Error()) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a test that you get an error with an invalid go template format