-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Re-Enable up for Alpha * Update gateway/up.go Co-authored-by: Jake Runzer <[email protected]> * Ignore node_modules * Gitignore respected Co-authored-by: Jake Runzer <[email protected]>
- Loading branch information
1 parent
4129b8a
commit aacce40
Showing
8 changed files
with
192 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
// 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters