Skip to content

Commit

Permalink
[envsec] Fix non-git init and don't offer to link if no projects (#239)
Browse files Browse the repository at this point in the history
## Summary

* `GitSubdirectory` should not break if not in git repo
* Don't give user option to link projects if there are zero projects
* Trim name when validating 

## How was it tested?
  • Loading branch information
mikeland73 authored Dec 20, 2023
1 parent 14ba2cf commit 9664edd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
33 changes: 17 additions & 16 deletions internal/flow/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,23 @@ func (i *Init) Run(ctx context.Context) (id.ProjectID, error) {

// TODO: printOrgNotice will be a team picker once that is implemented.
i.printOrgNotice(member)
linkToExisting, err := i.linkToExistingPrompt()
orgID, err := typeid.Parse[id.OrgID](i.Token.IDClaims().OrgID)
if err != nil {
return id.ProjectID{}, err
}

projects, err := i.Client.ListProjects(ctx, orgID)
if err != nil {
return id.ProjectID{}, err
}
if linkToExisting {
return i.showExistingListPrompt(ctx)
if len(projects) > 0 {
linkToExisting, err := i.linkToExistingPrompt()
if err != nil {
return id.ProjectID{}, err
}
if linkToExisting {
return i.showExistingListPrompt(projects)
}
}
return i.createNewPrompt(ctx, member)
}
Expand Down Expand Up @@ -85,18 +96,8 @@ func (i *Init) linkToExistingPrompt() (bool, error) {
}

func (i *Init) showExistingListPrompt(
ctx context.Context,
projects []*projectsv1alpha1.Project,
) (id.ProjectID, error) {
orgID, err := typeid.Parse[id.OrgID](i.Token.IDClaims().OrgID)
if err != nil {
return id.ProjectID{}, err
}

projects, err := i.Client.ListProjects(ctx, orgID)
if err != nil {
return id.ProjectID{}, err
}

repo, err := git.GitRepoURL(i.WorkingDir)
if err != nil {
return id.ProjectID{}, err
Expand Down Expand Up @@ -154,7 +155,7 @@ func (i *Init) createNewPrompt(
Label: "What’s the name of your new project",
Default: filepath.Base(i.WorkingDir),
Validate: func(name string) error {
if name == "" {
if strings.TrimSpace(name) == "" {
return errors.New("project name cannot be empty")
}
return nil
Expand Down Expand Up @@ -186,7 +187,7 @@ func (i *Init) createNewPrompt(
orgID,
repo,
directory,
name,
strings.TrimSpace(name),
)
if err != nil {
return id.ProjectID{}, err
Expand Down
3 changes: 3 additions & 0 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ func GitSubdirectory(wd string) (string, error) {
cmd.Dir = wd
output, err := cmd.CombinedOutput()
if err != nil {
if !isInGitRepo(wd) {
return "", nil
}
return "", err
}
return filepath.Clean(strings.TrimSpace(string(output))), nil
Expand Down

0 comments on commit 9664edd

Please sign in to comment.