From 1c8626916247e02e83a2b9df5f39b607cd2fb09a Mon Sep 17 00:00:00 2001 From: Marwan Hawari Date: Sat, 5 Mar 2022 11:28:46 -0800 Subject: [PATCH] add 'rename' subcommand --- cmd/rename.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ lib/util.go | 8 ++++---- main.go | 9 +++++++++ 3 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 cmd/rename.go diff --git a/cmd/rename.go b/cmd/rename.go new file mode 100644 index 0000000..9505ea6 --- /dev/null +++ b/cmd/rename.go @@ -0,0 +1,56 @@ +package cmd + +import ( + "fmt" + "os" + "path/filepath" + + "github.com/marwanhawari/stew/constants" + stew "github.com/marwanhawari/stew/lib" +) + +// Rename is executed when you run `stew rename` +func Rename(cliInput string) { + + err := stew.ValidateCLIInput(cliInput) + stew.CatchAndExit(err) + + stewPath, err := stew.GetStewPath() + stew.CatchAndExit(err) + systemInfo := stew.NewSystemInfo(stewPath) + + userOS := systemInfo.Os + userArch := systemInfo.Arch + stewBinPath := systemInfo.StewBinPath + stewLockFilePath := systemInfo.StewLockFilePath + + lockFile, err := stew.NewLockFile(stewLockFilePath, userOS, userArch) + stew.CatchAndExit(err) + + if len(lockFile.Packages) == 0 { + stew.CatchAndExit(stew.NoBinariesInstalledError{}) + } + + var binaryFound bool + var renamedBinaryName string + for index, pkg := range lockFile.Packages { + if pkg.Binary == cliInput { + renamedBinaryName, err = stew.PromptRenameBinary(cliInput) + stew.CatchAndExit(err) + err = os.Rename(filepath.Join(stewBinPath, cliInput), filepath.Join(stewBinPath, renamedBinaryName)) + stew.CatchAndExit(err) + + lockFile.Packages[index].Binary = renamedBinaryName + binaryFound = true + break + } + } + if !binaryFound { + stew.CatchAndExit(stew.BinaryNotInstalledError{Binary: cliInput}) + } + + err = stew.WriteLockFileJSON(lockFile, stewLockFilePath) + stew.CatchAndExit(err) + + fmt.Printf("✨ Successfully renamed the %v binary to %v\n", constants.GreenColor(cliInput), constants.GreenColor(renamedBinaryName)) +} diff --git a/lib/util.go b/lib/util.go index d085b84..a04e785 100644 --- a/lib/util.go +++ b/lib/util.go @@ -194,7 +194,7 @@ func getBinary(filePaths []string, repo string) (string, string, error) { return "", "", err } binaryName = filepath.Base(binaryFile) - binaryName, err = renameBinary(binaryName) + binaryName, err = PromptRenameBinary(binaryName) if err != nil { return "", "", nil } @@ -311,7 +311,7 @@ func extractBinary(downloadedFilePath, tmpExtractionPath string) error { } else { originalBinaryName := filepath.Base(downloadedFilePath) - renamedBinaryName, err := renameBinary(originalBinaryName) + renamedBinaryName, err := PromptRenameBinary(originalBinaryName) if err != nil { return err } @@ -403,8 +403,8 @@ func InstallBinary(downloadedFilePath string, repo string, systemInfo SystemInfo return binaryName, nil } -// renameBinary takes in the original name of the binary and will return the new name of the binary. -func renameBinary(originalBinaryName string) (string, error) { +// PromptRenameBinary takes in the original name of the binary and will return the new name of the binary. +func PromptRenameBinary(originalBinaryName string) (string, error) { renamedBinaryName, err := warningPromptInput("Rename the binary?", originalBinaryName) if err != nil { return "", err diff --git a/main.go b/main.go index 62eb54d..d83365f 100644 --- a/main.go +++ b/main.go @@ -71,6 +71,15 @@ func main() { return nil }, }, + { + Name: "rename", + Usage: "Rename an installed binary using a prompt UI. [Ex: stew rename fzf]", + Aliases: []string{"re"}, + Action: func(c *cli.Context) error { + cmd.Rename(c.Args().First()) + return nil + }, + }, { Name: "list", Usage: "List installed binaries [Ex: stew list]",