Skip to content

Commit

Permalink
[fix] can use relative path
Browse files Browse the repository at this point in the history
close #2
  • Loading branch information
magcho committed Sep 15, 2019
1 parent e70a318 commit f0613aa
Showing 1 changed file with 41 additions and 4 deletions.
45 changes: 41 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ func FolderExists(filename string) bool {

func FileExists(filename string) bool {
if f, err := os.Stat(filename); os.IsNotExist(err) || f.IsDir() {
return false
return false
} else {
return true
return true
}
}
func createLink(filePath string, DOTZ_ROOT string, silentFlag bool) (dotzPath string, err error) {
Expand Down Expand Up @@ -143,6 +143,34 @@ func replaceSlash2DotzPath(origin string) string {
return strings.Replace(origin, "//", DOTZ_ROOT, 1)
}

func convertFullPath(origin string) (string, error) {
i, _ := exec.Command("pwd").Output()
pwd := string(i[:len(i)-1]) // 末端に\nがあるので消してstringにキャスト
dirArr := strings.Split(pwd, "/")

if strings.HasPrefix(origin, HOME) {
// フルパスが入力された時
return origin, nil

} else if strings.HasPrefix(origin, "../") {
// 相対パスで上のディレクトリが指定されたとき
n := strings.Count(origin, "../")
basePath := strings.Join(dirArr[:len(dirArr)-n], "/")
relativePath := strings.Replace(origin, "../", "", -1)
return basePath + "/" + relativePath, nil

} else if strings.HasPrefix(origin, "./") {
// 相対パスでカレントディレクトリ以下が指定された時
return strings.Replace(origin, "./", pwd+"/", 1), nil

} else if FileExists(pwd+`/`+origin) || FolderExists(pwd+`/`+origin) {
// 直接ファイル名が入力された時
return pwd + "/" + origin, nil

}
return "", ErrInputFileTypeIsNotFonund
}

func main() {

app := cli.NewApp()
Expand Down Expand Up @@ -288,7 +316,7 @@ func main() {
{
Name: "track",
Aliases: []string{"t"},
Usage: "file append into dotz project",
Usage: "Track files for dotz project",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "silent, s",
Expand All @@ -310,6 +338,11 @@ func main() {
for i := 0; i < c.NArg(); i++ {
originFilePath := c.Args().Get(i)

originFilePath, err := convertFullPath(originFilePath)
if err != nil {
return err
}

if FileExists(originFilePath) {
// 指定された対象がファイルの時
dotzFilePath, err := createLink(originFilePath, DOTZ_ROOT, c.Bool("silent"))
Expand Down Expand Up @@ -348,13 +381,17 @@ func main() {
}
}

WriteDotzConf(CONFIG, DOTZ_ROOT + DOTZ_CONFIG_FILENAME)
WriteDotzConf(CONFIG, DOTZ_ROOT+DOTZ_CONFIG_FILENAME)

return nil
},
},
}

// app.Action = func(c *cli.Context) error {
// return nil
// }

err := app.Run(os.Args)
if err != nil {
fmt.Println(err)
Expand Down

0 comments on commit f0613aa

Please sign in to comment.