Skip to content

Commit

Permalink
feat: add multigroup flag
Browse files Browse the repository at this point in the history
feat: better log when error
  • Loading branch information
yyy1000 committed Jul 6, 2023
1 parent a632400 commit 659edfa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
20 changes: 16 additions & 4 deletions pkg/rescaffold/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,24 @@ const DefaultOutputDir = "output-dir"
func (opts *MigrateOptions) Rescaffold() error {
config := yaml.New(machinery.Filesystem{FS: afero.NewOsFs()})
if err := config.LoadFrom(opts.InputDir); err != nil {
log.Fatal(err)
log.Fatalf("Failed to load PROJECT file %v", err)
}
// create output directory
// nolint: gosec
if err := os.MkdirAll(opts.OutputDir, 0755); err != nil {
log.Fatal(err)
log.Fatalf("Failed to create output directory %v", err)
}
// use the new directory to set up the new project
if err := os.Chdir(opts.OutputDir); err != nil {
log.Fatal(err)
log.Fatalf("Failed to change the current working directory %v", err)
}
// init project with plugins
if err := kubebuilderInit(config); err != nil {
log.Fatal(err)
log.Fatalf("Failed to run init subcommand %v", err)
}
// call edit subcommands to enable or disable multigroup layout
if err := kubebuilderEdit(config); err != nil {
log.Fatalf("Failed to run edit subcommand %v", err)
}
return nil
}
Expand Down Expand Up @@ -107,6 +111,14 @@ func kubebuilderInit(store store.Store) error {
return util.RunCmd("kubebuilder init", "kubebuilder", args...)
}

func kubebuilderEdit(store store.Store) error {
if store.Config().IsMultiGroup() {
args := []string{"edit", "--multigroup"}
return util.RunCmd("kubebuilder edit", "kubebuilder", args...)
}
return nil
}

func getInitArgs(store store.Store) []string {
var args []string
plugins := store.Config().GetPluginChain()
Expand Down
20 changes: 20 additions & 0 deletions test/e2e/alphagenerate/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,24 @@ func ReGenerateProject(kbc *utils.TestContext) {
filepath.Join(kbc.Dir, "testdir", "PROJECT"), version)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
ExpectWithOffset(1, fileContainsExpr).To(BeTrue())

By("editing a project with multigroup=true")
err = kbc.Edit(
"--multigroup=true",
)
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("regenerating the project at another output directory")
err = kbc.Regenerate(
"--input-dir", kbc.Dir,
"--output-dir", filepath.Join(kbc.Dir, "testdir2"),
)
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("checking if the project file was generated with the expected multigroup flag")
var multiGroup = `multigroup: true`
fileContainsExpr, err = pluginutil.HasFileContentWith(
filepath.Join(kbc.Dir, "testdir2", "PROJECT"), multiGroup)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
ExpectWithOffset(1, fileContainsExpr).To(BeTrue())
}

0 comments on commit 659edfa

Please sign in to comment.