From 7c297a9b6a0e0ee54465df236277ee383f9ca79b Mon Sep 17 00:00:00 2001 From: Jefferson Faseler Date: Tue, 23 Nov 2021 15:43:32 -0600 Subject: [PATCH] Add reinstall option (#13) --- cmd/pkgs.go | 28 +++++++++++++++++++++++++++- internal/bundle/bundle.go | 16 ++++++++++++++++ pkgmngr/pkgmngr.go | 32 ++++++++++++++++++++++++++++---- 3 files changed, 71 insertions(+), 5 deletions(-) diff --git a/cmd/pkgs.go b/cmd/pkgs.go index 1e3eacd..4d30f21 100644 --- a/cmd/pkgs.go +++ b/cmd/pkgs.go @@ -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]", @@ -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 } @@ -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) diff --git a/internal/bundle/bundle.go b/internal/bundle/bundle.go index e9740d0..ac21ecc 100644 --- a/internal/bundle/bundle.go +++ b/internal/bundle/bundle.go @@ -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) diff --git a/pkgmngr/pkgmngr.go b/pkgmngr/pkgmngr.go index f2551c3..1da5ec2 100644 --- a/pkgmngr/pkgmngr.go +++ b/pkgmngr/pkgmngr.go @@ -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) @@ -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) @@ -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