-
Notifications
You must be signed in to change notification settings - Fork 507
/
cmd_loglevel.go
85 lines (76 loc) · 2.5 KB
/
cmd_loglevel.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
75
76
77
78
79
80
81
82
83
84
85
package cli
import (
"errors"
"fmt"
"strings"
"time"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"google.golang.org/protobuf/types/known/durationpb"
"github.com/telepresenceio/telepresence/rpc/v2/connector"
"github.com/telepresenceio/telepresence/v2/pkg/client/cli/ann"
"github.com/telepresenceio/telepresence/v2/pkg/client/cli/util"
"github.com/telepresenceio/telepresence/v2/pkg/client/errcat"
)
const defaultDuration = 30 * time.Minute
type logLevelSetter struct {
duration time.Duration
localOnly bool
remoteOnly bool
}
func logLevelArg(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("accepts exactly one argument (the log level)")
}
lvl, err := logrus.ParseLevel(args[0])
if err != nil {
return err
}
switch lvl {
case logrus.PanicLevel, logrus.FatalLevel:
return fmt.Errorf("unsupported log level: %s", lvl)
}
return nil
}
func loglevelCommand() *cobra.Command {
lvs := logrus.AllLevels[2:] // Don't include `panic` and `fatal`
lvStrs := make([]string, len(lvs))
for i, lv := range lvs {
lvStrs[i] = lv.String()
}
lls := logLevelSetter{}
cmd := &cobra.Command{
Use: fmt.Sprintf("loglevel <%s>", strings.Join(lvStrs, ",")),
Args: logLevelArg,
Short: "Temporarily change the log-level of the traffic-manager, traffic-agent, and user and root daemons",
RunE: lls.setTempLogLevel,
ValidArgs: lvStrs,
Annotations: map[string]string{
ann.RootDaemon: ann.Required,
ann.Session: ann.Required,
},
}
flags := cmd.Flags()
flags.DurationVarP(&lls.duration, "duration", "d", defaultDuration, "The time that the log-level will be in effect (0s means indefinitely)")
flags.BoolVarP(&lls.localOnly, "local-only", "l", false, "Only affect the user and root daemons")
flags.BoolVarP(&lls.remoteOnly, "remote-only", "r", false, "Only affect the traffic-manager and traffic-agents")
return cmd
}
func (lls *logLevelSetter) setTempLogLevel(cmd *cobra.Command, args []string) error {
rq := &connector.LogLevelRequest{LogLevel: args[0], Duration: durationpb.New(lls.duration)}
switch {
case lls.localOnly && lls.remoteOnly:
return errcat.User.New("the local-only and remote-only options are mutually exclusive")
case lls.localOnly:
rq.Scope = connector.LogLevelRequest_LOCAL_ONLY
case lls.remoteOnly:
rq.Scope = connector.LogLevelRequest_REMOTE_ONLY
}
if err := util.InitCommand(cmd); err != nil {
return err
}
ctx := cmd.Context()
userD := util.GetUserDaemon(ctx)
_, err := userD.SetLogLevel(ctx, rq)
return err
}