@@ -18,20 +18,26 @@ package helper
18
18
19
19
import (
20
20
"fmt"
21
+ "io"
21
22
"os"
22
23
"path/filepath"
23
24
24
25
"github.com/arduino/go-paths-helper"
25
26
"github.com/spf13/cobra"
27
+ "golang.org/x/exp/slog"
26
28
"gopkg.in/yaml.v3"
27
29
)
28
30
29
31
// RunPlugin runs the given plugin
30
32
func RunPlugin (plugin Plugin ) {
31
33
info := plugin .GetPluginInfo ()
32
34
33
- var portAddress string
34
- var fqbn string
35
+ var (
36
+ fqbn string
37
+ logLevel string
38
+ verbose bool
39
+ portAddress string
40
+ )
35
41
36
42
firmwareFlashCmd := & cobra.Command {
37
43
Use : "flash" ,
@@ -117,6 +123,28 @@ func RunPlugin(plugin Plugin) {
117
123
cli := & cobra.Command {
118
124
Use : appName ,
119
125
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
+ },
120
148
}
121
149
cli .AddCommand (firmwareCmd )
122
150
cli .AddCommand (certCmd )
@@ -125,6 +153,8 @@ func RunPlugin(plugin Plugin) {
125
153
// The fqbn is an optional flag that can be used by the plugin to do specific operations with a board.
126
154
// With this input we can support a family of boards and not only a single one per plugin
127
155
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." )
128
158
129
159
if err := cli .Execute (); err != nil {
130
160
fatal (err .Error (), 1 )
0 commit comments