-
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.
feat: Create an --output/-o flag for JSON or plain text responses (#195)
Create an --output/-o flag for JSON or plain text responses * accepts either "json" or "plaintext * defaults to plaintext
- Loading branch information
Showing
14 changed files
with
369 additions
and
35 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
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
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
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,49 @@ | ||
package output | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
var multiplePlaintextOutputFn = func(r resource) string { | ||
return fmt.Sprintf("* %s (%s)", r.Name, r.Key) | ||
} | ||
|
||
// TODO: rename this to be "cleaner"? -- NewMultipleOutput() | ||
func NewMultipleOutputterFn(input []byte) multipleOutputterFn { | ||
return multipleOutputterFn{ | ||
input: input, | ||
} | ||
} | ||
|
||
type multipleOutputterFn struct { | ||
input []byte | ||
} | ||
|
||
func (o multipleOutputterFn) New() (Outputter, error) { | ||
var r resources | ||
err := json.Unmarshal(o.input, &r) | ||
if err != nil { | ||
return MultipleOutputter{}, err | ||
} | ||
|
||
return MultipleOutputter{ | ||
outputFn: multiplePlaintextOutputFn, | ||
resources: r, | ||
resourceJSON: o.input, | ||
}, nil | ||
} | ||
|
||
type MultipleOutputter struct { | ||
outputFn PlaintextOutputFn | ||
resources resources | ||
resourceJSON []byte | ||
} | ||
|
||
func (o MultipleOutputter) JSON() string { | ||
return string(o.resourceJSON) | ||
} | ||
|
||
func (o MultipleOutputter) String() string { | ||
return formatColl(o.resources.Items, o.outputFn) | ||
} |
Oops, something went wrong.