-
Notifications
You must be signed in to change notification settings - Fork 2k
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 #1503 from nak3/add-printer
Support nomad CLI output with JSON and template format
- Loading branch information
Showing
14 changed files
with
488 additions
and
22 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
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.