From 8f3022402142ee14fb5259a38297ac59b45df57c Mon Sep 17 00:00:00 2001 From: nellfs Date: Sat, 19 Oct 2024 14:52:54 -0300 Subject: [PATCH] feat[#399]: set pkgm input defaults add default for autoremove add default for purge --- cmd/pkgManagers.go | 64 ++++++++++++++++++++++++++++++++++++---------- locales/en.yml | 1 + 2 files changed, 51 insertions(+), 14 deletions(-) diff --git a/cmd/pkgManagers.go b/cmd/pkgManagers.go index 9159c2e5..b3648df4 100644 --- a/cmd/pkgManagers.go +++ b/cmd/pkgManagers.go @@ -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( @@ -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') @@ -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 == "" { diff --git a/locales/en.yml b/locales/en.yml index 15b4f100..a0e9a0e0 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -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: