mirror of
https://github.com/gomods/athens
synced 2026-02-03 11:00:32 +00:00
changed to using path.Join to build url (#122)
* added base-url option for cli * changed to using path.Join to build url also, fixed linting issue
This commit is contained in:
committed by
Brian Ketelsen
parent
ab89139c5d
commit
00ac591082
+12
-7
@@ -6,6 +6,8 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
@@ -18,6 +20,7 @@ import (
|
||||
type uploadCmd struct {
|
||||
moduleName string
|
||||
version string
|
||||
baseURL string
|
||||
}
|
||||
|
||||
func newUploadCmd() *cobra.Command {
|
||||
@@ -29,6 +32,7 @@ func newUploadCmd() *cobra.Command {
|
||||
RunE: upload(uploadCmd),
|
||||
}
|
||||
cmd.Flags().StringVarP(&uploadCmd.version, "version", "v", "v0.0.1", "The version of this module")
|
||||
cmd.Flags().StringVarP(&uploadCmd.baseURL, "base-url", "u", "http://localhost:3000/admin/upload", "The Athens base url.")
|
||||
return cmd
|
||||
}
|
||||
|
||||
@@ -58,11 +62,12 @@ func upload(c *uploadCmd) func(*cobra.Command, []string) error {
|
||||
return fmt.Errorf("couldn't make zip (%s)", err)
|
||||
}
|
||||
|
||||
url := fmt.Sprintf(
|
||||
"http://localhost:3000/admin/upload/%s/%s",
|
||||
c.moduleName,
|
||||
c.version,
|
||||
)
|
||||
u, err := url.Parse(c.baseURL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("not a valid base url (%s)", err)
|
||||
}
|
||||
|
||||
u.Path = path.Join(u.Path, c.moduleName, c.version)
|
||||
postBody := &payloads.Upload{
|
||||
Module: modBytes,
|
||||
Zip: zipBytes,
|
||||
@@ -71,8 +76,8 @@ func upload(c *uploadCmd) func(*cobra.Command, []string) error {
|
||||
if err := json.NewEncoder(buf).Encode(postBody); err != nil {
|
||||
return fmt.Errorf("error encoding json (%s)", err)
|
||||
}
|
||||
cmd.Printf("POSTing to %s", url)
|
||||
resp, err := http.Post(url, "application/json", buf)
|
||||
cmd.Printf("POSTing to %s", u)
|
||||
resp, err := http.Post(u.String(), "application/json", buf)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error uploading (%s)", err)
|
||||
} else if resp.StatusCode != 200 {
|
||||
|
||||
Reference in New Issue
Block a user