Skip to content

Commit 42d2f71

Browse files
authored
[debug] Print exec time (#1461)
## Summary Enable this by setting `DEVBOX_PRINT_EXEC_TIME=1` ## How was it tested? ``` export DEVBOX_PRINT_EXEC_TIME=1 devbox run echo 1 ```
1 parent db56675 commit 42d2f71

File tree

7 files changed

+43
-20
lines changed

7 files changed

+43
-20
lines changed

internal/boxcli/midcobra/debug.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package midcobra
55

66
import (
77
"errors"
8-
"os"
98
"os/exec"
109
"strconv"
1110

@@ -15,7 +14,6 @@ import (
1514

1615
"go.jetpack.io/devbox/internal/boxcli/usererr"
1716
"go.jetpack.io/devbox/internal/debug"
18-
"go.jetpack.io/devbox/internal/envir"
1917
"go.jetpack.io/devbox/internal/telemetry"
2018
"go.jetpack.io/devbox/internal/ux"
2119
)
@@ -41,14 +39,11 @@ func (d *DebugMiddleware) preRun(cmd *cobra.Command, args []string) {
4139
return
4240
}
4341

44-
strVal := ""
4542
if d.flag.Changed {
46-
strVal = d.flag.Value.String()
47-
} else {
48-
strVal = os.Getenv(envir.DevboxDebug)
49-
}
50-
if enabled, _ := strconv.ParseBool(strVal); enabled {
51-
debug.Enable()
43+
strVal := d.flag.Value.String()
44+
if enabled, _ := strconv.ParseBool(strVal); enabled {
45+
debug.Enable()
46+
}
5247
}
5348
}
5449

internal/boxcli/root.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,11 @@ func Main() {
121121
return
122122
}
123123

124-
os.Exit(Execute(ctx, os.Args[1:]))
124+
code := Execute(ctx, os.Args[1:])
125+
// Run out here instead of as a middleware so we can capture any time we spend
126+
// in middlewares as well.
127+
debug.PrintExecutionTime()
128+
os.Exit(code)
125129
}
126130

127131
func listAllCommands(cmd *cobra.Command, indent string) {

internal/debug/debug.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@ import (
77
"fmt"
88
"io"
99
"log"
10+
"os"
1011
"runtime"
12+
"strconv"
1113

1214
"github.com/getsentry/sentry-go"
1315
"github.com/pkg/errors"
14-
15-
"go.jetpack.io/devbox/internal/envir"
1616
)
1717

18+
const DevboxDebug = "DEVBOX_DEBUG"
19+
1820
var enabled bool
1921

2022
func init() {
21-
enabled = envir.IsDevboxDebugEnabled()
23+
enabled, _ = strconv.ParseBool(os.Getenv(DevboxDebug))
2224
}
2325

2426
func IsEnabled() bool { return enabled }

internal/debug/time.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2023 Jetpack Technologies Inc and contributors. All rights reserved.
2+
// Use of this source code is governed by the license in the LICENSE file.
3+
4+
package debug
5+
6+
import (
7+
"fmt"
8+
"os"
9+
"strconv"
10+
"strings"
11+
"time"
12+
)
13+
14+
const devboxPrintExecTime = "DEVBOX_PRINT_EXEC_TIME"
15+
16+
var start = time.Now()
17+
18+
func PrintExecutionTime() {
19+
if enabled, _ := strconv.ParseBool(os.Getenv(devboxPrintExecTime)); !enabled {
20+
return
21+
}
22+
fmt.Fprintf(
23+
os.Stderr,
24+
"\"%s\" took %s\n", strings.Join(os.Args, " "),
25+
time.Since(start),
26+
)
27+
}

internal/envir/env.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package envir
55

66
const (
77
DevboxCache = "DEVBOX_CACHE"
8-
DevboxDebug = "DEVBOX_DEBUG"
98
DevboxFeaturePrefix = "DEVBOX_FEATURE_"
109
DevboxGateway = "DEVBOX_GATEWAY"
1110
// DevboxLatestVersion is the latest version available of the devbox CLI binary.

internal/envir/util.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ func DoNotTrack() bool {
2323
return doNotTrack
2424
}
2525

26-
func IsDevboxDebugEnabled() bool {
27-
enabled, _ := strconv.ParseBool(os.Getenv(DevboxDebug))
28-
return enabled
29-
}
30-
3126
func IsInBrowser() bool { // TODO: a better name
3227
inBrowser, _ := strconv.ParseBool(os.Getenv("START_WEB_TERMINAL"))
3328
return inBrowser

testscripts/testrunner/setupenv.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/rogpeppe/go-internal/testscript"
1010

11+
"go.jetpack.io/devbox/internal/debug"
1112
"go.jetpack.io/devbox/internal/envir"
1213
"go.jetpack.io/devbox/internal/xdg"
1314
)
@@ -22,7 +23,7 @@ func setupTestEnv(t *testing.T, envs *testscript.Env) error {
2223
return err
2324
}
2425

25-
envs.Setenv(envir.DevboxDebug, os.Getenv(envir.DevboxDebug))
26+
envs.Setenv(debug.DevboxDebug, os.Getenv(debug.DevboxDebug))
2627
return nil
2728
}
2829

0 commit comments

Comments
 (0)