Skip to content

Commit

Permalink
Merge pull request #7 from marwanhawari/allow_rename_3
Browse files Browse the repository at this point in the history
Allow renaming of binaries
  • Loading branch information
marwanhawari authored Mar 5, 2022
2 parents 26d3b35 + d91fd27 commit 528fe6d
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 59 deletions.
4 changes: 2 additions & 2 deletions cmd/browse.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"fmt"
"os"
"path"
"path/filepath"

"github.com/marwanhawari/stew/constants"
stew "github.com/marwanhawari/stew/lib"
Expand Down Expand Up @@ -57,7 +57,7 @@ func Browse(cliInput string) {
assetIndex, _ := stew.Contains(releaseAssets, asset)

downloadURL := githubProject.Releases[tagIndex].Assets[assetIndex].DownloadURL
downloadPath := path.Join(stewPkgPath, asset)
downloadPath := filepath.Join(stewPkgPath, asset)
downloadPathExists, err := stew.PathExists(downloadPath)
stew.CatchAndExit(err)

Expand Down
4 changes: 2 additions & 2 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"

"github.com/marwanhawari/stew/constants"
Expand Down Expand Up @@ -102,7 +102,7 @@ func Install(cliInputs []string) {
fmt.Println(constants.GreenColor(asset))
}

downloadPath := path.Join(stewPkgPath, asset)
downloadPath := filepath.Join(stewPkgPath, asset)
downloadPathExists, err := stew.PathExists(downloadPath)
stew.CatchAndExit(err)
if downloadPathExists {
Expand Down
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))
}
4 changes: 2 additions & 2 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"fmt"
"os"
"path"
"path/filepath"

