Skip to content

Commit

Permalink
fix: fix absolute path
Browse files Browse the repository at this point in the history
  • Loading branch information
dtomasi committed Aug 18, 2021
1 parent 7c7520d commit 466509b
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion git-dirclone/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/whilp/git-urls"
"os"
"os/exec"
"os/user"
"path"
"path/filepath"
"strings"
Expand All @@ -27,7 +28,7 @@ func newRootCmd() *cobra.Command {
return err
}

rootDir, err = filepath.Abs(rootDir)
rootDir, err = expandPathWithTilde(rootDir)
if err != nil {
return err
}
Expand All @@ -54,3 +55,24 @@ func newRootCmd() *cobra.Command {
func Execute() {
cobra.CheckErr(rootCmd.Execute())
}

func expandPathWithTilde(rootDir string) (string, error) {

usr, err := user.Current()
if err != nil {
return "", err
}

dir := usr.HomeDir

if rootDir == "~" {
// In case of "~", which won't be caught by the "else if"
rootDir = dir
} else if strings.HasPrefix(rootDir, "~/") {
// Use strings.HasPrefix so we don't match paths like
// "/something/~/something/"
rootDir = filepath.Join(dir, rootDir[2:])
}

return rootDir, nil
}

0 comments on commit 466509b

Please sign in to comment.