Skip to content

Commit b2a7f62

Browse files
committed
Add pretty debug logging
1 parent ccb86b1 commit b2a7f62

File tree

9 files changed

+67
-71
lines changed

9 files changed

+67
-71
lines changed

cmd/cmd.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"os"
6+
)
7+
8+
func Execute() {
9+
if err := rootCmd.ExecuteContext(context.Background()); err != nil {
10+
os.Exit(1)
11+
}
12+
}

cmd/concurrently.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"os"
77

88
"github.com/jclem/konk/konk"
9-
"github.com/jclem/konk/konk/debugger"
109
"github.com/spf13/cobra"
1110
)
1211

@@ -33,8 +32,11 @@ konk run concurrently -g -n lint -n test
3332
3433
konk run concurrently -bgcL -n "check:*"`,
3534
RunE: func(cmd *cobra.Command, args []string) error {
36-
dbg := debugger.Get(cmd.Context())
37-
dbg.Flags(cmd)
35+
ctx := cmd.Context()
36+
37+
if debug {
38+
cmd.DebugFlags()
39+
}
3840

3941
if workingDirectory != "" {
4042
if err := os.Chdir(workingDirectory); err != nil {
@@ -53,7 +55,7 @@ konk run concurrently -bgcL -n "check:*"`,
5355

5456
labels := collectLabels(cmdStrings)
5557

