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 // Confirmation Prompt
if !yesAll { if !yesAll {
reader := bufio.NewReader(os.Stdin) prompt := "Do you wish to install this package?"
if len(operation.Actions) == 1 { if len(operation.Actions) != 1 {
fmt.Printf("Do you wish to install this package? [y\\N] ") prompt = fmt.Sprintf("Do you wish to install all %d packages?", len(operation.Actions))
} else {
fmt.Printf("Do you wish to install these %d packages? [y\\N] ", len(operation.Actions))
} }
text, _ := reader.ReadString('\n') if !showConfirmationPrompt(prompt, false) {
if strings.TrimSpace(strings.ToLower(text)) != "y" && strings.TrimSpace(strings.ToLower(text)) != "yes" {
fmt.Println("Cancelling package installation...") fmt.Println("Cancelling package installation...")
exitCode = 1 exitCode = 1
return return
@@ -467,11 +464,7 @@ func installPackages() {
// Confirmation Prompt // Confirmation Prompt
if sourcePackagesShown > 0 && !yesAll { if sourcePackagesShown > 0 && !yesAll {
reader := bufio.NewReader(os.Stdin) if !showConfirmationPrompt("Do you wish to continue?", false) {
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" {
fmt.Println("Cancelling package installation...") fmt.Println("Cancelling package installation...")
exitCode = 1 exitCode = 1
return return
@@ -556,10 +549,12 @@ func removePackages() {
// Confirmation Prompt // Confirmation Prompt
if !yesAll { if !yesAll {
fmt.Printf("Are you sure you wish to remove all %d packages? [y\\N] ", len(operation.Actions)) prompt := "Do you wish to remove this package?"
reader := bufio.NewReader(os.Stdin) if len(operation.Actions) != 1 {
text, _ := reader.ReadString('\n') prompt = fmt.Sprintf("Do you wish to remove all %d packages?", len(operation.Actions))
if strings.TrimSpace(strings.ToLower(text)) != "y" && strings.TrimSpace(strings.ToLower(text)) != "yes" { }
if !showConfirmationPrompt(prompt, false) {
fmt.Println("Cancelling package removal...") fmt.Println("Cancelling package removal...")
exitCode = 1 exitCode = 1
return return
@@ -667,10 +662,12 @@ func doCleanup() {
// Confirmation Prompt // Confirmation Prompt
if !yesAll { if !yesAll {
fmt.Printf("Are you sure you wish to remove all %d packages? [y\\N] ", len(operation.Actions)) prompt := "Do you wish to remove this package?"
reader := bufio.NewReader(os.Stdin) if len(operation.Actions) != 1 {
text, _ := reader.ReadString('\n') prompt = fmt.Sprintf("Do you wish to remove all %d packages?", len(operation.Actions))
if strings.TrimSpace(strings.ToLower(text)) != "y" && strings.TrimSpace(strings.ToLower(text)) != "yes" { }
if !showConfirmationPrompt(prompt, false) {
fmt.Println("Cancelling package removal...") fmt.Println("Cancelling package removal...")
exitCode = 1 exitCode = 1
return return
@@ -720,10 +717,7 @@ func syncDatabases() {
// Confirmation Prompt // Confirmation Prompt
if !yesAll { if !yesAll {
fmt.Printf("Are you sure you wish to sync all databases? [y\\N] ") if !showConfirmationPrompt("Do you wish to sync all databases?", false) {
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
if strings.TrimSpace(strings.ToLower(text)) != "y" && strings.TrimSpace(strings.ToLower(text)) != "yes" {
fmt.Println("Cancelling database synchronization...") fmt.Println("Cancelling database synchronization...")
exitCode = 1 exitCode = 1
return return
@@ -797,10 +791,12 @@ func updatePackages() {
// Confirmation Prompt // Confirmation Prompt
if !yesAll { if !yesAll {
fmt.Printf("Are you sure you wish to update all %d packages? [y\\N] ", len(operation.Actions)) prompt := "Do you wish to update this package?"
reader := bufio.NewReader(os.Stdin) if len(operation.Actions) != 1 {
text, _ := reader.ReadString('\n') prompt = fmt.Sprintf("Do you wish to update all %d packages?", len(operation.Actions))
if strings.TrimSpace(strings.ToLower(text)) != "y" && strings.TrimSpace(strings.ToLower(text)) != "yes" { }
if !showConfirmationPrompt(prompt, false) {
fmt.Println("Cancelling package update...") fmt.Println("Cancelling package update...")
exitCode = 1 exitCode = 1
return return
@@ -826,11 +822,7 @@ func updatePackages() {
// Confirmation Prompt // Confirmation Prompt
if sourcePackagesShown > 0 && !yesAll { if sourcePackagesShown > 0 && !yesAll {
reader := bufio.NewReader(os.Stdin) if !showConfirmationPrompt("Do you wish to continue?", false) {
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" {
fmt.Println("Cancelling package installation...") fmt.Println("Cancelling package installation...")
exitCode = 1 exitCode = 1
return return
@@ -1208,3 +1200,26 @@ func isFlagSet(flagSet *flag.FlagSet, name string) bool {
}) })
return found 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"
"os/exec" "os/exec"
"path" "path"
"regexp"
"slices" "slices"
"sort" "sort"
"strconv" "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 // Setup split package information
for i, splitPkg := range pkgInfo.SplitPackages { for i, splitPkg := range pkgInfo.SplitPackages {
// Ensure split package contains a name // Ensure split package contains a name
if splitPkg.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 // Turn split package into yaml data