This repository has been archived by the owner on Mar 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
debug.go
79 lines (68 loc) · 1.83 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
75
76
77
78
79
// Copyright 2022 Jetpack Technologies Inc and contributors. All rights reserved.
// Use of this source code is governed by the license in the LICENSE file.
package midcobra
import (
"errors"
"os"
"os/exec"
"strconv"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/ublue-os/fleek/fin"
"github.com/ublue-os/fleek/internal/debug"
"github.com/ublue-os/fleek/internal/fleekcli/usererr"
"github.com/ublue-os/fleek/internal/ux"
)
type DebugMiddleware struct {
executionID string // uuid
flag *pflag.Flag
}
var _ Middleware = (*DebugMiddleware)(nil)
func (d *DebugMiddleware) AttachToFlag(flags *pflag.FlagSet, flagName string) {
flags.Bool(
flagName,
false,
"Show full stack traces on errors",
)
d.flag = flags.Lookup(flagName)
d.flag.Hidden = true
}
func (d *DebugMiddleware) preRun(_ *cobra.Command, _ []string) {
if d == nil {
return
}
strVal := ""
if d.flag.Changed {
strVal = d.flag.Value.String()
} else {
strVal = os.Getenv("FLEEK_DEBUG")
}
if enabled, _ := strconv.ParseBool(strVal); enabled {
debug.Enable()
}
}
func (d *DebugMiddleware) postRun(cmd *cobra.Command, _ []string, runErr error) {
if runErr == nil {
return
}
if usererr.HasUserMessage(runErr) {
if usererr.IsWarning(runErr) {
ux.Fwarning(cmd.ErrOrStderr(), runErr.Error())
return
}
color.New(color.FgRed).Fprintf(cmd.ErrOrStderr(), "\nError: %s\n\n", runErr.Error())
} else {
color.New(color.FgRed).Fprintf(cmd.ErrOrStderr(), "Error: %v\n\n", runErr)
}
st := debug.EarliestStackTrace(runErr)
var exitErr *exec.ExitError
if errors.As(runErr, &exitErr) {
fin.Debug.Printfln("Command stderr: %s\n", exitErr.Stderr)
}
fin.Debug.Printfln("\nExecutionID:%s\n%+v\n", d.executionID, st)
}
func (d *DebugMiddleware) withExecutionID(execID string) Middleware {
d.executionID = execID
return d
}