Skip to content

Commit 0186494

Browse files
authored
Merge pull request #5044 from camilamacedo86/fix-logs-alpha-commands-cli
🌱 (fix): Fix inconsistent logging format ( Follow up: #4968 )
2 parents efc7fb5 + 8aca76b commit 0186494

File tree

6 files changed

+39
-11
lines changed

6 files changed

+39
-11
lines changed

cmd/cmd.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package cmd
1818

1919
import (
20-
"log"
2120
"log/slog"
2221
"os"
2322

@@ -83,9 +82,11 @@ func Run() {
8382
cli.WithCompletion(),
8483
)
8584
if err != nil {
86-
log.Fatal(err)
85+
slog.Error("failed to create CLI", "error", err)
86+
os.Exit(1)
8787
}
8888
if err := c.Run(); err != nil {
89-
log.Fatal(err)
89+
slog.Error("CLI run failed", "error", err)
90+
os.Exit(1)
9091
}
9192
}

pkg/cli/alpha/generate.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,27 @@ limitations under the License.
1414
package alpha
1515

1616
import (
17-
"log"
17+
"log/slog"
18+
"os"
1819

1920
"github.com/spf13/cobra"
2021

2122
"sigs.k8s.io/kubebuilder/v4/pkg/cli/alpha/internal"
23+
"sigs.k8s.io/kubebuilder/v4/pkg/logging"
2224
)
2325

26+
func init() {
27+
// Initialize consistent logging for alpha commands
28+
opts := logging.HandlerOptions{
29+
SlogOpts: slog.HandlerOptions{
30+
Level: slog.LevelInfo,
31+
},
32+
}
33+
handler := logging.NewHandler(os.Stdout, opts)
34+
logger := slog.New(handler)
35+
slog.SetDefault(logger)
36+
}
37+
2438
// NewScaffoldCommand returns a new scaffold command, providing the `kubebuilder alpha generate`
2539
// feature to re-scaffold projects and assist users with updates.
2640
//
@@ -62,7 +76,8 @@ If no output directory is provided, the current working directory will be cleane
6276
},
6377
Run: func(_ *cobra.Command, _ []string) {
6478
if err := opts.Generate(); err != nil {
65-
log.Fatalf("failed to generate project: %s", err)
79+
slog.Error("failed to generate project", "error", err)
80+
os.Exit(1)
6681
}
6782
},
6883
}

pkg/cli/alpha/internal/generate.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ func (opts *Generate) Generate() error {
127127
// This is to avoid blocking the migration flow due to non-critical issues during setup.
128128
targets := []string{"manifests", "generate", "fmt", "vet", "lint-fix"}
129129
for _, target := range targets {
130-
log.Info("Running make target", "target", target)
131130
err := util.RunCmd(fmt.Sprintf("Running make %s", target), "make", target)
132131
if err != nil {
133132
log.Warn("make target failed", "target", target, "error", err)

pkg/cli/alpha/internal/update.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ func (opts *Update) cleanUpAncestorBranch() error {
212212
func runMakeTargets() error {
213213
targets := []string{"manifests", "generate", "fmt", "vet", "lint-fix"}
214214
for _, target := range targets {
215-
log.Info("Running make command", "target", target)
216215
err := util.RunCmd(fmt.Sprintf("Running make %s", target), "make", target)
217216
if err != nil {
218217
return fmt.Errorf("make %s failed: %v", target, err)

pkg/cli/alpha/update.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,27 @@ package alpha
1818

1919
import (
2020
"fmt"
21-
"log"
21+
"log/slog"
22+
"os"
2223

2324
"github.com/spf13/cobra"
2425

2526
"sigs.k8s.io/kubebuilder/v4/pkg/cli/alpha/internal/update"
27+
"sigs.k8s.io/kubebuilder/v4/pkg/logging"
2628
)
2729

30+
func init() {
31+
// Initialize consistent logging for alpha commands
32+
opts := logging.HandlerOptions{
33+
SlogOpts: slog.HandlerOptions{
34+
Level: slog.LevelInfo,
35+
},
36+
}
37+
handler := logging.NewHandler(os.Stdout, opts)
38+
logger := slog.New(handler)
39+
slog.SetDefault(logger)
40+
}
41+
2842
// NewUpdateCommand creates and returns a new Cobra command for updating Kubebuilder projects.
2943
func NewUpdateCommand() *cobra.Command {
3044
opts := update.Update{}
@@ -123,7 +137,8 @@ Defaults:
123137
},
124138
Run: func(_ *cobra.Command, _ []string) {
125139
if err := opts.Update(); err != nil {
126-
log.Fatalf("Update failed: %s", err)
140+
slog.Error("Update failed", "error", err)
141+
os.Exit(1)
127142
}
128143
},
129144
}

pkg/plugin/util/exec.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,14 @@ import (
2121
log "log/slog"
2222
"os"
2323
"os/exec"
24-
"strings"
2524
)
2625

2726
// RunCmd prints the provided message and command and then executes it binding stdout and stderr
2827
func RunCmd(msg, cmd string, args ...string) error {
2928
c := exec.Command(cmd, args...) //nolint:gosec
3029
c.Stdout = os.Stdout
3130
c.Stderr = os.Stderr
32-
log.Info(msg, "command", strings.Join(c.Args, " "))
31+
log.Info(msg)
3332

3433
if err := c.Run(); err != nil {
3534
return fmt.Errorf("error running %q: %w", cmd, err)

0 commit comments

Comments
 (0)