Skip to content

Commit

Permalink
Add reinstall option (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jefferson-Faseler authored Nov 23, 2021
1 parent da3fcbd commit 7c297a9
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
28 changes: 27 additions & 1 deletion cmd/pkgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,30 @@ var installPkgCmd = &cobra.Command{
},
}

// ReinstallPkgCmd represents the command for installing vim packages
var reinstallPkgCmd = &cobra.Command{
Use: "reinstall [pkg url]",
Short: "reinstall a vim package",
Long: "Use when the package cannot update with a simple pull",
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
cmd.Help()
return nil
}

for _, pkgName := range args {
fmt.Println("Reinstalling " + pkgName)
err := pkgmngr.Reinstall(pkgName)
if err != nil {
return err
}
}
return nil
},
}

// removePkgCmd represents the command for removing vim packages
var removePkgCmd = &cobra.Command{
Use: "rm [pkg name]",
Expand Down Expand Up @@ -88,7 +112,8 @@ var updatePkgCmd = &cobra.Command{
fmt.Println("Updating " + pkgName)
wasUpToDate, err := pkgmngr.Update(pkgName)
if err != nil {
if strings.Contains(err.Error(), "object not found") {
if strings.Contains(err.Error(), "object not found") ||
strings.Contains(err.Error(), "ssh: handshake failed: knownhosts: key mismatch") {
fmt.Println(err.Error())
continue
}
Expand Down Expand Up @@ -125,6 +150,7 @@ var listPkgCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(installPkgCmd)
rootCmd.AddCommand(reinstallPkgCmd)
rootCmd.AddCommand(removePkgCmd)
rootCmd.AddCommand(updatePkgCmd)
rootCmd.AddCommand(listPkgCmd)
Expand Down
16 changes: 16 additions & 0 deletions internal/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ func Clone(pkgURL, dirPath string) error {
return err
}

// RemoteURL returns the fetch URL. Always assumes remote name is 'origin'
func RemoteURL(dirPath string) (string, error) {
repo, err := git.PlainOpen(dirPath)
if err != nil {
return "", err
}

// always assume origin
remote, err := repo.Remote("origin")
if err != nil {
return "", err
}

return remote.Config().URLs[0], nil
}

// Pull the latest changes from the remote repository of the directory given
func Pull(dirPath string) error {
repo, err := git.PlainOpen(dirPath)
Expand Down
32 changes: 28 additions & 4 deletions pkgmngr/pkgmngr.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,18 @@ func Install(pkgURL string) error {
return nil
}
}
return install(pkgURL, reinstall)
}

func install(pkgURL string, reinstall bool) error {
pkgName := getPkgName(pkgURL)
dirPath := filepath.Join(bundle.DirPath(), pkgName)

tmpDirPath := filepath.Join(os.TempDir(), pkgName)
err := os.MkdirAll(tmpDirPath, os.ModePerm)
if err != nil {
return err
}

err = bundle.Clone(pkgURL, tmpDirPath)
if err != nil {
rmErr := os.RemoveAll(tmpDirPath)
Expand All @@ -54,6 +60,18 @@ func Install(pkgURL string) error {
return os.Rename(tmpDirPath, dirPath)
}

// Reinstall will re-clone a package in place
func Reinstall(pkgName string) error {
dirPath := filepath.Join(bundle.DirPath(), pkgName)

remoteURL, err := bundle.RemoteURL(dirPath)
if err != nil {
return err
}

return install(remoteURL, true)
}

// Remove will remove a package froom the vim bundle
func Remove(pkgName string) error {
dirPath := filepath.Join(bundle.DirPath(), pkgName)
Expand All @@ -73,14 +91,20 @@ func Update(pkgName string) (bool, error) {
return true, nil
}
if strings.Contains(err.Error(), "object not found") {
fmt.Println(pkgName + ` is having troubles updating.
fmt.Println(fmt.Sprintf(`%s is having troubles updating.
This can happen when the package was shallow installed, and needs repairing.
Try reinstalling the package with the url to see if this fixes the issue.`)
Try reinstalling the package with the url to see if this fixes the issue.
To reinstall simply run:
prion reinstall %s
`, pkgName, pkgName))
}
if strings.Contains(err.Error(), "ssh: handshake failed: knownhosts: key mismatch") {
userHomeDir, err := os.UserHomeDir()
if err != nil {
fmt.Errorf(err.Error())
fmt.Println("Could not determine user's home directory")
return false, err
}
fmt.Println(fmt.Sprintf(`%s cannot connect to its remote host over ssh.
If you have not set up ssh connections with your git server you will need to
Expand Down

0 comments on commit 7c297a9

Please sign in to comment.