mirror of
https://github.com/gomods/athens
synced 2026-02-03 11:00:32 +00:00
adding CLI and refactoring payload defns
This commit is contained in:
@@ -15,3 +15,4 @@
|
||||
node_modules
|
||||
public
|
||||
tmp/*
|
||||
vgp
|
||||
|
||||
Generated
+7
-1
@@ -335,6 +335,12 @@
|
||||
revision = "acdc4509485b587f5e675510c4f2c63e90ff68a8"
|
||||
version = "v1.1.0"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
name = "github.com/pierrre/archivefile"
|
||||
packages = ["zip"]
|
||||
revision = "e2d100bc74f58ed7d2f41175cd2cf5fe29446399"
|
||||
|
||||
[[projects]]
|
||||
name = "github.com/pkg/errors"
|
||||
packages = ["."]
|
||||
@@ -501,6 +507,6 @@
|
||||
[solve-meta]
|
||||
analyzer-name = "dep"
|
||||
analyzer-version = 1
|
||||
inputs-digest = "d985a72ff370dba9e708969f958fefb388a8411a32ed0ec000f5613f27a7c816"
|
||||
inputs-digest = "5d725089953a20a5e348470245b57930e8bb973fc837e1185ca9d62bb463684a"
|
||||
solver-name = "gps-cdcl"
|
||||
solver-version = 1
|
||||
|
||||
+2
-6
@@ -3,16 +3,12 @@ package actions
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/arschles/vgoprox/pkg/payloads"
|
||||
"github.com/arschles/vgoprox/pkg/storage"
|
||||
"github.com/gobuffalo/buffalo"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type uploadPayload struct {
|
||||
Module []byte `json:"module_base64"`
|
||||
Zip []byte `json:"zip_base64"`
|
||||
}
|
||||
|
||||
func uploadHandler(store storage.Saver) func(c buffalo.Context) error {
|
||||
return func(c buffalo.Context) error {
|
||||
stdParams, err := getStandardParams(c)
|
||||
@@ -20,7 +16,7 @@ func uploadHandler(store storage.Saver) func(c buffalo.Context) error {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
version := c.Param("ver")
|
||||
payload := new(uploadPayload)
|
||||
payload := new(payloads.Upload)
|
||||
if c.Bind(payload); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/arschles/vgoprox/pkg/payloads"
|
||||
)
|
||||
|
||||
const help = `Usage:
|
||||
vgp <basepath> <module> <version>
|
||||
|
||||
Details:
|
||||
|
||||
- The directory from which code will be uploaded is <basepath>/<module>
|
||||
- ... and that directory must have a go.mod file in ot
|
||||
- ... 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
|
||||
`
|
||||
|
||||
func main() {
|
||||
if len(os.Args) != 4 {
|
||||
log.Println(help)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
basePath := os.Args[1]
|
||||
module := os.Args[2]
|
||||
version := os.Args[3]
|
||||
|
||||
fullDirectory, err := filepath.Abs(filepath.Join(basePath, module))
|
||||
if err != nil {
|
||||
log.Fatalf("couldn't get full directory (%s)", err)
|
||||
}
|
||||
log.Printf("found directory %s", fullDirectory)
|
||||
modFilePath := filepath.Join(fullDirectory, "go.mod")
|
||||
log.Printf("reading %s file", modFilePath)
|
||||
modBytes, err := ioutil.ReadFile(modFilePath)
|
||||
if err != nil {
|
||||
log.Fatalf("couldn't find go.mod file (%s)", err)
|
||||
}
|
||||
|
||||
zipBytes, err := makeZip(fullDirectory)
|
||||
if err != nil {
|
||||
log.Fatalf("couldn't make zip (%s)", err)
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("http://localhost:3000/admin/upload/%s/%s/%s", basePath, module, version)
|
||||
postBody := &payloads.Upload{
|
||||
Module: modBytes,
|
||||
Zip: zipBytes,
|
||||
}
|
||||
buf := new(bytes.Buffer)
|
||||
if err := json.NewEncoder(buf).Encode(postBody); err != nil {
|
||||
log.Fatalf("error encoding json (%s)", err)
|
||||
}
|
||||
resp, err := http.Post(url, "application/json", buf)
|
||||
if err != nil {
|
||||
log.Fatalf("error uploading (%s)", err)
|
||||
} else if resp.StatusCode != 200 {
|
||||
log.Fatalf("upload failed because status code was %d", resp.StatusCode)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/pierrre/archivefile/zip"
|
||||
)
|
||||
|
||||
type file struct {
|
||||
Name string
|
||||
Body string
|
||||
}
|
||||
|
||||
// the dir must end with a "/"
|
||||
func makeZip(dir string) ([]byte, error) {
|
||||
buf := new(bytes.Buffer)
|
||||
if err := zip.Archive(dir, buf, nil); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package payloads
|
||||
|
||||
type Upload struct {
|
||||
Module []byte `json:"module_base64"`
|
||||
Zip []byte `json:"zip_base64"`
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
hi!
|
||||
@@ -0,0 +1 @@
|
||||
this is a test, obvisouly.
|
||||
@@ -0,0 +1 @@
|
||||
module "arschles.com/testmodule"
|
||||
@@ -0,0 +1 @@
|
||||
package pkg
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
Copyright (C) 2015 Pierre Durand
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
+160
@@ -0,0 +1,160 @@
|
||||
// Package zip provides a helper for the archive/zip package (archive/unarchive to/from a file/reader/writer)
|
||||
package zip
|
||||
|
||||
import (
|
||||
zip_impl "archive/zip"
|
||||
"io"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Archive compresses a file/directory to a writer
|
||||
//
|
||||
// If the path ends with a separator, then the contents of the folder at that path
|
||||
// are at the root level of the archive, otherwise, the root of the archive contains
|
||||
// the folder as its only item (with contents inside).
|
||||
//
|
||||
// If progress is not nil, it is called for each file added to the archive.
|
||||
func Archive(inFilePath string, writer io.Writer, progress ProgressFunc) error {
|
||||
zipWriter := zip_impl.NewWriter(writer)
|
||||
|
||||
basePath := filepath.Dir(inFilePath)
|
||||
|
||||
err := filepath.Walk(inFilePath, func(filePath string, fileInfo os.FileInfo, err error) error {
|
||||
if err != nil || fileInfo.IsDir() {
|
||||
return err
|
||||
}
|
||||
|
||||
relativeFilePath, err := filepath.Rel(basePath, filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
archivePath := path.Join(filepath.SplitList(relativeFilePath)...)
|
||||
|
||||
if progress != nil {
|
||||
progress(archivePath)
|
||||
}
|
||||
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
_ = file.Close()
|
||||
}()
|
||||
|
||||
zipFileWriter, err := zipWriter.Create(archivePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = io.Copy(zipFileWriter, file)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return zipWriter.Close()
|
||||
}
|
||||
|
||||
// ArchiveFile compresses a file/directory to a file
|
||||
//
|
||||
// See Archive() doc
|
||||
func ArchiveFile(inFilePath string, outFilePath string, progress ProgressFunc) error {
|
||||
outFile, err := os.Create(outFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
_ = outFile.Close()
|
||||
}()
|
||||
|
||||
return Archive(inFilePath, outFile, progress)
|
||||
}
|
||||
|
||||
// Unarchive decompresses a reader to a directory
|
||||
//
|
||||
// The data's size is required because the zip reader needs it.
|
||||
//
|
||||
// The archive's content will be extracted directly to outFilePath.
|
||||
//
|
||||
// If progress is not nil, it is called for each file extracted from the archive.
|
||||
func Unarchive(reader io.ReaderAt, readerSize int64, outFilePath string, progress ProgressFunc) error {
|
||||
zipReader, err := zip_impl.NewReader(reader, readerSize)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, zipFile := range zipReader.File {
|
||||
err := unarchiveFile(zipFile, outFilePath, progress)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnarchiveFile decompresses a file to a directory
|
||||
//
|
||||
// See Unarchive() doc
|
||||
func UnarchiveFile(inFilePath string, outFilePath string, progress ProgressFunc) error {
|
||||
inFile, err := os.Open(inFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
_ = inFile.Close()
|
||||
}()
|
||||
|
||||
inFileInfo, err := inFile.Stat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
inFileSize := inFileInfo.Size()
|
||||
|
||||
return Unarchive(inFile, inFileSize, outFilePath, progress)
|
||||
}
|
||||
|
||||
func unarchiveFile(zipFile *zip_impl.File, outFilePath string, progress ProgressFunc) error {
|
||||
if zipFile.FileInfo().IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
if progress != nil {
|
||||
progress(zipFile.Name)
|
||||
}
|
||||
|
||||
zipFileReader, err := zipFile.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
_ = zipFileReader.Close()
|
||||
}()
|
||||
|
||||
filePath := filepath.Join(outFilePath, filepath.Join(strings.Split(zipFile.Name, "/")...))
|
||||
|
||||
err = os.MkdirAll(filepath.Dir(filePath), os.FileMode(0755))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
file, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
_ = file.Close()
|
||||
}()
|
||||
|
||||
_, err = io.Copy(file, zipFileReader)
|
||||
return err
|
||||
}
|
||||
|
||||
// ProgressFunc is the type of the function called for each archive file.
|
||||
type ProgressFunc func(archivePath string)
|
||||
Reference in New Issue
Block a user