-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
60ed1d3
commit 1c86269
Showing
3 changed files
with
69 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters