look for go-import in the HTML

This commit is contained in:
Jeff Carr 2024-03-08 15:09:46 -06:00
parent 54367ddc72
commit 546ad6a842
1 changed files with 73 additions and 3 deletions

76
new.go
View File

@ -2,6 +2,8 @@ package repostatus
import ( import (
"errors" "errors"
"io/ioutil"
"net/http"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -103,19 +105,87 @@ func guessPaths(path string) (string, string, string, bool, error) {
} }
// attempt to git clone if the go path doesn't exist // attempt to git clone if the go path doesn't exist
func clone(wdir string, path string) { func clone(wdir string, path string) error {
fullpath := filepath.Join(wdir, path) fullpath := filepath.Join(wdir, path)
if IsDirectory(fullpath) { if IsDirectory(fullpath) {
return // directory already exists
return nil
} }
err := os.Chdir(wdir) err := os.Chdir(wdir)
if err != nil { if err != nil {
return return err
} }
base := filepath.Join(wdir, filepath.Dir(path)) base := filepath.Join(wdir, filepath.Dir(path))
os.MkdirAll(base, 0750) os.MkdirAll(base, 0750)
err = os.Chdir(base)
if err != nil {
return err
}
shell.RunPath(base, []string{"git", "clone", "http://" + path}) shell.RunPath(base, []string{"git", "clone", "http://" + path})
if IsDirectory(fullpath) {
// clone worked
return nil
}
var url string
if url, err = findGoImport("http://" + path); err != nil {
return err
}
log.Info("URL:", url)
shell.RunPath(base, []string{"git", "clone", url})
if IsDirectory(fullpath) {
// clone worked
return nil
}
return errors.New("resolve go import")
}
// this is a terrible hack and really
// shouldn't be here. Hopefully this can
// be removed and the GO compiler code can
// be involked directly (it appears to currently
// be an internal/ function in the GO sources
// so it can't be imported from outside the compiler
func findGoImport(url string) (string, error) {
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
tmp := string(bodyBytes)
parts := strings.Split(tmp, "go-import")
if len(parts) < 2 {
return "", errors.New("missing go-import")
}
// this is terrible, it doesn't even look for 'content='
// but again, this is just a hack for my own code to be
// usuable after the removal in go v1.22 of the go get clone behavior that was in v1.21
parts = strings.Split(parts[1], "\"")
var newurl string
for {
if len(parts) == 0 {
break
}
tmp := strings.TrimSpace(parts[0])
fields := strings.Split(tmp, " ")
log.Info("FIELDS:", fields)
if len(fields) == 3 {
newurl = fields[2]
break
}
parts = parts[1:]
}
if newurl == "" {
return "", errors.New("missing git content string")
}
return newurl, nil
} }
func NewRepoStatusWindow(path string) (error, *RepoStatus) { func NewRepoStatusWindow(path string) (error, *RepoStatus) {