Skip to content

Commit 5ba3bf2

Browse files
committed
Feedback and add way to read the current config.
1 parent 738fb98 commit 5ba3bf2

2 files changed

Lines changed: 43 additions & 18 deletions

File tree

cmd/jag/commands/config.go

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package commands
77
import (
88
"fmt"
99
"os"
10+
"strings"
1011
"time"
1112

1213
"github.com/spf13/cobra"
@@ -209,26 +210,50 @@ func ConfigIdentifyCmd() *cobra.Command {
209210
Args: cobra.NoArgs,
210211
}
211212

212-
cmd.AddCommand(
213-
&cobra.Command{
214-
Use: "timeout",
215-
Short: "Set the timeout for device identification",
216-
Args: cobra.ExactArgs(1),
217-
RunE: func(cmd *cobra.Command, args []string) error {
218-
cfg, err := directory.GetUserConfig()
219-
if err != nil {
220-
return err
213+
timeoutCmd := &cobra.Command{
214+
Use: "timeout [duration]",
215+
Short: "Set the timeout for device identification",
216+
Args: cobra.MaximumNArgs(1),
217+
RunE: func(cmd *cobra.Command, args []string) error {
218+
cfg, err := directory.GetUserConfig()
219+
if err != nil {
220+
return err
221+
}
222+
223+
clear, err := cmd.Flags().GetBool("clear")
224+
if err != nil {
225+
return err
226+
}
227+
228+
if clear {
229+
if len(args) > 0 {
230+
return fmt.Errorf("cannot use --clear with an argument")
221231
}
232+
if cfg.IsSet(IdentifyTimeoutCfgKey) {
233+
delete(cfg.Get(strings.Split(IdentifyTimeoutCfgKey, ".")[0]).(map[string]interface{}), strings.Split(IdentifyTimeoutCfgKey, ".")[1])
234+
}
235+
return directory.WriteConfig(cfg)
236+
}
222237

223-
timeout, err := time.ParseDuration(args[0])
224-
if err != nil {
225-
return fmt.Errorf("invalid duration: %w", err)
238+
if len(args) == 0 {
239+
if cfg.IsSet(IdentifyTimeoutCfgKey) {
240+
fmt.Println(cfg.GetString(IdentifyTimeoutCfgKey))
241+
} else {
242+
fmt.Printf("Default: %s\n", identifyTimeout)
226243
}
244+
return nil
245+
}
227246

228-
cfg.Set(IdentifyTimeoutCfgKey, timeout)
229-
return directory.WriteConfig(cfg)
230-
},
247+
timeout, err := time.ParseDuration(args[0])
248+
if err != nil {
249+
return fmt.Errorf("invalid duration: %w", err)
250+
}
251+
252+
cfg.Set(IdentifyTimeoutCfgKey, timeout.String())
253+
return directory.WriteConfig(cfg)
231254
},
232-
)
255+
}
256+
timeoutCmd.Flags().Bool("clear", false, "Clear the configuration")
257+
cmd.AddCommand(timeoutCmd)
233258
return cmd
234259
}

cmd/jag/commands/scan.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ func ScanCmd() *cobra.Command {
8080
}
8181

8282
identifyTimeout := identifyTimeout
83-
if cfg.IsSet(IdentifyTimeoutCfgKey) {
84-
timeout := cfg.GetString(IdentifyTimeoutCfgKey)
83+
if userCfg, err := directory.GetUserConfig(); err == nil && userCfg.IsSet(IdentifyTimeoutCfgKey) {
84+
timeout := userCfg.GetString(IdentifyTimeoutCfgKey)
8585
if d, err := time.ParseDuration(timeout); err == nil {
8686
identifyTimeout = d
8787
}

0 commit comments

Comments
 (0)