make dir before create cache file

pull/89/head
Baokun Lee 2019-07-25 22:43:19 +08:00
parent fac88abd58
commit 12fb2b3845
1 changed files with 11 additions and 6 deletions

17
main.go
View File

@ -26,6 +26,7 @@ import (
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
"path"
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
@ -122,8 +123,8 @@ type ops struct{}
func (*ops) NewContext(r *http.Request) (context.Context, error) { func (*ops) NewContext(r *http.Request) (context.Context, error) {
return context.Background(), nil return context.Background(), nil
} }
func (*ops) List(ctx context.Context, path string) (proxy.File, error) { func (*ops) List(ctx context.Context, mpath string) (proxy.File, error) {
escMod, err := module.EscapePath(path) escMod, err := module.EscapePath(mpath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -135,17 +136,21 @@ func (*ops) List(ctx context.Context, path string) (proxy.File, error) {
Path string Path string
Versions []string Versions []string
} }
if err := goJSON(&list, "go", "list", "-m", "-json", "-versions", path+"@latest"); err != nil { if err := goJSON(&list, "go", "list", "-m", "-json", "-versions", mpath+"@latest"); err != nil {
return nil, err return nil, err
} }
if list.Path != path { if list.Path != mpath {
return nil, fmt.Errorf("go list -m: asked for %s but got %s", path, list.Path) return nil, fmt.Errorf("go list -m: asked for %s but got %s", mpath, list.Path)
} }
data := []byte(strings.Join(list.Versions, "\n") + "\n") data := []byte(strings.Join(list.Versions, "\n") + "\n")
if len(data) == 1 { if len(data) == 1 {
data = nil data = nil
} }
ioutil.WriteFile(file, data, 0666) os.MkdirAll(path.Dir(file), 755)
if err := ioutil.WriteFile(file, data, 0666); err != nil {
return nil, err
}
return os.Open(file) return os.Open(file)
} }
func (*ops) Latest(ctx context.Context, path string) (proxy.File, error) { func (*ops) Latest(ctx context.Context, path string) (proxy.File, error) {