Skip to content

Commit

Permalink
Add skip logic into the user prompt and verification section
Browse files Browse the repository at this point in the history
  • Loading branch information
tnederlof committed May 2, 2023
1 parent 650fcb7 commit 8bf08c9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
10 changes: 6 additions & 4 deletions cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,15 @@ func newSetup(setupOpts setupOpts) error {
return fmt.Errorf("issue selecting if verification is to be run: %w", err)
}
if verifyChoice {
username, err := operatingsystem.PromptAndVerifyUser()
username, skip, err := operatingsystem.PromptAndVerifyUser()
if err != nil {
return err
}
err = workbench.VerifyInstallation(username)
if err != nil {
return fmt.Errorf("issue running verification: %w", err)
if !skip {
err = workbench.VerifyInstallation(username)
if err != nil {
return fmt.Errorf("issue running verification: %w", err)
}
}
}
step = "done"
Expand Down
45 changes: 27 additions & 18 deletions internal/operatingsystem/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,37 @@ package operatingsystem

import (
"fmt"
"strings"

"github.com/sol-eng/wbi/internal/system"
)

func PromptAndVerifyUser() (string, error) {
userAccount, err := PromptUserAccount()
if err != nil {
return "", err
func PromptAndVerifyUser() (string, bool, error) {
var overallSkip bool
var userAccount string
for {
username, err := PromptUserAccount()
userAccount = username
if err != nil {
return "", false, err
}
if strings.Contains(userAccount, "skip") {
overallSkip = true
break
}
// lookup user account details
user, err := UserLookup(userAccount)
if err != nil {
system.PrintAndLogInfo(fmt.Sprintf(`The user account "%s" you entered cannot be found. Please try again. To skip this section type "skip".`, userAccount))
} else if user.Uid == "0" {
system.PrintAndLogInfo(fmt.Sprintf(`The user account "%s" is root. A non-root user is required. Please try again. To skip this section type "skip".`, userAccount))
} else if user.HomeDir == "" {
system.PrintAndLogInfo(fmt.Sprintf(`The user account "%s" does not have a home directory. A home directory is required. Please try again. To skip this section type "skip".`, userAccount))
} else {
system.PrintAndLogInfo(fmt.Sprintf("user %s account found and validated", userAccount))
break
}
}
// lookup user account details
user, err := UserLookup(userAccount)
if err != nil {
return "", fmt.Errorf("user %s account not found", userAccount)
}
// verify non-root and a home directory exists
if user.Uid == "0" {
return "", fmt.Errorf("user %s account is root. A non-root user is required", userAccount)
}
if user.HomeDir == "" {
return "", fmt.Errorf("user %s account does not have a home directory. A home directory is required", userAccount)
}
system.PrintAndLogInfo(fmt.Sprintf("user %s account found and validated", userAccount))

return user.Username, nil
return userAccount, overallSkip, nil
}

0 comments on commit 8bf08c9

Please sign in to comment.