@@ -7,6 +7,7 @@ package commands
77import (
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}
0 commit comments