From 89eeef7da156397a6b391bd993929899dbd17545 Mon Sep 17 00:00:00 2001 From: Aaron Schlesinger Date: Sun, 25 Feb 2018 14:34:01 -0800 Subject: [PATCH] adding CLI and refactoring payload defns --- .gitignore | 1 + Gopkg.lock | 8 +- Makefile | 2 + actions/upload.go | 8 +- cmd/cli/main.go | 69 ++++++++ cmd/cli/zip.go | 21 +++ pkg/payloads/upload.go | 6 + testmodule/LICENSE | 1 + testmodule/README | 1 + testmodule/go.mod | 1 + testmodule/pkg/pkg.go | 1 + vendor/github.com/pierrre/archivefile/LICENSE | 7 + .../github.com/pierrre/archivefile/zip/zip.go | 160 ++++++++++++++++++ 13 files changed, 279 insertions(+), 7 deletions(-) create mode 100644 Makefile create mode 100644 cmd/cli/main.go create mode 100644 cmd/cli/zip.go create mode 100644 pkg/payloads/upload.go create mode 100644 testmodule/LICENSE create mode 100644 testmodule/README create mode 100644 testmodule/go.mod create mode 100644 testmodule/pkg/pkg.go create mode 100644 vendor/github.com/pierrre/archivefile/LICENSE create mode 100644 vendor/github.com/pierrre/archivefile/zip/zip.go diff --git a/.gitignore b/.gitignore index 3f264106..bd4afca1 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ node_modules public tmp/* +vgp diff --git a/Gopkg.lock b/Gopkg.lock index 1716f6e1..a5b08fe4 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -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 diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..3e1af6ba --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +cli: + go build -o vgp ./cmd/cli diff --git a/actions/upload.go b/actions/upload.go index 26ea2b79..d1799e33 100644 --- a/actions/upload.go +++ b/actions/upload.go @@ -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) } diff --git a/cmd/cli/main.go b/cmd/cli/main.go new file mode 100644 index 00000000..c22090ad --- /dev/null +++ b/cmd/cli/main.go @@ -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 + +Details: + +- The directory from which code will be uploaded is / +- ... 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) + } +} diff --git a/cmd/cli/zip.go b/cmd/cli/zip.go new file mode 100644 index 00000000..2d2c9728 --- /dev/null +++ b/cmd/cli/zip.go @@ -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 +} diff --git a/pkg/payloads/upload.go b/pkg/payloads/upload.go new file mode 100644 index 00000000..ad67f54f --- /dev/null +++ b/pkg/payloads/upload.go @@ -0,0 +1,6 @@ +package payloads + +type Upload struct { + Module []byte `json:"module_base64"` + Zip []byte `json:"zip_base64"` +} diff --git a/testmodule/LICENSE b/testmodule/LICENSE new file mode 100644 index 00000000..32aad8c3 --- /dev/null +++ b/testmodule/LICENSE @@ -0,0 +1 @@ +hi! diff --git a/testmodule/README b/testmodule/README new file mode 100644 index 00000000..c145c5e4 --- /dev/null +++ b/testmodule/README @@ -0,0 +1 @@ +this is a test, obvisouly. diff --git a/testmodule/go.mod b/testmodule/go.mod new file mode 100644 index 00000000..1d67ab8d --- /dev/null +++ b/testmodule/go.mod @@ -0,0 +1 @@ +module "arschles.com/testmodule" diff --git a/testmodule/pkg/pkg.go b/testmodule/pkg/pkg.go new file mode 100644 index 00000000..c1caffeb --- /dev/null +++ b/testmodule/pkg/pkg.go @@ -0,0 +1 @@ +package pkg diff --git a/vendor/github.com/pierrre/archivefile/LICENSE b/vendor/github.com/pierrre/archivefile/LICENSE new file mode 100644 index 00000000..210d800c --- /dev/null +++ b/vendor/github.com/pierrre/archivefile/LICENSE @@ -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. \ No newline at end of file diff --git a/vendor/github.com/pierrre/archivefile/zip/zip.go b/vendor/github.com/pierrre/archivefile/zip/zip.go new file mode 100644 index 00000000..1cb1fd75 --- /dev/null +++ b/vendor/github.com/pierrre/archivefile/zip/zip.go @@ -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)