56-
commands, err := konk.RunConcurrently(cmd.Context(), konk.RunConcurrentlyConfig{
58+
commands, err := konk.RunConcurrently(ctx, konk.RunConcurrentlyConfig{
5759
Commands: cmdParts,
5860
Labels: labels,
5961
Env: make([]string, 0),
@@ -64,9 +66,7 @@ konk run concurrently -bgcL -n "check:*"`,
6466
NoShell: noShell,
6567
})
6668

67-
if commands != nil {
68-
dbg.Prettyln(commands)
69-
}
69+
debugCommands(ctx, commands)
7070

7171
if err != nil {
7272
return fmt.Errorf("running commands: %w", err)

cmd/proc.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"strings"
88

99
"github.com/jclem/konk/konk"
10-
"github.com/jclem/konk/konk/debugger"
1110
"github.com/spf13/cobra"
1211
)
1312

@@ -21,8 +20,11 @@ var procCommand = cobra.Command{
2120
Aliases: []string{"p"},
2221
Short: "Run commands defined in a Procfile (alias: p)",
2322
RunE: func(cmd *cobra.Command, _ []string) error {
24-
dbg := debugger.Get(cmd.Context())
25-
dbg.Flags(cmd)
23+
ctx := cmd.Context()
24+
25+
if debug {
26+
cmd.DebugFlags()
27+
}
2628

2729
if workingDirectory != "" {
2830
if err := os.Chdir(workingDirectory); err != nil {
@@ -85,7 +87,7 @@ var procCommand = cobra.Command{
8587
}
8688
}
8789

88-
commands, err := konk.RunConcurrently(cmd.Context(), konk.RunConcurrentlyConfig{
90+
commands, err := konk.RunConcurrently(ctx, konk.RunConcurrentlyConfig{
8991
Commands: commandStrings,
9092
Labels: commandLabels,
9193
Env: envLines,
@@ -96,9 +98,7 @@ var procCommand = cobra.Command{
9698
NoShell: noShell,
9799
})
98100

99-
if commands != nil {
100-
dbg.Prettyln(commands)
101-
}
101+
debugCommands(ctx, commands)
102102

103103
if err != nil {
104104
return fmt.Errorf("running commands: %w", err)

cmd/root.go

+22-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ package cmd
22

33
import (
44
"context"
5+
"log/slog"
56
"os"
7+
"strconv"
68

7-
"github.com/jclem/konk/konk/debugger"
9+
"github.com/golang-cz/devslog"
10+
"github.com/jclem/konk/konk"
811
"github.com/spf13/cobra"
912
)
1013

@@ -26,14 +29,28 @@ var rootCmd = &cobra.Command{
2629
// Ensures that usage isn't printed for errors such as non-zero exits.
2730
// SEE: https://github.com/spf13/cobra/issues/340#issuecomment-378726225
2831
cmd.SilenceUsage = true
32+
33+
level := slog.LevelInfo
34+
if debug {
35+
level = slog.LevelDebug
36+
}
37+
38+
slog.SetDefault(slog.New(devslog.NewHandler(os.Stdout, &devslog.Options{ //nolint:exhaustruct // Fields not needed.
39+
HandlerOptions: &slog.HandlerOptions{Level: level}, //nolint:exhaustruct // Fields not needed.
40+
})))
2941
},
3042
}
3143

32-
func Execute() {
44+
func init() {
3345
rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "D", false, "debug mode")
34-
ctx := debugger.WithDebugger(context.Background(), &debug)
46+
}
3547

36-
if err := rootCmd.ExecuteContext(ctx); err != nil {
37-
os.Exit(1)
48+
func debugCommands(ctx context.Context, commands []*konk.Command) {
49+
if commands != nil {
50+
var attrs []any
51+
for i, c := range commands {
52+
attrs = append(attrs, slog.Any(strconv.Itoa(i), c))
53+
}
54+
slog.DebugContext(ctx, "commands", attrs...)
3855
}
3956
}

cmd/serially.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"os"
88

99
"github.com/jclem/konk/konk"
10-
"github.com/jclem/konk/konk/debugger"
1110
"github.com/mattn/go-shellwords"
1211
"github.com/spf13/cobra"
1312
)
@@ -24,8 +23,11 @@ konk run serially "echo foo" "echo bar"
2423
2524
konk run serially -n build -n deploy`,
2625
RunE: func(cmd *cobra.Command, args []string) error {
27-
dbg := debugger.Get(cmd.Context())
28-
dbg.Flags(cmd)
26+
ctx := cmd.Context()
27+
28+
if debug {
29+
cmd.DebugFlags()
30+
}
2931

3032
if workingDirectory != "" {
3133
if err := os.Chdir(workingDirectory); err != nil {
@@ -79,7 +81,7 @@ konk run serially -n build -n deploy`,
7981
commands[i] = c
8082
}
8183

82-
dbg.Prettyln(commands)
84+
debugCommands(ctx, commands)
8385

8486
for _, c := range commands {
8587
ctx, cancel := context.WithCancel(context.Background())

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require github.com/spf13/cobra v1.8.1
77
require (
88
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
99
github.com/davecgh/go-spew v1.1.1 // indirect
10+
github.com/golang-cz/devslog v0.0.11 // indirect
1011
github.com/kr/text v0.2.0 // indirect
1112
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
1213
github.com/mattn/go-isatty v0.0.14 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
77
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
88
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
99
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
10+
github.com/golang-cz/devslog v0.0.11 h1:v4Yb9o0ZpuZ/D8ZrtVw1f9q5XrjnkxwHF1XmWwO8IHg=
11+
github.com/golang-cz/devslog v0.0.11/go.mod h1:bSe5bm0A7Nyfqtijf1OMNgVJHlWEuVSXnkuASiE1vV8=
1012
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
1113
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
1214
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=

konk/command.go

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"errors"
77
"fmt"
8+
"log/slog"
89
"math/rand"
910
"os"
1011
"os/exec"
@@ -21,6 +22,15 @@ type Command struct {
2122
prefix string
2223
}
2324

25+
var _ slog.LogValuer = (*Command)(nil)
26+
27+
func (c *Command) LogValue() slog.Value {
28+
return slog.GroupValue(
29+
slog.String("command", c.cmd.String()),
30+
slog.String("prefix", c.prefix),
31+
)
32+
}
33+
2434
type RunCommandConfig struct {
2535
AggregateOutput bool
2636
StopOnCancel bool

konk/debugger/debugger.go

-48
This file was deleted.

0 commit comments

Comments
 (0)