• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kubernetes-sigs / kubebuilder / 17187150698

24 Aug 2025 09:45AM UTC coverage: 70.639%. First build
17187150698

Pull #5044

github

camilamacedo86
(fix): log format using slog
Pull Request #5044: WIP (fix): log format using slog

23 of 27 new or added lines in 3 files covered. (85.19%)

3118 of 4414 relevant lines covered (70.64%)

13.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.0
/pkg/cli/alpha/generate.go
1
/*
2
Copyright 2023 The Kubernetes Authors.
3
Licensed under the Apache License, Version 2.0 (the "License");
4
you may not use this file except in compliance with the License.
5
You may obtain a copy of the License at
6
        http://www.apache.org/licenses/LICENSE-2.0
7
Unless required by applicable law or agreed to in writing, software
8
distributed under the License is distributed on an "AS IS" BASIS,
9
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
See the License for the specific language governing permissions and
11
limitations under the License.
12
*/
13

14
package alpha
15

16
import (
17
        "log/slog"
18
        "os"
19

20
        "github.com/spf13/cobra"
21

22
        "sigs.k8s.io/kubebuilder/v4/pkg/cli/alpha/internal"
23
        "sigs.k8s.io/kubebuilder/v4/pkg/logging"
24
)
25

26
func init() {
2✔
27
        // Initialize consistent logging for alpha commands
2✔
28
        opts := logging.HandlerOptions{
2✔
29
                SlogOpts: slog.HandlerOptions{
2✔
30
                        Level: slog.LevelInfo,
2✔
31
                },
2✔
32
        }
2✔
33
        handler := logging.NewHandler(os.Stdout, opts)
2✔
34
        logger := slog.New(handler)
2✔
35
        slog.SetDefault(logger)
2✔
36
}
2✔
37

38
// NewScaffoldCommand returns a new scaffold command, providing the `kubebuilder alpha generate`
39
// feature to re-scaffold projects and assist users with updates.
40
//
41
// IMPORTANT: This command is intended solely for Kubebuilder's use, as it is designed to work
42
// specifically within Kubebuilder's project configuration, key mappings, and plugin initialization.
43
// Its implementation includes fixed values and logic tailored to Kubebuilder’s unique setup, which may
44
// not apply to other projects. Consequently, importing and using this command directly in other projects
45
// will likely result in unexpected behavior, as external projects may have different supported plugin
46
// structures, configurations, and requirements.
47
//
48
// For other projects using Kubebuilder as a library, replicating similar functionality would require
49
// a custom implementation to ensure compatibility with the specific configurations and plugins of that project.
50
//
51
// Technically, implementing functions that allow re-scaffolding with the exact plugins and project-specific
52
// code of external projects is not feasible within Kubebuilder’s current design.
53
func NewScaffoldCommand() *cobra.Command {
2✔
54
        opts := internal.Generate{}
2✔
55

2✔
56
        scaffoldCmd := &cobra.Command{
2✔
57
                Use:   "generate",
2✔
58
                Short: "Re-scaffold a Kubebuilder project from its PROJECT file",
2✔
59
                Long: `The 'generate' command re-creates a Kubebuilder project scaffold based on the configuration 
2✔
60
defined in the PROJECT file, using the latest installed Kubebuilder version and plugins.
2✔
61

2✔
62
This is helpful for migrating projects to a newer Kubebuilder layout or plugin version (e.g., v3 to v4)
2✔
63
as update your project from any previous version to the current one.
2✔
64

2✔
65
If no output directory is provided, the current working directory will be cleaned (except .git and PROJECT).`,
2✔
66
                Example: `
2✔
67
  # **WARNING**(will delete all files to allow the re-scaffold except .git and PROJECT)
2✔
68
  # Re-scaffold the project in-place 
2✔
69
  kubebuilder alpha generate
2✔
70

2✔
71
  # Re-scaffold the project from ./test into ./my-output
2✔
72
  kubebuilder alpha generate --input-dir="./path/to/project" --output-dir="./my-output"
2✔
73
`,
2✔
74
                PreRunE: func(_ *cobra.Command, _ []string) error {
2✔
75
                        return opts.Validate()
×
76
                },
×
77
                Run: func(_ *cobra.Command, _ []string) {
×
78
                        if err := opts.Generate(); err != nil {
×
NEW
79
                                slog.Error("failed to generate project", "error", err)
×
NEW
80
                                os.Exit(1)
×
81
                        }
×
82
                },
83
        }
84

85
        scaffoldCmd.Flags().StringVar(&opts.InputDir, "input-dir", "",
2✔
86
                "Path to the directory containing the PROJECT file. "+
2✔
87
                        "Defaults to the current working directory. WARNING: delete existing files (except .git and PROJECT).")
2✔
88

2✔
89
        scaffoldCmd.Flags().StringVar(&opts.OutputDir, "output-dir", "",
2✔
90
                "Directory where the new project scaffold will be written. "+
2✔
91
                        "If unset, re-scaffolding occurs in-place "+
2✔
92
                        "and will delete existing files (except .git and PROJECT).")
2✔
93

2✔
94
        return scaffoldCmd
2✔
95
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc