Skip to content

Commit 47a2fff

Browse files
committed
Split daemon mode configs from core configs
1 parent edc63f8 commit 47a2fff

17 files changed

+643
-216
lines changed

.github/workflows/check-go-task.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Check Go
33

44
env:
55
# See: https://github.com/actions/setup-go/tree/v2#readme
6-
GO_VERSION: "1.16"
6+
GO_VERSION: "1.17"
77

88
# See: https://docs.github.com/en/actions/reference/events-that-trigger-workflows
99
on:

.github/workflows/test-go-task.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Test Go
33

44
env:
55
# See: https://github.com/actions/setup-go/tree/v2#readme
6-
GO_VERSION: "1.16"
6+
GO_VERSION: "1.17"
77

88
# See: https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows
99
on:

cli/arguments/logging.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package arguments
17+
18+
import (
19+
"strings"
20+
21+
"github.com/spf13/cobra"
22+
"github.com/spf13/pflag"
23+
)
24+
25+
var (
26+
validLogLevels = []string{"trace", "debug", "info", "warn", "error", "fatal", "panic"}
27+
validLogFormats = []string{"text", "json"}
28+
)
29+
30+
// AddLoggingFlags adds the flags used to set logging level, file and format to the specified FlagSet
31+
func AddLoggingFlags(flags *pflag.FlagSet) {
32+
flags.String("log-level", "info", tr("Messages with this level and above will be logged. Valid levels are: %s", strings.Join(validLogLevels, ", ")))
33+
flags.String("log-file", "", tr("Path to the file where logs will be written."))
34+
flags.String("log-format", "text", tr("The output format for the logs, can be: %s", strings.Join(validLogFormats, ", ")))
35+
}
36+
37+
// AddLoggingCompletion adds completion function for logging flags to the specified Command
38+
func AddLoggingCompletion(cmd *cobra.Command) {
39+
cmd.RegisterFlagCompletionFunc("log-level", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
40+
return validLogLevels, cobra.ShellCompDirectiveDefault
41+
})
42+
cmd.RegisterFlagCompletionFunc("log-format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
43+
return validLogFormats, cobra.ShellCompDirectiveDefault
44+
})
45+
}

cli/arguments/output.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package arguments
17+
18+
import (
19+
"strings"
20+
21+
"github.com/spf13/cobra"
22+
"github.com/spf13/pflag"
23+
)
24+
25+
var validOutputFormats = []string{"text", "json", "jsonmini", "yaml"}
26+
27+
// AddOutputFlags adds the flags used to set verbosity, colored output and its format to the specified FlagSet.
28+
func AddOutputFlags(flags *pflag.FlagSet) {
29+
flags.BoolP("verbose", "v", false, tr("Print the logs on the standard output."))
30+
flags.String("format", "text", tr("The output format for the logs, can be: %s", strings.Join(validOutputFormats, ", ")))
31+
flags.Bool("no-color", false, "Disable colored output.")
32+
}
33+
34+
// AddOutputCompletion adds completion functions for output format to the specified Command.
35+
func AddOutputCompletion(cmd *cobra.Command) {
36+
cmd.RegisterFlagCompletionFunc("format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
37+
return validOutputFormats, cobra.ShellCompDirectiveDefault
38+
})
39+
}

cli/cli.go

+41-105
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ package cli
1717

