fix path bug for windows

This commit is contained in:
STARAI\Star 2024-09-08 21:06:23 +08:00
parent dea12f178a
commit 874e63e4f5

24
main.go
View File

@ -9,7 +9,6 @@ import (
"io" "io"
"os" "os"
"os/exec" "os/exec"
"path"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strconv" "strconv"
@ -95,7 +94,7 @@ var cachePath = "c:\\.ag\\cache"
func init() { func init() {
hc.SetGlobalHeader("Content-Type", "application/json") hc.SetGlobalHeader("Content-Type", "application/json")
if homePath, err := os.UserHomeDir(); err == nil { if homePath, err := os.UserHomeDir(); err == nil {
cachePath = path.Join(homePath, ".ag", "cache") cachePath = filepath.Join(homePath, ".ag", "cache")
} }
} }
@ -205,15 +204,15 @@ func checkProjectPath() string {
// } // }
// return args[0] // return args[0]
//} else { //} else {
if len(u.ReadDirN(".")) != 0 { if u.FileExists("go.mod") {
fmt.Println(u.BRed("current path is not empty")) fmt.Println(u.BRed("go.mod exists"))
return "" return ""
} }
if pathname, err := os.Getwd(); err != nil { if pathname, err := os.Getwd(); err != nil {
fmt.Println(u.BRed("getwd error: " + err.Error())) fmt.Println(u.BRed("getwd error: " + err.Error()))
return "" return ""
} else { } else {
return path.Base(pathname) return filepath.Base(pathname)
} }
//} //}
} }
@ -261,14 +260,14 @@ type Tag struct {
func fetchRepo(name string) (repoPath string) { func fetchRepo(name string) (repoPath string) {
apiUrl, owner, repo := parseRepo(name) apiUrl, owner, repo := parseRepo(name)
tagsUrl := apiUrl + path.Join("repos", owner, repo, "tags") tagsUrl := apiUrl + filepath.Join("repos", owner, repo, "tags")
tags := make([]Tag, 0) tags := make([]Tag, 0)
fmt.Println("try to fetch tags", tagsUrl) fmt.Println("try to fetch tags", tagsUrl)
r := hc.Get(tagsUrl) r := hc.Get(tagsUrl)
if r.Error == nil { if r.Error == nil {
r.To(&tags) r.To(&tags)
} else if apiUrl != "https://api.github.com/" { } else if apiUrl != "https://api.github.com/" {
tagsUrl = "https://api.github.com/" + path.Join("repos", owner, repo, "tags") tagsUrl = "https://api.github.com/" + filepath.Join("repos", owner, repo, "tags")
fmt.Println("try to fetch tags", tagsUrl) fmt.Println("try to fetch tags", tagsUrl)
r = hc.Get(tagsUrl) r = hc.Get(tagsUrl)
if r.Error == nil { if r.Error == nil {
@ -721,13 +720,14 @@ func main() {
if len(os.Args) > 2 { if len(os.Args) > 2 {
cmd2 += " " + os.Args[2] cmd2 += " " + os.Args[2]
} }
for _, cmdInfo := range commands { for i := len(commands) - 1; i >= 0; i-- {
if cmd1 == cmdInfo.Name || cmd1 == cmdInfo.ShortName { cmdInfo := commands[i]
cmdInfo.Func(os.Args[2:]) if cmd2 == cmdInfo.Name || cmd2 == cmdInfo.ShortName {
return
} else if cmd2 == cmdInfo.Name {
cmdInfo.Func(os.Args[3:]) cmdInfo.Func(os.Args[3:])
return return
} else if cmd1 == cmdInfo.Name || cmd1 == cmdInfo.ShortName {
cmdInfo.Func(os.Args[2:])
return
} }
} }
} }