-
-
Notifications
You must be signed in to change notification settings - Fork 4
Improve output format #32
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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f7a9d77
Replace fmt print into log info
polldo fb1d716
Distinguish board from device
polldo 7801fce
Improve device format output
polldo 7293f81
Fix log infof
polldo 179f886
Improve logs of arduino-cli
polldo 9c719b1
Fix log msg
polldo 6690306
Update go mod
polldo 69d71b0
Improve thing output format
polldo 493689c
Refactor thing output format
polldo 29b415d
Fix device iot client
polldo 370916c
Improve errors: use stderr and error codes
polldo c318bbc
cli package: don't return error
polldo 37bd229
Remove unreachable code
polldo 887249d
Improve log initialization
polldo 01c92f0
Remove duplication of info messages
polldo 1787c4f
Add arduino-cli source to upload logs
polldo 5d10e44
Return consistent output
polldo 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 hidden or 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 hidden or 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,83 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
|
||
"github.com/arduino/arduino-cli/cli/errorcodes" | ||
"github.com/arduino/arduino-cli/cli/feedback" | ||
"github.com/arduino/iot-cloud-cli/cli/config" | ||
"github.com/arduino/iot-cloud-cli/cli/device" | ||
"github.com/arduino/iot-cloud-cli/cli/thing" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var cliFlags struct { | ||
verbose bool | ||
outputFormat string | ||
} | ||
|
||
func Execute() { | ||
cli := &cobra.Command{ | ||
Use: "arduino-cloud-cli", | ||
Short: "Arduino Cloud CLI.", | ||
Long: "Arduino Cloud Command Line Interface (arduino-cloud-cli).", | ||
PersistentPreRun: preRun, | ||
} | ||
|
||
cli.AddCommand(config.NewCommand()) | ||
cli.AddCommand(device.NewCommand()) | ||
cli.AddCommand(thing.NewCommand()) | ||
|
||
cli.PersistentFlags().BoolVarP(&cliFlags.verbose, "verbose", "v", false, "Print the logs on the standard output.") | ||
cli.PersistentFlags().StringVar(&cliFlags.outputFormat, "format", "text", "The output format, can be {text|json}.") | ||
|
||
if err := cli.Execute(); err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
} | ||
} | ||
|
||
func parseFormatString(arg string) (feedback.OutputFormat, bool) { | ||
f, found := map[string]feedback.OutputFormat{ | ||
"json": feedback.JSON, | ||
"text": feedback.Text, | ||
}[arg] | ||
|
||
return f, found | ||
} | ||
|
||
func preRun(cmd *cobra.Command, args []string) { | ||
// enable log if verbose flag is passed | ||
if cliFlags.verbose { | ||
logrus.SetLevel(logrus.InfoLevel) | ||
logrus.SetOutput(os.Stdout) | ||
logrus.SetFormatter(&logrus.TextFormatter{ | ||
ForceColors: false, | ||
}) | ||
} else { | ||
logrus.SetOutput(ioutil.Discard) | ||
} | ||
// logrus. | ||
glumia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// normalize the format strings | ||
cliFlags.outputFormat = strings.ToLower(cliFlags.outputFormat) | ||
// check the right output format was passed | ||
format, found := parseFormatString(cliFlags.outputFormat) | ||
if !found { | ||
feedback.Error("Invalid output format: " + cliFlags.outputFormat) | ||
os.Exit(errorcodes.ErrBadCall) | ||
} | ||
// use the output format to configure the Feedback | ||
feedback.SetFormat(format) | ||
|
||
if cliFlags.outputFormat != "text" { | ||
cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) { | ||
polldo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
logrus.Warn("Calling help on JSON format") | ||
polldo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
feedback.Error("Invalid Call : should show Help, but it is available only in TEXT mode.") | ||
os.Exit(errorcodes.ErrBadCall) | ||
}) | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.