WIP: Removing the CLI (#227)

* Removing the CLI

Still TODO:

- Removing CLI references from docs

* removing the payloads.Upload struct

since it's not used anywhere
This commit is contained in:
Aaron Schlesinger
2018-07-09 13:44:58 -07:00
committed by GitHub
parent f1d66d9d45
commit c3e3dee4d8
6 changed files with 0 additions and 176 deletions
-23
View File
@@ -1,23 +0,0 @@
package main
import (
"log"
"github.com/spf13/cobra"
)
func main() {
cmd := &cobra.Command{
Use: "athens",
Short: "The athens dependency manager",
Example: usage,
SilenceUsage: true,
}
cmd.AddCommand(newUploadCmd())
if err := cmd.Execute(); err != nil {
log.Fatal(err)
}
}
-98
View File
@@ -1,98 +0,0 @@
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"path"
"path/filepath"
parser "github.com/gomods/athens/pkg/gomod/file"
"github.com/gomods/athens/pkg/module"
"github.com/gomods/athens/pkg/payloads"
"github.com/spf13/afero"
"github.com/spf13/cobra"
)
type uploadCmd struct {
moduleName string
version string
baseURL string
}
func newUploadCmd() *cobra.Command {
uploadCmd := &uploadCmd{}
cmd := &cobra.Command{
Use: "upload [directory]",
Short: "package up a directory and upload it to the athens server",
Args: cobra.ExactArgs(1),
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
}
func upload(c *uploadCmd) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
fs := afero.NewOsFs()
dir := args[0]
fullDirectory, err := filepath.Abs(dir)
if err != nil {
return fmt.Errorf("couldn't get full directory (%s)", err)
}
cmd.Printf("found directory %s", fullDirectory)
modFilePath := filepath.Join(fullDirectory, "go.mod")
modBytes, err := afero.ReadFile(fs, modFilePath)
if err != nil {
return fmt.Errorf("couldn't find go.mod file (%s)", err)
}
gomodParser := parser.NewFileParser(fs, modFilePath)
c.moduleName, err = gomodParser.ModuleName()
if err != nil {
return fmt.Errorf("couldn't parse go.mod file (%s)", err)
}
zipReader := module.MakeZip(fs, fullDirectory, c.moduleName, c.version)
infoFilePath := filepath.Join(fullDirectory, c.version+".info")
infoBytes, err := afero.ReadFile(fs, infoFilePath)
if err != nil {
return fmt.Errorf("coudln't find .info file (%s)", err)
}
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)
zipBytes, err := ioutil.ReadAll(zipReader)
if err != nil {
return err
}
postBody := &payloads.Upload{
Module: modBytes,
Zip: zipBytes,
Info: infoBytes,
}
buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(postBody); err != nil {
return fmt.Errorf("error encoding json (%s)", err)
}
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 {
return fmt.Errorf("upload failed because status code was %d", resp.StatusCode)
}
cmd.Println("Upload successful!")
return nil
}
}
-11
View File
@@ -1,11 +0,0 @@
package main
const usage = `athens <dir> --base-url foo.com --module bar --version v1.0.0
Details:
- The directory from which code will be uploaded is <dir>
- ... and that directory must have a go.mod file in it
- ... and the go.mod file's 'module' directive must match 'bar'
- ... and if there's a vendor directory under that directory, it won't be ignored right now
- ... and the go.mod file will be uploaded with the source`
-1
View File
@@ -12,7 +12,6 @@ func addProxyRoutes(app *buffalo.App, storage storage.Backend, mf *module.Filter
app.GET("/{module:.+}/@v/{version}.info", cacheMissHandler(versionInfoHandler(storage), app.Worker, mf))
app.GET("/{module:.+}/@v/{version}.mod", cacheMissHandler(versionModuleHandler(storage), app.Worker, mf))
app.GET("/{module:.+}/@v/{version}.zip", cacheMissHandler(versionZipHandler(storage), app.Worker, mf))
app.POST("/admin/upload/{module:[a-zA-Z./]+}/{version}", uploadHandler(storage))
app.POST("/admin/fetch/{module:[a-zA-Z./]+}/{owner}/{repo}/{ref}/{version}", fetchHandler(storage))
return nil
}
-36
View File
@@ -1,36 +0,0 @@
package actions
import (
"bytes"
"net/http"
"github.com/bketelsen/buffet"
"github.com/gobuffalo/buffalo"
"github.com/gomods/athens/pkg/paths"
"github.com/gomods/athens/pkg/payloads"
"github.com/gomods/athens/pkg/storage"
"github.com/pkg/errors"
)
func uploadHandler(store storage.Saver) func(c buffalo.Context) error {
return func(c buffalo.Context) error {
sp := buffet.SpanFromContext(c)
sp.SetOperationName("uploadHandler")
mod, err := paths.GetModule(c)
if err != nil {
return errors.WithStack(err)
}
version := c.Param("version")
payload := new(payloads.Upload)
if c.Bind(payload); err != nil {
return errors.WithStack(err)
}
saveErr := store.Save(c, mod, version, payload.Module, bytes.NewReader(payload.Zip), payload.Info)
if storage.IsVersionAlreadyExistsErr(saveErr) {
return c.Error(http.StatusConflict, saveErr)
} else if err != nil {
return errors.WithStack(err)
}
return c.Render(http.StatusOK, proxy.JSON(nil))
}
}
-7
View File
@@ -2,13 +2,6 @@ package payloads
import "github.com/gomods/athens/pkg/eventlog"
// Upload is used to send a module (zip and mod file) via POST request to the storage backend and save it there.
type Upload struct {
Module []byte `json:"module"`
Zip []byte `json:"zip"`
Info []byte `json:"info"`
}
// Module is used by proxy to send info about cache miss to Olympus
type Module struct {
Name string `json:"name"`