Skip to content

Commit

Permalink
add 'rename' subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
marwanhawari committed Mar 5, 2022
1 parent 60ed1d3 commit 1c86269
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
56 changes: 56 additions & 0 deletions cmd/rename.go
Original file line number Diff line number Diff line change
@@ -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))
}
8 changes: 4 additions & 4 deletions lib/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]",
Expand Down

0 comments on commit 1c86269

Please sign in to comment.