"github.com/marwanhawari/stew/constants"
stew "github.com/marwanhawari/stew/lib"
Expand Down Expand Up @@ -89,7 +89,7 @@ func Upgrade(cliFlag bool, binaryName string) {

downloadURL := githubProject.Releases[tagIndex].Assets[assetIndex].DownloadURL

downloadPath := path.Join(stewPkgPath, asset)
downloadPath := filepath.Join(stewPkgPath, asset)
downloadPathExists, err := stew.PathExists(downloadPath)
stew.CatchAndExit(err)
if downloadPathExists {
Expand Down
14 changes: 7 additions & 7 deletions lib/stewfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"

"github.com/marwanhawari/stew/constants"
)
Expand Down Expand Up @@ -131,10 +131,10 @@ type SystemInfo struct {
func NewSystemInfo(stewPath string) SystemInfo {
var systemInfo SystemInfo
systemInfo.StewPath = stewPath
systemInfo.StewBinPath = path.Join(stewPath, "bin")
systemInfo.StewPkgPath = path.Join(stewPath, "pkg")
systemInfo.StewLockFilePath = path.Join(stewPath, "Stewfile.lock.json")
systemInfo.StewTmpPath = path.Join(stewPath, "tmp")
systemInfo.StewBinPath = filepath.Join(stewPath, "bin")
systemInfo.StewPkgPath = filepath.Join(stewPath, "pkg")
systemInfo.StewLockFilePath = filepath.Join(stewPath, "Stewfile.lock.json")
systemInfo.StewTmpPath = filepath.Join(stewPath, "tmp")
systemInfo.Os = getOS()
systemInfo.Arch = getArch()

Expand All @@ -143,8 +143,8 @@ func NewSystemInfo(stewPath string) SystemInfo {

// DeleteAssetAndBinary will delete the asset from the ~/.stew/pkg path and delete the binary from the ~/.stew/bin path
func DeleteAssetAndBinary(stewPkgPath, stewBinPath, asset, binary string) error {
assetPath := path.Join(stewPkgPath, asset)
binPath := path.Join(stewBinPath, binary)
assetPath := filepath.Join(stewPkgPath, asset)
binPath := filepath.Join(stewBinPath, binary)
err := os.RemoveAll(assetPath)
if err != nil {
return err
Expand Down
30 changes: 15 additions & 15 deletions lib/stewfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package stew
import (
"io/ioutil"
"os"
"path"
"path/filepath"
"reflect"
"runtime"
"testing"
Expand Down Expand Up @@ -69,7 +69,7 @@ func Test_readLockFileJSON(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tempDir := t.TempDir()
lockFilePath := path.Join(tempDir, "Stewfile.lock.json")
lockFilePath := filepath.Join(tempDir, "Stewfile.lock.json")
WriteLockFileJSON(testLockfile, lockFilePath)

got, err := readLockFileJSON(lockFilePath)
Expand Down Expand Up @@ -97,7 +97,7 @@ func TestWriteLockFileJSON(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tempDir := t.TempDir()
lockFilePath := path.Join(tempDir, "Stewfile.lock.json")
lockFilePath := filepath.Join(tempDir, "Stewfile.lock.json")

if err := WriteLockFileJSON(testLockfile, lockFilePath); (err != nil) != tt.wantErr {
t.Errorf("WriteLockFileJSON() error = %v, wantErr %v", err, tt.wantErr)
Expand Down Expand Up @@ -201,7 +201,7 @@ func TestReadStewfileContents(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {

tempDir := t.TempDir()
testStewfilePath := path.Join(tempDir, "Stewfile")
testStewfilePath := filepath.Join(tempDir, "Stewfile")
ioutil.WriteFile(testStewfilePath, []byte(testStewfileContents), 0644)

got, err := ReadStewfileContents(testStewfilePath)
Expand Down Expand Up @@ -241,7 +241,7 @@ func TestNewLockFile(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {

tempDir := t.TempDir()
lockFilePath := path.Join(tempDir, "Stewfile.lock.json")
lockFilePath := filepath.Join(tempDir, "Stewfile.lock.json")
WriteLockFileJSON(testLockfile, lockFilePath)

got, err := NewLockFile(lockFilePath, tt.args.userOS, tt.args.userArch)
Expand Down Expand Up @@ -279,7 +279,7 @@ func TestNewLockFileDoesntExist(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {

tempDir := t.TempDir()
lockFilePath := path.Join(tempDir, "Stewfile.lock.json")
lockFilePath := filepath.Join(tempDir, "Stewfile.lock.json")

got, err := NewLockFile(lockFilePath, tt.args.userOS, tt.args.userArch)
if (err != nil) != tt.wantErr {
Expand Down Expand Up @@ -309,10 +309,10 @@ func TestNewSystemInfo(t *testing.T) {
Os: runtime.GOOS,
Arch: runtime.GOARCH,
StewPath: tempDir,
StewBinPath: path.Join(tempDir, "bin"),
StewPkgPath: path.Join(tempDir, "pkg"),
StewLockFilePath: path.Join(tempDir, "Stewfile.lock.json"),
StewTmpPath: path.Join(tempDir, "tmp"),
StewBinPath: filepath.Join(tempDir, "bin"),
StewPkgPath: filepath.Join(tempDir, "pkg"),
StewLockFilePath: filepath.Join(tempDir, "Stewfile.lock.json"),
StewTmpPath: filepath.Join(tempDir, "tmp"),
}

got := NewSystemInfo(tempDir)
Expand All @@ -336,10 +336,10 @@ func TestDeleteAssetAndBinary(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tempDir := t.TempDir()
os.MkdirAll(path.Join(tempDir, "pkg"), 0755)
testStewAssetPath := path.Join(tempDir, "pkg", "testAsset.tar.gz")
os.MkdirAll(path.Join(tempDir, "bin"), 0755)
testStewBinaryPath := path.Join(tempDir, "bin", "testBinary")
os.MkdirAll(filepath.Join(tempDir, "pkg"), 0755)
testStewAssetPath := filepath.Join(tempDir, "pkg", "testAsset.tar.gz")
os.MkdirAll(filepath.Join(tempDir, "bin"), 0755)
testStewBinaryPath := filepath.Join(tempDir, "bin", "testBinary")

ioutil.WriteFile(testStewAssetPath, []byte("This is a test asset"), 0644)
ioutil.WriteFile(testStewBinaryPath, []byte("This is a test binary"), 0644)
Expand All @@ -351,7 +351,7 @@ func TestDeleteAssetAndBinary(t *testing.T) {
t.Errorf("Either the asset or the binary does not exist yet")
}

if err := DeleteAssetAndBinary(path.Dir(testStewAssetPath), path.Dir(testStewBinaryPath), path.Base(testStewAssetPath), path.Base(testStewBinaryPath)); (err != nil) != tt.wantErr {
if err := DeleteAssetAndBinary(filepath.Dir(testStewAssetPath), filepath.Dir(testStewBinaryPath), filepath.Base(testStewAssetPath), filepath.Base(testStewBinaryPath)); (err != nil) != tt.wantErr {
t.Errorf("DeleteAssetAndBinary() error = %v, wantErr %v", err, tt.wantErr)
}

Expand Down
18 changes: 18 additions & 0 deletions lib/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,21 @@ func WarningPromptConfirm(message string) (bool, error) {

return result, nil
}

// warningPromptInput launches the input UI with a warning styling
func warningPromptInput(message string, defaultInput string) (string, error) {
result := ""
prompt := &survey.Input{
Message: message,
Default: defaultInput,
}
err := survey.AskOne(prompt, &result, survey.WithIcons(func(icons *survey.IconSet) {
icons.Question.Text = "!"
icons.Question.Format = "yellow+hb"
}))
if err != nil {
return "", ExitUserSelectionError{Err: err}
}

return result, nil
}
42 changes: 33 additions & 9 deletions lib/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io"
"net/http"
"os"
"path"
"path/filepath"
"regexp"
"runtime"
Expand Down Expand Up @@ -41,7 +40,7 @@ func CatchAndExit(err error) {
if err != nil {
fmt.Println(err)
stewPath, _ := GetStewPath()
stewTmpPath := path.Join(stewPath, "tmp")
stewTmpPath := filepath.Join(stewPath, "tmp")
err = os.RemoveAll(stewTmpPath)
if err != nil {
fmt.Println(err)
Expand Down Expand Up @@ -195,6 +194,10 @@ func getBinary(filePaths []string, repo string) (string, string, error) {
return "", "", err
}
binaryName = filepath.Base(binaryFile)
binaryName, err = PromptRenameBinary(binaryName)
if err != nil {
return "", "", nil
}
}
}

Expand Down Expand Up @@ -276,7 +279,7 @@ func parseGithubInput(cliInput string) (CLIInput, error) {
}

func parseURLInput(cliInput string) (CLIInput, error) {
return CLIInput{IsGithubInput: false, Asset: path.Base(cliInput), DownloadURL: cliInput}, nil
return CLIInput{IsGithubInput: false, Asset: filepath.Base(cliInput), DownloadURL: cliInput}, nil
}

// Contains checks if a string slice contains a given target
Expand All @@ -298,14 +301,26 @@ func getArch() string {
}

func extractBinary(downloadedFilePath, tmpExtractionPath string) error {
var err error
isArchive := isArchiveFile(downloadedFilePath)
if isArchive {
err = archiver.Unarchive(downloadedFilePath, tmpExtractionPath)
err := archiver.Unarchive(downloadedFilePath, tmpExtractionPath)
if err != nil {
return err
}

} else {
err = copyFile(downloadedFilePath, path.Join(tmpExtractionPath, filepath.Base(downloadedFilePath)))
originalBinaryName := filepath.Base(downloadedFilePath)

renamedBinaryName, err := PromptRenameBinary(originalBinaryName)
if err != nil {
return err
}
err = copyFile(downloadedFilePath, filepath.Join(tmpExtractionPath, renamedBinaryName))
if err != nil {
return err
}
}
return err
return nil
}

// InstallBinary will extract the binary and copy it to the ~/.stew/bin path
Expand All @@ -332,7 +347,7 @@ func InstallBinary(downloadedFilePath string, repo string, systemInfo SystemInfo

// Check if the binary already exists
for index, pkg := range lockFile.Packages {
previousAssetPath := path.Join(assetDownloadPath, pkg.Asset)
previousAssetPath := filepath.Join(assetDownloadPath, pkg.Asset)
newAssetPath := downloadedFilePath
var overwrite bool
if pkg.Binary == binaryName {
Expand Down Expand Up @@ -375,7 +390,7 @@ func InstallBinary(downloadedFilePath string, repo string, systemInfo SystemInfo
}
}

err = copyFile(binaryFile, path.Join(binaryInstallPath, binaryName))
err = copyFile(binaryFile, filepath.Join(binaryInstallPath, binaryName))
if err != nil {
return "", err
}
Expand All @@ -387,3 +402,12 @@ func InstallBinary(downloadedFilePath string, repo string, systemInfo SystemInfo

return binaryName, nil
}

// 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
}
return renamedBinaryName, nil
}
Loading

0 comments on commit 528fe6d

Please sign in to comment.