/
debug.go
74 lines (68 loc) · 1.76 KB
/
debug.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* Copyright (c) 2021, 2023 Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
*/
package cmd
import (
"errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const (
setDebugMsg = "Debug is now set to "
setDebugError = "you can only specify 'on' or 'off'"
getDebugMsg = "Current debug level: "
setError = "you must provide either on or off"
off = "off"
on = "on"
)
// setDebugCmd represents the set debug command.
var setDebugCmd = &cobra.Command{
Use: "debug {on|off}}",
Short: "set debug messages to be on or off",
Long: `The 'set debug' command sets debug to on or off. If 'on' then additional
information is logged in the log file (cohctl.log).`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
displayErrorAndExit(cmd, setError)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
value := args[0]
var debug bool
if value == on {
debug = true
} else if value == off {
debug = false
} else {
return errors.New(setDebugError)
}
viper.Set(debugContextKey, debug)
err := WriteConfig()
if err != nil {
return err
}
cmd.Println(setDebugMsg + value)
return nil
},
}
// getDebugCmd represents the get debug command.
var getDebugCmd = &cobra.Command{
Use: "debug",
Short: "display the current debug level",
Long: `The 'get debug' command displays the current debug level. If 'on' then
additional information is logged in the log file.`,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
var debug string
if Config.Debug {
debug = on
} else {
debug = off
}
cmd.Printf("%s%v\n", getDebugMsg, debug)
return nil
},
}