-
Notifications
You must be signed in to change notification settings - Fork 157
Re-Enable up for Alpha #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
da357ef
b113ffa
edac055
3e0cbe6
487d85d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/railwayapp/cli/entity" | ||
| ) | ||
|
|
||
| func (h *Handler) Up(ctx context.Context, req *entity.CommandRequest) error { | ||
| return h.ctrl.Up(ctx) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| package controller | ||
|
|
||
| import ( | ||
| "archive/tar" | ||
| "bytes" | ||
| "compress/gzip" | ||
| "context" | ||
| "io" | ||
| "io/ioutil" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/monochromegane/go-gitignore" | ||
| ) | ||
|
|
||
| func compress(src string, buf io.Writer) error { | ||
| // tar > gzip > buf | ||
| zr := gzip.NewWriter(buf) | ||
| tw := tar.NewWriter(zr) | ||
|
|
||
| ignore, err := gitignore.NewGitIgnore(".gitignore", ".") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| // walk through every file in the folder | ||
| filepath.Walk(src, func(file string, fi os.FileInfo, err error) error { | ||
| if ignore.Match(file, fi.IsDir()) { | ||
| return nil | ||
| } | ||
| // generate tar header | ||
| header, err := tar.FileInfoHeader(fi, file) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if err != nil { | ||
| return err | ||
| } | ||
| // must provide real name | ||
| // (see https://golang.org/src/archive/tar/common.go?#L626) | ||
| header.Name = filepath.ToSlash(file) | ||
|
|
||
| // write header | ||
| if err := tw.WriteHeader(header); err != nil { | ||
| return err | ||
| } | ||
| // if not a dir, write file content | ||
| if !fi.IsDir() { | ||
| data, err := os.Open(file) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if _, err := io.Copy(tw, data); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| }) | ||
|
|
||
| // produce tar | ||
| if err := tw.Close(); err != nil { | ||
| return err | ||
| } | ||
| // produce gzip | ||
| if err := zr.Close(); err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (c *Controller) Up(ctx context.Context) error { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is only enabled for admins, should we also prevent non-admins from using this command?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would mean exposing an endpoint to determine if a user is or isn't an admin. Too much leakage IMO.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We already have a gql endpoint setup to fetch the user. We could a also return the |
||
| // projectID, err := c.cfg.GetProject() | ||
| // if err != nil { | ||
| // return err | ||
| // } | ||
| // environmentID, err := c.cfg.GetEnvironment() | ||
| // if err != nil { | ||
| // return err | ||
| // } | ||
| var buf bytes.Buffer | ||
| if err := compress("./", &buf); err != nil { | ||
| return err | ||
| } | ||
| ioutil.WriteFile("./out.tar.gz", buf.Bytes(), 0777) | ||
| // res, err := c.gtwy.Up(ctx, &entity.UpRequest{ | ||
| // Data: buf, | ||
| // ProjectID: projectID, | ||
| // EnvironmentID: environmentID, | ||
| // }) | ||
| // if err != nil { | ||
| // return err | ||
| // } | ||
| //fmt.Printf("Deploy available at %s\n", res.URL) | ||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package entity | ||
|
|
||
| import "bytes" | ||
|
|
||
| type UpRequest struct { | ||
| Data bytes.Buffer | ||
| ProjectID string | ||
| EnvironmentID string | ||
| } | ||
|
|
||
| type UpResponse struct { | ||
| URL string | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package gateway | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "io/ioutil" | ||
| "mime/multipart" | ||
| "net/http" | ||
|
|
||
| "github.com/railwayapp/cli/entity" | ||
| ) | ||
|
|
||
| func constructReq(ctx context.Context, req *entity.UpRequest) (*http.Request, error) { | ||
| body := new(bytes.Buffer) | ||
| writer := multipart.NewWriter(body) | ||
| part, err := writer.CreateFormFile("file", req.ProjectID) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| part.Write(req.Data.Bytes()) | ||
|
|
||
| err = writer.Close() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| url := fmt.Sprintf("%s/project/%s/up", GetHost(), req.ProjectID) | ||
| httpReq, err := http.NewRequest("POST", url, body) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| httpReq.Header.Set("Content-Type", writer.FormDataContentType()) | ||
| return httpReq, nil | ||
| } | ||
|
|
||
| func (g *Gateway) Up(ctx context.Context, req *entity.UpRequest) (*entity.UpResponse, error) { | ||
| httpReq, err := constructReq(ctx, req) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| client := &http.Client{} | ||
| resp, err := client.Do(httpReq) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| bodyBytes, err := ioutil.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if resp.StatusCode < 200 || resp.StatusCode >= 400 { | ||
| return nil, errors.New(string(bodyBytes)) | ||
| } | ||
| var res entity.UpResponse | ||
| if err := json.Unmarshal(bodyBytes, &res); err != nil { | ||
| return nil, err | ||
| } | ||
| return &res, nil | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.