Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions cmd/crossplane/project/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"path/filepath"
"strings"

"github.com/google/go-containerregistry/pkg/name"
"k8s.io/apimachinery/pkg/util/validation"

"github.com/crossplane/crossplane-runtime/v2/pkg/errors"
Expand All @@ -38,8 +39,10 @@ const projectFileName = "crossplane-project.yaml"

// initCmd initializes a new project.
type initCmd struct {
Name string `arg:"" help:"The name of the new project."`
Directory string `help:"Directory to initialize. Defaults to project name." short:"d" type:"path"`
Name string `arg:"" help:"The name of the new project."`
Directory string `help:"Directory to initialize. Defaults to project name" short:"d" type:"path"`
Registry string `default:"example.com/my-org" help:"Override the registry in the project file." optional:"" short:"r"`
Repository string `help:"Override the repository name in the project file. Defaults to the project name." optional:""`
}

func (c *initCmd) Help() string {
Expand All @@ -55,12 +58,24 @@ func (c *initCmd) Run(sp terminal.SpinnerPrinter) error {
if c.Directory == "" {
c.Directory = c.Name
}

if strings.TrimSpace(c.Repository) == "" {
c.Repository = c.Name
}
Comment on lines +61 to +63

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Normalize Repository after trimming, before fallback and validation.

Right now TrimSpace is only used for the emptiness check, but Line 74 still consumes the untrimmed value. Could we persist the trimmed value so accidental leading/trailing spaces don’t cause avoidable parse failures?

Suggested patch
-	if strings.TrimSpace(c.Repository) == "" {
+	c.Repository = strings.TrimSpace(c.Repository)
+	if c.Repository == "" {
 		c.Repository = c.Name
 	}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@cmd/crossplane/project/init.go` around lines 61 - 63, In the Repository field
initialization block, the TrimSpace function is only used for the emptiness
check, but the untrimmed value is still assigned to c.Repository when empty.
Persist the trimmed value by assigning the result of
strings.TrimSpace(c.Repository) back to c.Repository before the fallback check,
so that any subsequent validation or usage of the Repository field at line 74 or
elsewhere operates on the normalized value without accidental leading or
trailing whitespace.

// Check if the target directory is suitable.
if err := c.checkTargetDirectory(); err != nil {
return err
}

rp := strings.TrimRight(strings.TrimSpace(c.Registry), "/")
if rp == "" {
return errors.New("registry cannot be empty; set --registry to an OCI registry prefix like 'xpkg.crossplane.io/my-org'")
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

r, err := name.NewRepository(rp + "/" + c.Repository)
if err != nil {
return errors.Wrapf(err, "cannot build repository \"%s/%s\"", rp, c.Repository)
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

return sp.WrapWithSuccessSpinner("Initializing project", func() error {
if err := os.MkdirAll(c.Directory, 0o750); err != nil {
return errors.Wrapf(err, "failed to create directory %s", c.Directory)
Expand All @@ -73,8 +88,8 @@ kind: Project
metadata:
name: %s
spec:
repository: example.com/my-org/%s
`, c.Name, c.Name)
repository: %s
`, c.Name, r.String())

if err := os.WriteFile(projFile, []byte(content), 0o600); err != nil {
return errors.Wrapf(err, "failed to write %s", projectFileName)
Expand Down
Loading