-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): implement JSON format for all commands
We are adding a new Global Flag called `--json` that users can use to switch the CLI output format to display JSON instead of the default, human readable format. To make this setting persistent on the current terminal, a user can export the environment variable `LW_JSON=1`. Signed-off-by: Salim Afiune Maya <[email protected]>
- Loading branch information
Showing
6 changed files
with
143 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// Author:: Salim Afiune Maya (<[email protected]>) | ||
// Copyright:: Copyright 2020, Lacework Inc. | ||
// License:: Apache License, Version 2.0 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/fatih/color" | ||
) | ||
|
||
// OutputJSON will print out the JSON representation of the provided data | ||
func (c *cliState) OutputJSON(v interface{}) error { | ||
pretty, err := c.JsonF.Marshal(v) | ||
if err != nil { | ||
cli.Log.Debugw("unable to pretty print JSON object", "raw", v) | ||
return err | ||
} | ||
fmt.Fprintln(color.Output, string(pretty)) | ||
return nil | ||
} | ||
|
||
// OutputHumanRead will print out the provided message if the cli state is | ||
// configured to talk to humans, to switch to json format use --json | ||
func (c *cliState) OutputHuman(format string, a ...interface{}) { | ||
if c.HumanOutput() { | ||
fmt.Fprintf(os.Stdout, format, a...) | ||
} | ||
} |
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