Skip to content

Commit

Permalink
fix: cannot get the home dir on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen committed Apr 19, 2024
1 parent 6745e92 commit 55fb231
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions cmd/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package cmd

import (
"fmt"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -29,10 +31,15 @@ func newCheckoutCommand() (c *cobra.Command) {
RunE: opt.runE,
}

userHomeDir, err := homedir.Dir()
if err != nil {
panic(errors.Wrap(err, "cannot find the home directory"))

Check warning on line 36 in cmd/checkout.go

View check run for this annotation

Codecov / codecov/patch

cmd/checkout.go#L36

Added line #L36 was not covered by tests
}

flags := c.Flags()
flags.StringVarP(&opt.url, "url", "", "", "The git repository URL")
flags.StringVarP(&opt.remote, "remote", "", "origin", "The remote name")
flags.StringVarP(&opt.sshPrivateKey, "ssh-private-key", "", "$HOME/.ssh/id_rsa",
flags.StringVarP(&opt.sshPrivateKey, "ssh-private-key", "", filepath.Join(userHomeDir, ".ssh/id_rsa"),
"The SSH private key file path")
flags.StringVarP(&opt.username, "username", "", "", "The username of the git repository")
flags.StringVarP(&opt.password, "password", "", "", "The password of the git repository")
Expand Down Expand Up @@ -99,7 +106,6 @@ func (o *checkoutOption) runE(c *cobra.Command, args []string) (err error) {

if wd, err = repo.Worktree(); err == nil {
if c.Flags().Changed("branch") {
c.Printf("Switched to branch '%s'\n", o.branch)
version = o.branch

if err = wd.Checkout(&git.CheckoutOptions{
Expand All @@ -108,6 +114,7 @@ func (o *checkoutOption) runE(c *cobra.Command, args []string) (err error) {
err = fmt.Errorf("unable to checkout git branch: %s, error: %v", o.branch, err)
return
}
c.Printf("Switched to branch '%s'\n", o.branch)

Check warning on line 117 in cmd/checkout.go

View check run for this annotation

Codecov / codecov/patch

cmd/checkout.go#L117

Added line #L117 was not covered by tests
}

if o.tag != "" {
Expand All @@ -117,6 +124,7 @@ func (o *checkoutOption) runE(c *cobra.Command, args []string) (err error) {
err = fmt.Errorf("unable to checkout git tag: %s, error: %v", o.tag, err)
return
}
c.Printf("Switched to tag '%s'\n", o.tag)

Check warning on line 127 in cmd/checkout.go

View check run for this annotation

Codecov / codecov/patch

cmd/checkout.go#L127

Added line #L127 was not covered by tests
}

if o.pr > 0 {
Expand All @@ -136,6 +144,7 @@ func (o *checkoutOption) runE(c *cobra.Command, args []string) (err error) {
err = fmt.Errorf("unable to checkout git branch: %s, error: %v", o.tag, err)
return
}
c.Printf("Switched to pr-%d\n", o.pr)

Check warning on line 147 in cmd/checkout.go

View check run for this annotation

Codecov / codecov/patch

cmd/checkout.go#L147

Added line #L147 was not covered by tests
version = fmt.Sprintf("pr-%d", o.pr)
}

Expand Down

0 comments on commit 55fb231

Please sign in to comment.