Skip to content

Commit

Permalink
feat[#399]: set pkgm input defaults
Browse files Browse the repository at this point in the history
add default for autoremove

add default for purge
  • Loading branch information
nellfs authored and kbdharun committed Oct 20, 2024
1 parent 3416569 commit f5e4f9e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 14 deletions.
64 changes: 50 additions & 14 deletions cmd/pkgManagers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,32 @@ import (
"github.com/vanilla-os/orchid/cmdr"
)

const (
PkgManagerCmdAutoRemove = "autoRemove"
PkgManagerCmdClean = "clean"
PkgManagerCmdInstall = "install"
PkgManagerCmdList = "list"
PkgManagerCmdPurge = "purge"
PkgManagerCmdRemove = "remove"
PkgManagerCmdSearch = "search"
PkgManagerCmdShow = "show"
PkgManagerCmdUpdate = "update"
PkgManagerCmdUpgrade = "upgrade"
)

var PkgManagerCmdSetOrder = []string{
PkgManagerCmdInstall,
PkgManagerCmdUpdate,
PkgManagerCmdRemove,
PkgManagerCmdPurge,
PkgManagerCmdAutoRemove,
PkgManagerCmdClean,
PkgManagerCmdList,
PkgManagerCmdSearch,
PkgManagerCmdShow,
PkgManagerCmdUpgrade,
}

func NewPkgManagersCommand() *cmdr.Command {
// Root command
cmd := cmdr.NewCommand(
Expand Down Expand Up @@ -474,24 +500,34 @@ func newPkgManager(cmd *cobra.Command, args []string) error {
}

cmdMap := map[string]*string{
"autoRemove": &autoRemove,
"clean": &clean,
"install": &install,
"list": &list,
"purge": &purge,
"remove": &remove,
"search": &search,
"show": &show,
"update": &update,
"upgrade": &upgrade,
}

for cmdName, cmd := range cmdMap {
PkgManagerCmdAutoRemove: &autoRemove,
PkgManagerCmdClean: &clean,
PkgManagerCmdInstall: &install,
PkgManagerCmdList: &list,
PkgManagerCmdPurge: &purge,
PkgManagerCmdRemove: &remove,
PkgManagerCmdSearch: &search,
PkgManagerCmdShow: &show,
PkgManagerCmdUpdate: &update,
PkgManagerCmdUpgrade: &upgrade,
}

for _, cmdName := range PkgManagerCmdSetOrder {
cmd := cmdMap[cmdName]
if *cmd == "" {
if noPrompt {
cmdr.Error.Printf(apx.Trans("pkgmanagers.new.error.noCommand"), cmdName)
return nil
}
if cmdName == PkgManagerCmdPurge || cmdName == PkgManagerCmdAutoRemove {
cmdr.Info.Printfln(apx.Trans("pkgmanagers.new.info.askCommandWithDefault"), cmdName, remove)
*cmd, _ = reader.ReadString('\n')
*cmd = strings.ReplaceAll(*cmd, "\n", "")
if *cmd == "" {
*cmd = remove
}
continue
}

cmdr.Info.Printfln(apx.Trans("pkgmanagers.new.info.askCommand"), cmdName)
*cmd, _ = reader.ReadString('\n')
Expand Down Expand Up @@ -679,7 +715,7 @@ func updatePkgManager(cmd *cobra.Command, args []string) error {
cmdr.Error.Println(apx.Trans("pkgmanagers.update.error.builtIn"))
os.Exit(126)
}

reader := bufio.NewReader(os.Stdin)

if autoRemove == "" {
Expand Down
1 change: 1 addition & 0 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ pkgmanagers:
askName: "Choose a name:"
askSudo: "Does the package manager need sudo to run?"
askCommand: "Enter the command for '%s':"
askCommandWithDefault: "Enter the command for '%s' (default: '%s'):"
askOverwrite: "A package manager with the name '%s' already exists. Overwrite
it?"
options:
Expand Down

0 comments on commit f5e4f9e

Please sign in to comment.