Skip to content

Commit 729401e

Browse files
author
Massimiliano Pippi
committed
remove format from globals
1 parent 452f010 commit 729401e

File tree

7 files changed

+21
-38
lines changed

7 files changed

+21
-38
lines changed

cli/cli.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/arduino/arduino-cli/cli/generatedocs"
3434
"github.com/arduino/arduino-cli/cli/globals"
3535
"github.com/arduino/arduino-cli/cli/lib"
36+
"github.com/arduino/arduino-cli/cli/output"
3637
"github.com/arduino/arduino-cli/cli/sketch"
3738
"github.com/arduino/arduino-cli/cli/upload"
3839
"github.com/arduino/arduino-cli/cli/version"
@@ -52,9 +53,10 @@ var (
5253
PersistentPreRun: preRun,
5354
}
5455

55-
verbose bool
56-
logFile string
57-
logFormat string
56+
verbose bool
57+
logFile string
58+
logFormat string
59+
outputFormat string
5860
)
5961

6062
const (
@@ -83,7 +85,7 @@ func createCliCommandTree(cmd *cobra.Command) {
8385
cmd.PersistentFlags().StringVar(&globals.LogLevel, "log-level", defaultLogLevel, "Messages with this level and above will be logged (default: warn).")
8486
cmd.PersistentFlags().StringVar(&logFile, "log-file", "", "Path to the file where logs will be written.")
8587
cmd.PersistentFlags().StringVar(&logFormat, "log-format", "text", "The output format for the logs, can be [text|json].")
86-
cmd.PersistentFlags().StringVar(&globals.OutputFormat, "format", "text", "The output format, can be [text|json].")
88+
cmd.PersistentFlags().StringVar(&outputFormat, "format", "text", "The output format, can be [text|json].")
8789
cmd.PersistentFlags().StringVar(&globals.YAMLConfigFile, "config-file", "", "The custom config file (if not specified the default will be used).")
8890
cmd.PersistentFlags().StringSliceVar(&globals.AdditionalUrls, "additional-urls", []string{}, "Additional URLs for the board manager.")
8991
}
@@ -115,7 +117,9 @@ func parseFormatString(arg string) (feedback.OutputFormat, bool) {
115117

116118
func preRun(cmd *cobra.Command, args []string) {
117119
// normalize the format strings
118-
globals.OutputFormat = strings.ToLower(globals.OutputFormat)
120+
outputFormat = strings.ToLower(outputFormat)
121+
// configure the output package
122+
output.OutputFormat = outputFormat
119123
logFormat = strings.ToLower(logFormat)
120124

121125
// should we log to file?
@@ -159,9 +163,9 @@ func preRun(cmd *cobra.Command, args []string) {
159163
}
160164

161165
// check the right output format was passed
162-
format, found := parseFormatString(globals.OutputFormat)
166+
format, found := parseFormatString(outputFormat)
163167
if !found {
164-
feedback.Error("Invalid output format: " + globals.OutputFormat)
168+
feedback.Error("Invalid output format: " + outputFormat)
165169
os.Exit(errorcodes.ErrBadCall)
166170
}
167171

@@ -174,7 +178,7 @@ func preRun(cmd *cobra.Command, args []string) {
174178
logrus.Info("Starting root command preparation (`arduino`)")
175179

176180
logrus.Info("Formatter set")
177-
if globals.OutputFormat != "text" {
181+
if outputFormat != "text" {
178182
cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
179183
logrus.Warn("Calling help on JSON format")
180184
feedback.Error("Invalid Call : should show Help, but it is available only in TEXT mode.")

cli/config/init.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ func initInitCommand() *cobra.Command {
4040
Args: cobra.NoArgs,
4141
Run: runInitCommand,
4242
}
43-
initCommand.Flags().BoolVar(&initFlags._default, "default", false,
44-
"If omitted, ask questions to the user about setting configuration properties, otherwise use default configuration.")
4543
initCommand.Flags().StringVar(&initFlags.location, "save-as", "",
4644
"Sets where to save the configuration file [default is ./arduino-cli.yaml].")
4745
return initCommand
@@ -55,13 +53,6 @@ var initFlags struct {
5553
func runInitCommand(cmd *cobra.Command, args []string) {
5654
logrus.Info("Executing `arduino config init`")
5755

58-
if !initFlags._default {
59-
if globals.OutputFormat != "text" {
60-
feedback.Error("The interactive mode is supported only in text mode.")
61-
os.Exit(errorcodes.ErrBadCall)
62-
}
63-
}
64-
6556
filepath := initFlags.location
6657
if filepath == "" {
6758
filepath = globals.Config.ConfigFile.String()

cli/core/search.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525

2626
"github.com/arduino/arduino-cli/cli/errorcodes"
2727
"github.com/arduino/arduino-cli/cli/feedback"
28-
"github.com/arduino/arduino-cli/cli/globals"
2928
"github.com/arduino/arduino-cli/cli/instance"
3029
"github.com/arduino/arduino-cli/commands/core"
3130
rpc "github.com/arduino/arduino-cli/rpc/commands"
@@ -51,11 +50,6 @@ func runSearchCommand(cmd *cobra.Command, args []string) {
5150
logrus.Info("Executing `arduino core search`")
5251

5352
arguments := strings.ToLower(strings.Join(args, " "))
54-
55-
if globals.OutputFormat != "json" {
56-
feedback.Printf("Searching for platforms matching '%s'", arguments)
57-
}
58-
5953
resp, err := core.PlatformSearch(context.Background(), &rpc.PlatformSearchReq{
6054
Instance: instance,
6155
SearchArgs: arguments,

cli/feedback/exported.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,6 @@ func Error(v ...interface{}) {
6868
fb.Error(v...)
6969
}
7070

71-
// PrintJSON is a convenient wrapper to provide feedback by printing the
72-
// desired output in a pretty JSON format. It adds a newline to the output.
73-
func PrintJSON(v interface{}) {
74-
fb.PrintJSON(v)
75-
}
76-
7771
// PrintResult is a convenient wrapper to provide feedback for complex data,
7872
// where the contents can't be just serialized to JSON but requires more
7973
// structure.

cli/feedback/feedback.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (fb *Feedback) Printf(format string, v ...interface{}) {
8888
// Print behaves like fmt.Print but writes on the out writer and adds a newline.
8989
func (fb *Feedback) Print(v interface{}) {
9090
if fb.format == JSON {
91-
fb.PrintJSON(v)
91+
fb.printJSON(v)
9292
} else {
9393
fmt.Fprintln(fb.out, v)
9494
}
@@ -107,9 +107,9 @@ func (fb *Feedback) Error(v ...interface{}) {
107107
logrus.Error(fmt.Sprint(v...))
108108
}
109109

110-
// PrintJSON is a convenient wrapper to provide feedback by printing the
110+
// printJSON is a convenient wrapper to provide feedback by printing the
111111
// desired output in a pretty JSON format. It adds a newline to the output.
112-
func (fb *Feedback) PrintJSON(v interface{}) {
112+
func (fb *Feedback) printJSON(v interface{}) {
113113
if d, err := json.MarshalIndent(v, "", " "); err != nil {
114114
fb.Errorf("Error during JSON encoding of the output: %v", err)
115115
} else {
@@ -122,7 +122,7 @@ func (fb *Feedback) PrintJSON(v interface{}) {
122122
// structure.
123123
func (fb *Feedback) PrintResult(res Result) {
124124
if fb.format == JSON {
125-
fb.PrintJSON(res.Data())
125+
fb.printJSON(res.Data())
126126
} else {
127127
fb.Print(fmt.Sprintf("%s", res))
128128
}

cli/globals/globals.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ import (
2929
var (
3030
// Debug determines whether to dump debug output to stderr or not
3131
Debug bool
32-
// OutputFormat can be "text" or "json"
33-
OutputFormat string
3432
// HTTPClientHeader is the object that will be propagated to configure the clients inside the downloaders
3533
HTTPClientHeader = getHTTPClientHeader()
3634
// VersionInfo contains all info injected during build

cli/output/rpc_progress.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,18 @@ package output
2020
import (
2121
"fmt"
2222

23-
"github.com/arduino/arduino-cli/cli/globals"
2423
"github.com/arduino/arduino-cli/commands"
2524
rpc "github.com/arduino/arduino-cli/rpc/commands"
2625
"github.com/cmaglie/pb"
2726
)
2827

28+
// OutputFormat can be "text" or "json"
29+
var OutputFormat string
30+
2931
// ProgressBar returns a DownloadProgressCB that prints a progress bar.
3032
// If JSON output format has been selected, the callback outputs nothing.
3133
func ProgressBar() commands.DownloadProgressCB {
32-
if globals.OutputFormat != "json" {
34+
if OutputFormat != "json" {
3335
return NewDownloadProgressBarCB()
3436
}
3537
return func(curr *rpc.DownloadProgress) {
@@ -40,7 +42,7 @@ func ProgressBar() commands.DownloadProgressCB {
4042
// TaskProgress returns a TaskProgressCB that prints the task progress.
4143
// If JSON output format has been selected, the callback outputs nothing.
4244
func TaskProgress() commands.TaskProgressCB {
43-
if globals.OutputFormat != "json" {
45+
if OutputFormat != "json" {
4446
return NewTaskProgressCB()
4547
}
4648
return func(curr *rpc.TaskProgress) {

0 commit comments

Comments
 (0)