1818
import (
1919
"fmt"
20-
"io/ioutil"
2120
"os"
2221
"strings"
2322

23+
"github.com/arduino/arduino-cli/cli/arguments"
2424
"github.com/arduino/arduino-cli/cli/board"
2525
"github.com/arduino/arduino-cli/cli/burnbootloader"
2626
"github.com/arduino/arduino-cli/cli/cache"
@@ -47,17 +47,15 @@ import (
4747
"github.com/arduino/arduino-cli/configuration"
4848
"github.com/arduino/arduino-cli/i18n"
4949
"github.com/arduino/arduino-cli/inventory"
50+
"github.com/arduino/arduino-cli/logging"
5051
"github.com/fatih/color"
5152
"github.com/mattn/go-colorable"
52-
"github.com/rifflock/lfshook"
5353
"github.com/sirupsen/logrus"
5454
"github.com/spf13/cobra"
5555
semver "go.bug.st/relaxed-semver"
5656
)
5757

5858
var (
59-
verbose bool
60-
outputFormat string
6159
configFile string
6260
updaterMessageChan chan *semver.Version = make(chan *semver.Version)
6361
)
@@ -104,45 +102,15 @@ func createCliCommandTree(cmd *cobra.Command) {
104102
cmd.AddCommand(burnbootloader.NewCommand())
105103
cmd.AddCommand(version.NewCommand())
106104

107-
cmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, tr("Print the logs on the standard output."))
108-
validLogLevels := []string{"trace", "debug", "info", "warn", "error", "fatal", "panic"}
109-
cmd.PersistentFlags().String("log-level", "", tr("Messages with this level and above will be logged. Valid levels are: %s", strings.Join(validLogLevels, ", ")))
110-
cmd.RegisterFlagCompletionFunc("log-level", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
111-
return validLogLevels, cobra.ShellCompDirectiveDefault
112-
})
113-
cmd.PersistentFlags().String("log-file", "", tr("Path to the file where logs will be written."))
114-
validLogFormats := []string{"text", "json"}
115-
cmd.PersistentFlags().String("log-format", "", tr("The output format for the logs, can be: %s", strings.Join(validLogFormats, ", ")))
116-
cmd.RegisterFlagCompletionFunc("log-format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
117-
return validLogFormats, cobra.ShellCompDirectiveDefault
118-
})
119-
validOutputFormats := []string{"text", "json", "jsonmini", "yaml"}
120-
cmd.PersistentFlags().StringVar(&outputFormat, "format", "text", tr("The output format for the logs, can be: %s", strings.Join(validOutputFormats, ", ")))
121-
cmd.RegisterFlagCompletionFunc("format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
122-
return validOutputFormats, cobra.ShellCompDirectiveDefault
123-
})
105+
arguments.AddLoggingFlags(cmd.PersistentFlags())
106+
arguments.AddLoggingCompletion(cmd)
107+
arguments.AddOutputFlags(cmd.PersistentFlags())
108+
arguments.AddOutputCompletion(cmd)
124109
cmd.PersistentFlags().StringVar(&configFile, "config-file", "", tr("The custom config file (if not specified the default will be used)."))
125110
cmd.PersistentFlags().StringSlice("additional-urls", []string{}, tr("Comma-separated list of additional URLs for the Boards Manager."))
126-
cmd.PersistentFlags().Bool("no-color", false, "Disable colored output.")
127111
configuration.BindFlags(cmd, configuration.Settings)
128112
}
129113

