-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract outputs into smaller interfaces
- Loading branch information
Showing
17 changed files
with
415 additions
and
340 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,53 @@ | ||
package commands | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/bryanl/doit" | ||
) | ||
|
||
type displayer interface { | ||
JSON(io.Writer) error | ||
String(io.Writer) error | ||
} | ||
|
||
// displayOutput displays an object or group of objects to a user. It | ||
// checks to see what the output type should be. | ||
func displayOutput(item displayer, out io.Writer) error { | ||
output, err := doit.DoitConfig.GetString(doit.NSRoot, "output") | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
if output == "" { | ||
output = "text" | ||
} | ||
|
||
switch output { | ||
case "json": | ||
return item.JSON(out) | ||
case "text": | ||
return item.String(out) | ||
default: | ||
return fmt.Errorf("unknown output type") | ||
} | ||
} | ||
|
||
func writeJSON(item interface{}, w io.Writer) error { | ||
b, err := json.Marshal(item) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var out bytes.Buffer | ||
err = json.Indent(&out, b, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
_, err = out.WriteTo(w) | ||
|
||
return err | ||
} |
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
Oops, something went wrong.