Skip to content

Commit be99500

Browse files
add logging flag
1 parent 6e9ef67 commit be99500

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

cli.go

+32-2
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,26 @@ 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
34-
var fqbn string
35+
var (
36+
fqbn string
37+
logLevel string
38+
verbose bool
39+
portAddress string
40+
)
3541

3642
firmwareFlashCmd := &cobra.Command{
3743
Use: "flash",
@@ -117,6 +123,28 @@ func RunPlugin(plugin Plugin) {
117123
cli := &cobra.Command{
118124
Use: appName,
119125
Short: info.Name + " - This is an Arduino Firmware Uploader plugin.",
126+
PersistentPreRun: func(cmd *cobra.Command, args []string) {
127+
t, found := map[string]slog.Level{
128+
"trace": slog.LevelDebug,
129+
"debug": slog.LevelDebug,
130+
"info": slog.LevelInfo,
131+
"warn": slog.LevelWarn,
132+
"error": slog.LevelError,
133+
"fatal": slog.LevelError,
134+
"panic": slog.LevelError,
135+
}[logLevel]
136+
if !found {
137+
t = slog.LevelError
138+
}
139+
140+
var w io.Writer
141+
if !verbose {
142+
w = io.Discard
143+
} else {
144+
w = os.Stdout
145+
}
146+
slog.SetDefault(slog.New(slog.NewTextHandler(w, &slog.HandlerOptions{Level: t})))
147+
},
120148
}
121149
cli.AddCommand(firmwareCmd)
122150
cli.AddCommand(certCmd)
@@ -125,6 +153,8 @@ func RunPlugin(plugin Plugin) {
125153
// The fqbn is an optional flag that can be used by the plugin to do specific operations with a board.
126154
// With this input we can support a family of boards and not only a single one per plugin
127155
cli.PersistentFlags().StringVarP(&fqbn, "fqbn", "b", "", "Fully qualified board name")
156+
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")
157+
cli.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Print the logs on the standard output.")
128158

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

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/arduino/go-paths-helper v1.9.1
77
github.com/spf13/cobra v1.7.0
88
go.bug.st/relaxed-semver v0.11.0
9+
golang.org/x/exp v0.0.0-20230711153332-06a737ee72cb
910
gopkg.in/yaml.v3 v3.0.1
1011
)
1112

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)