130-
// convert the string passed to the `--log-level` option to the corresponding
131-
// logrus formal level.
132-
func toLogLevel(s string) (t logrus.Level, found bool) {
133-
t, found = map[string]logrus.Level{
134-
"trace": logrus.TraceLevel,
135-
"debug": logrus.DebugLevel,
136-
"info": logrus.InfoLevel,
137-
"warn": logrus.WarnLevel,
138-
"error": logrus.ErrorLevel,
139-
"fatal": logrus.FatalLevel,
140-
"panic": logrus.PanicLevel,
141-
}[s]
142-
143-
return
144-
}
145-
146114
func parseFormatString(arg string) (feedback.OutputFormat, bool) {
147115
f, found := map[string]feedback.OutputFormat{
148116
"json": feedback.JSON,
@@ -155,10 +123,33 @@ func parseFormatString(arg string) (feedback.OutputFormat, bool) {
155123
}
156124

157125
func preRun(cmd *cobra.Command, args []string) {
126+
// Set feedback format output
127+
outputFormat, err := cmd.Flags().GetString("format")
128+
if err != nil {
129+
feedback.Errorf(tr("Error getting flag value: %s", err))
130+
os.Exit(errorcodes.ErrBadCall)
131+
}
132+
outputFormat = strings.ToLower(outputFormat)
133+
output.OutputFormat = outputFormat
134+
format, found := parseFormatString(outputFormat)
135+
if !found {
136+
feedback.Errorf(tr("Invalid output format: %s"), outputFormat)
137+
os.Exit(errorcodes.ErrBadCall)
138+
}
139+
feedback.SetFormat(format)
140+
141+
// Set default feedback output to colorable
142+
feedback.SetOut(colorable.NewColorableStdout())
143+
feedback.SetErr(colorable.NewColorableStderr())
144+
145+
if cmd.Name() == "daemon" {
146+
return
147+
}
148+
158149
configFile := configuration.Settings.ConfigFileUsed()
159150

160151
// initialize inventory
161-
err := inventory.Init(configuration.Settings.GetString("directories.Data"))
152+
err = inventory.Init(configuration.Settings.GetString("directories.Data"))
162153
if err != nil {
163154
feedback.Errorf("Error: %v", err)
164155
os.Exit(errorcodes.ErrBadArgument)
@@ -167,10 +158,6 @@ func preRun(cmd *cobra.Command, args []string) {
167158
// https://no-color.org/
168159
color.NoColor = configuration.Settings.GetBool("output.no_color") || os.Getenv("NO_COLOR") != ""
169160

170-
// Set default feedback output to colorable
171-
feedback.SetOut(colorable.NewColorableStdout())
172-
feedback.SetErr(colorable.NewColorableStderr())
173-
174161
updaterMessageChan = make(chan *semver.Version)
175162
go func() {
176163
if cmd.Name() == "version" {
@@ -185,70 +172,19 @@ func preRun(cmd *cobra.Command, args []string) {
185172
updaterMessageChan <- updater.CheckForUpdate(currentVersion)
186173
}()
187174

188-
//
189-
// Prepare logging
190-
//
191-
192-
// decide whether we should log to stdout
193-
if verbose {
194-
// if we print on stdout, do it in full colors
195-
logrus.SetOutput(colorable.NewColorableStdout())
196-
logrus.SetFormatter(&logrus.TextFormatter{
197-
ForceColors: true,
198-
DisableColors: color.NoColor,
199-
})
200-
} else {
201-
logrus.SetOutput(ioutil.Discard)
202-
}
203-
204-
// set the Logger format
205-
logFormat := strings.ToLower(configuration.Settings.GetString("logging.format"))
206-
if logFormat == "json" {
207-
logrus.SetFormatter(&logrus.JSONFormatter{})
208-
}
209-
210-
// should we log to file?
211-
logFile := configuration.Settings.GetString("logging.file")
212-
if logFile != "" {
213-
file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
214-
if err != nil {
215-
fmt.Println(tr("Unable to open file for logging: %s", logFile))
216-
os.Exit(errorcodes.ErrBadCall)
217-
}
218-
219-
// we use a hook so we don't get color codes in the log file
220-
if logFormat == "json" {
221-
logrus.AddHook(lfshook.NewHook(file, &logrus.JSONFormatter{}))
222-
} else {
223-
logrus.AddHook(lfshook.NewHook(file, &logrus.TextFormatter{}))
224-
}
225-
}
226-
227-
// configure logging filter
228-
if lvl, found := toLogLevel(configuration.Settings.GetString("logging.level")); !found {
229-
feedback.Errorf(tr("Invalid option for --log-level: %s"), configuration.Settings.GetString("logging.level"))
230-
os.Exit(errorcodes.ErrBadArgument)
231-
} else {
232-
logrus.SetLevel(lvl)
233-
}
234-
235-
//
236-
// Prepare the Feedback system
237-
//
238-
239-
// normalize the format strings
240-
outputFormat = strings.ToLower(outputFormat)
241-
// configure the output package
242-
output.OutputFormat = outputFormat
243-
// check the right output format was passed
244-
format, found := parseFormatString(outputFormat)
245-
if !found {
246-
feedback.Errorf(tr("Invalid output format: %s"), outputFormat)
175+
// Setups logging if necessary
176+
verbose, err := cmd.Flags().GetBool("verbose")
177+
if err != nil {
178+
feedback.Errorf(tr("Error getting flag value: %s", err))
247179
os.Exit(errorcodes.ErrBadCall)
248180
}
249-
250-
// use the output format to configure the Feedback
251-
feedback.SetFormat(format)
181+
logging.Setup(
182+
verbose,
183+
color.NoColor,
184+
configuration.Settings.GetString("logging.level"),
185+
configuration.Settings.GetString("logging.file"),
186+
configuration.Settings.GetString("logging.format"),
187+
)
252188

253189
//
254190
// Print some status info and check command is consistent

cli/config/validate.go

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626

2727
var validMap = map[string]reflect.Kind{
2828
"board_manager.additional_urls": reflect.Slice,
29-
"daemon.port": reflect.String,
3029
"directories.data": reflect.String,
3130
"directories.downloads": reflect.String,
3231
"directories.user": reflect.String,

0 commit comments

Comments
 (0)