From 12fb2b38450c9f13354cb12861aeb5bde738f15e Mon Sep 17 00:00:00 2001 From: Baokun Lee Date: Thu, 25 Jul 2019 22:43:19 +0800 Subject: [PATCH] make dir before create cache file --- main.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 5a6a371..e39f6d5 100644 --- a/main.go +++ b/main.go @@ -26,6 +26,7 @@ import ( "net/http" "os" "os/exec" + "path" "path/filepath" "strings" "time" @@ -122,8 +123,8 @@ type ops struct{} func (*ops) NewContext(r *http.Request) (context.Context, error) { return context.Background(), nil } -func (*ops) List(ctx context.Context, path string) (proxy.File, error) { - escMod, err := module.EscapePath(path) +func (*ops) List(ctx context.Context, mpath string) (proxy.File, error) { + escMod, err := module.EscapePath(mpath) if err != nil { return nil, err } @@ -135,17 +136,21 @@ func (*ops) List(ctx context.Context, path string) (proxy.File, error) { Path 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 } - if list.Path != path { - return nil, fmt.Errorf("go list -m: asked for %s but got %s", path, list.Path) + if list.Path != mpath { + return nil, fmt.Errorf("go list -m: asked for %s but got %s", mpath, list.Path) } data := []byte(strings.Join(list.Versions, "\n") + "\n") if len(data) == 1 { 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) } func (*ops) Latest(ctx context.Context, path string) (proxy.File, error) {