Skip to content

Commit afd6401

Browse files
add logging flag
1 parent 13fe018 commit afd6401

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

cli.go

+31-1
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,25 @@ package helper
1818

1919
import (
2020
"fmt"
21+
"io"
2122
"os"
2223
"path/filepath"
2324

2425
"github.com/arduino/go-paths-helper"
2526
"github.com/spf13/cobra"
27+
"golang.org/x/exp/slog"
2628
"gopkg.in/yaml.v3"
2729
)
2830

2931
// RunPlugin runs the given plugin
3032
func RunPlugin(plugin Plugin) {
3133
info := plugin.GetPluginInfo()
3234

33-
var portAddress string
35+
var (
36+
portAddress string
37+
logLevel string
38+
verbose bool
39+
)
3440

3541
firmwareFlashCmd := &cobra.Command{
3642
Use: "flash",
@@ -113,11 +119,35 @@ func RunPlugin(plugin Plugin) {
113119
cli := &cobra.Command{
114120
Use: appName,
115121
Short: info.Name + " - This is an Arduino Firmware Uploader plugin.",
122+
PersistentPreRun: func(cmd *cobra.Command, args []string) {
123+
t, found := map[string]slog.Level{
124+
"trace": slog.LevelDebug,
125+
"debug": slog.LevelDebug,
126+
"info": slog.LevelInfo,
127+
"warn": slog.LevelWarn,
128+
"error": slog.LevelError,
129+
"fatal": slog.LevelError,
130+
"panic": slog.LevelError,
131+
}[logLevel]
132+
if !found {
133+
t = slog.LevelError
134+
}
135+
136+
var w io.Writer
137+
if !verbose {
138+
w = io.Discard
139+
} else {
140+
w = os.Stdout
141+
}
142+
slog.SetDefault(slog.New(slog.NewTextHandler(w, &slog.HandlerOptions{Level: t})))
143+
},
116144
}
117145
cli.AddCommand(firmwareCmd)
118146
cli.AddCommand(certCmd)
119147
cli.AddCommand(versionCmd)
120148
cli.PersistentFlags().StringVarP(&portAddress, "address", "p", "", "Port address")
149+
cli.PersistentFlags().StringVar(&logLevel, "log-level", "info", "Messages with this level and above will be logged. Valid levels are: trace, debug, info, warn, error, fatal, panic")
150+
cli.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Print the logs on the standard output.")
121151

122152
if err := cli.Execute(); err != nil {
123153
fatal(err.Error(), 1)

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ require (
1313
github.com/inconshreveable/mousetrap v1.1.0 // indirect
1414
github.com/pkg/errors v0.9.1 // indirect
1515
github.com/spf13/pflag v1.0.5 // indirect
16+
golang.org/x/exp v0.0.0-20230711153332-06a737ee72cb // indirect
1617
)

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
1919
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
2020
go.bug.st/relaxed-semver v0.11.0 h1:ngzpUlBEZ5F9hJnMZP55LIFbgX3bCztBBufMhJViAsY=
2121
go.bug.st/relaxed-semver v0.11.0/go.mod h1:rqPEm+790OTQlAdfSJSHWwpKOg3A8UyvAWMZxYkQivc=
22+
golang.org/x/exp v0.0.0-20230711153332-06a737ee72cb h1:xIApU0ow1zwMa2uL1VDNeQlNVFTWMQxZUZCMDy0Q4Us=
23+
golang.org/x/exp v0.0.0-20230711153332-06a737ee72cb/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
2224
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2325
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2426
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

0 commit comments

Comments
 (0)