2 Commits
Author SHA1 Message Date
EnumDev ac1ba2ada3 Improve confirmation prompts 2025-10-08 20:46:44 +03:00
EnumDev 5fb8211933 Validate package names using regex 2025-10-07 20:50:08 +03:00
2 changed files with 60 additions and 34 deletions
+48 -33
View File
@@ -433,15 +433,12 @@ func installPackages() {
// Confirmation Prompt
if !yesAll {
reader := bufio.NewReader(os.Stdin)
if len(operation.Actions) == 1 {
fmt.Printf("Do you wish to install this package? [y\\N] ")
} else {
fmt.Printf("Do you wish to install these %d packages? [y\\N] ", len(operation.Actions))
prompt := "Do you wish to install this package?"
if len(operation.Actions) != 1 {
prompt = fmt.Sprintf("Do you wish to install all %d packages?", len(operation.Actions))
}
text, _ := reader.ReadString('\n')
if strings.TrimSpace(strings.ToLower(text)) != "y" && strings.TrimSpace(strings.ToLower(text)) != "yes" {
if !showConfirmationPrompt(prompt, false) {
fmt.Println("Cancelling package installation...")
exitCode = 1
return
@@ -467,11 +464,7 @@ func installPackages() {
// Confirmation Prompt
if sourcePackagesShown > 0 && !yesAll {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("Are you sure you wish to continue? [y\\N] ")
text, _ := reader.ReadString('\n')
if strings.TrimSpace(strings.ToLower(text)) != "y" && strings.TrimSpace(strings.ToLower(text)) != "yes" {
if !showConfirmationPrompt("Do you wish to continue?", false) {
fmt.Println("Cancelling package installation...")
exitCode = 1
return
@@ -556,10 +549,12 @@ func removePackages() {
// Confirmation Prompt
if !yesAll {
fmt.Printf("Are you sure you wish to remove all %d packages? [y\\N] ", len(operation.Actions))
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
if strings.TrimSpace(strings.ToLower(text)) != "y" && strings.TrimSpace(strings.ToLower(text)) != "yes" {
prompt := "Do you wish to remove this package?"
if len(operation.Actions) != 1 {
prompt = fmt.Sprintf("Do you wish to remove all %d packages?", len(operation.Actions))
}
if !showConfirmationPrompt(prompt, false) {
fmt.Println("Cancelling package removal...")
exitCode = 1
return
@@ -667,10 +662,12 @@ func doCleanup() {
// Confirmation Prompt
if !yesAll {
fmt.Printf("Are you sure you wish to remove all %d packages? [y\\N] ", len(operation.Actions))
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
if strings.TrimSpace(strings.ToLower(text)) != "y" && strings.TrimSpace(strings.ToLower(text)) != "yes" {
prompt := "Do you wish to remove this package?"
if len(operation.Actions) != 1 {
prompt = fmt.Sprintf("Do you wish to remove all %d packages?", len(operation.Actions))
}
if !showConfirmationPrompt(prompt, false) {
fmt.Println("Cancelling package removal...")
exitCode = 1
return
@@ -720,10 +717,7 @@ func syncDatabases() {
// Confirmation Prompt
if !yesAll {
fmt.Printf("Are you sure you wish to sync all databases? [y\\N] ")
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
if strings.TrimSpace(strings.ToLower(text)) != "y" && strings.TrimSpace(strings.ToLower(text)) != "yes" {
if !showConfirmationPrompt("Do you wish to sync all databases?", false) {
fmt.Println("Cancelling database synchronization...")
exitCode = 1
return
@@ -797,10 +791,12 @@ func updatePackages() {
// Confirmation Prompt
if !yesAll {
fmt.Printf("Are you sure you wish to update all %d packages? [y\\N] ", len(operation.Actions))
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
if strings.TrimSpace(strings.ToLower(text)) != "y" && strings.TrimSpace(strings.ToLower(text)) != "yes" {
prompt := "Do you wish to update this package?"
if len(operation.Actions) != 1 {
prompt = fmt.Sprintf("Do you wish to update all %d packages?", len(operation.Actions))
}
if !showConfirmationPrompt(prompt, false) {
fmt.Println("Cancelling package update...")
exitCode = 1
return
@@ -826,11 +822,7 @@ func updatePackages() {
// Confirmation Prompt
if sourcePackagesShown > 0 && !yesAll {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("Are you sure you wish to continue? [y\\N] ")
text, _ := reader.ReadString('\n')
if strings.TrimSpace(strings.ToLower(text)) != "y" && strings.TrimSpace(strings.ToLower(text)) != "yes" {
if !showConfirmationPrompt("Do you wish to continue?", false) {
fmt.Println("Cancelling package installation...")
exitCode = 1
return
@@ -1208,3 +1200,26 @@ func isFlagSet(flagSet *flag.FlagSet, name string) bool {
})
return found
}
func showConfirmationPrompt(prompt string, defaultTo bool) bool {
reader := bufio.NewReader(os.Stdin)
if defaultTo {
fmt.Printf("%s [Y/n] ", prompt)
} else {
fmt.Printf("%s [y/N] ", prompt)
}
text, _ := reader.ReadString('\n')
text = strings.TrimSpace(text)
if len(text) > 0 {
switch text[0] {
case 'y', 'Y':
return true
case 'n', 'N':
return false
}
}
return defaultTo
}
+12 -1
View File
@@ -10,6 +10,7 @@ import (
"os"
"os/exec"
"path"
"regexp"
"slices"
"sort"
"strconv"
@@ -461,11 +462,21 @@ func ReadPackageInfo(contents string) (*PackageInfo, error) {
}
}
// Ensure package name is valid
if match, _ := regexp.MatchString("^[a-zA-Z0-9._-]+$", pkgInfo.Name); !match {
return nil, fmt.Errorf("package name (%s) is invalid", pkgInfo.Name)
}
// Setup split package information
for i, splitPkg := range pkgInfo.SplitPackages {
// Ensure split package contains a name
if splitPkg.Name == "" {
return nil, fmt.Errorf("invalid split package name: %s", splitPkg.Name)
return nil, fmt.Errorf("package name (%s) is invalid", splitPkg.Name)
}
// Ensure split package name is valid
if match, _ := regexp.MatchString("^[a-zA-Z0-9._-]+$", splitPkg.Name); !match {
return nil, fmt.Errorf("package name (%s) is invalid", splitPkg.Name)
}
// Turn split package into yaml data