-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathgenerate.go
More file actions
99 lines (85 loc) · 4.14 KB
/
generate.go
File metadata and controls
99 lines (85 loc) · 4.14 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package alpha
import (
"log/slog"
"os"
"github.com/spf13/cobra"
"sigs.k8s.io/kubebuilder/v4/internal/cli/alpha/internal"
)
// NewScaffoldCommand returns a new scaffold command, providing the `kubebuilder alpha generate`
// feature to re-scaffold projects and assist users with updates.
//
// IMPORTANT: This command is intended solely for Kubebuilder's use, as it is designed to work
// specifically within Kubebuilder's project configuration, key mappings, and plugin initialization.
// Its implementation includes fixed values and logic tailored to Kubebuilder’s unique setup, which may
// not apply to other projects. Consequently, importing and using this command directly in other projects
// will likely result in unexpected behavior, as external projects may have different supported plugin
// structures, configurations, and requirements.
//
// For other projects using Kubebuilder as a library, replicating similar functionality would require
// a custom implementation to ensure compatibility with the specific configurations and plugins of that project.
//
// Technically, implementing functions that allow re-scaffolding with the exact plugins and project-specific
// code of external projects is not feasible within Kubebuilder’s current design.
func NewScaffoldCommand() *cobra.Command {
opts := internal.Generate{}
scaffoldCmd := &cobra.Command{
Use: "generate",
Short: "Re-scaffold a Kubebuilder project from its PROJECT file",
Long: `The 'generate' command re-creates a Kubebuilder project scaffold based on the configuration
defined in the PROJECT file, using the latest installed Kubebuilder version and plugins.
This is helpful for migrating projects to a newer Kubebuilder layout or plugin version (e.g., v3 to v4)
as update your project from any previous version to the current one.
If no output directory is provided, the current working directory will be cleaned (except .git and PROJECT).`,
Example: `
# **WARNING**(will delete all files to allow the re-scaffold except .git and PROJECT)
# Re-scaffold the project in-place
kubebuilder alpha generate
# Re-scaffold the project from ./test into ./my-output
kubebuilder alpha generate --input-dir="./path/to/project" --output-dir="./my-output"
`,
PreRunE: func(_ *cobra.Command, _ []string) error {
return opts.Validate()
},
Run: func(_ *cobra.Command, _ []string) {
if err := opts.Generate(); err != nil {
slog.Error("failed to generate project", "error", err)
os.Exit(1)
}
},
}
// Show hint message on how to list flags instead of showing file completion for
// commands that don't take files as arguments
scaffoldCmd.ValidArgsFunction = func(
_ *cobra.Command,
args []string,
toComplete string,
) ([]cobra.Completion, cobra.ShellCompDirective) {
completions := []cobra.Completion{}
if len(args) == 0 && toComplete == "" {
completions = cobra.AppendActiveHelp(completions, "Type '--' and press TAB to list flags")
}
return completions, cobra.ShellCompDirectiveNoFileComp
}
scaffoldCmd.Flags().StringVar(&opts.InputDir, "input-dir", "",
"Path to the directory containing the PROJECT file. "+
"Defaults to the current working directory. WARNING: delete existing files (except .git and PROJECT).")
scaffoldCmd.Flags().StringVar(&opts.OutputDir, "output-dir", "",
"Directory where the new project scaffold will be written. "+
"If unset, re-scaffolding occurs in-place "+
"and will delete existing files (except .git and PROJECT).")
scaffoldCmd.Flags().BoolVar(&opts.SkipGoVersionCheck, "skip-go-version-check", true,
"skip the Go version check during project generation")
return scaffoldCmd
}