-
Notifications
You must be signed in to change notification settings - Fork 0
feat(files): Add deployment content push #6
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
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 |
|---|---|---|
| @@ -1 +1 @@ | ||
| 0.3.0 | ||
| 0.4.0-beta.4 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,137 @@ | ||||||||||||||||||
| package flatrun | ||||||||||||||||||
|
|
||||||||||||||||||
| import ( | ||||||||||||||||||
| "archive/tar" | ||||||||||||||||||
| "compress/gzip" | ||||||||||||||||||
| "context" | ||||||||||||||||||
| "fmt" | ||||||||||||||||||
| "io" | ||||||||||||||||||
| "mime/multipart" | ||||||||||||||||||
| "net/http" | ||||||||||||||||||
| "net/url" | ||||||||||||||||||
| "os" | ||||||||||||||||||
| "path/filepath" | ||||||||||||||||||
| "strconv" | ||||||||||||||||||
| "strings" | ||||||||||||||||||
| ) | ||||||||||||||||||
|
|
||||||||||||||||||
| func (c *Client) PushDeploymentFiles(ctx context.Context, deployment, source, destination string, deleteMissing bool) ([]byte, error) { | ||||||||||||||||||
| info, err := os.Stat(source) | ||||||||||||||||||
| if err != nil { | ||||||||||||||||||
| return nil, err | ||||||||||||||||||
| } | ||||||||||||||||||
| if !info.IsDir() { | ||||||||||||||||||
| return nil, fmt.Errorf("source must be a directory") | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| reader, writer := io.Pipe() | ||||||||||||||||||
| multipartWriter := multipart.NewWriter(writer) | ||||||||||||||||||
| errCh := make(chan error, 1) | ||||||||||||||||||
| go func() { | ||||||||||||||||||
| errCh <- writePushBody(multipartWriter, writer, source, destination, deleteMissing) | ||||||||||||||||||
| }() | ||||||||||||||||||
|
|
||||||||||||||||||
| apiBase := strings.TrimRight(c.baseURL, "/") | ||||||||||||||||||
| if !strings.HasSuffix(apiBase, "/api") { | ||||||||||||||||||
| apiBase += "/api" | ||||||||||||||||||
| } | ||||||||||||||||||
| path := "/deployments/" + url.PathEscape(deployment) + "/files-push" | ||||||||||||||||||
| req, err := http.NewRequestWithContext(ctx, http.MethodPost, apiBase+path, reader) | ||||||||||||||||||
| if err != nil { | ||||||||||||||||||
| _ = reader.Close() | ||||||||||||||||||
| return nil, err | ||||||||||||||||||
| } | ||||||||||||||||||
| req.Header.Set("Accept", "application/json") | ||||||||||||||||||
| req.Header.Set("Content-Type", multipartWriter.FormDataContentType()) | ||||||||||||||||||
| if c.token != "" { | ||||||||||||||||||
| req.Header.Set("Authorization", "Bearer "+c.token) | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| data, requestErr := c.doRequest(req) | ||||||||||||||||||
| archiveErr := <-errCh | ||||||||||||||||||
| if requestErr != nil { | ||||||||||||||||||
| return nil, requestErr | ||||||||||||||||||
| } | ||||||||||||||||||
| if archiveErr != nil { | ||||||||||||||||||
| return nil, archiveErr | ||||||||||||||||||
| } | ||||||||||||||||||
| return data, nil | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| func writePushBody(multipartWriter *multipart.Writer, pipe *io.PipeWriter, source, destination string, deleteMissing bool) error { | ||||||||||||||||||
| fail := func(err error) error { | ||||||||||||||||||
| _ = pipe.CloseWithError(err) | ||||||||||||||||||
| return err | ||||||||||||||||||
| } | ||||||||||||||||||
| if err := multipartWriter.WriteField("destination", destination); err != nil { | ||||||||||||||||||
| return fail(err) | ||||||||||||||||||
| } | ||||||||||||||||||
| if err := multipartWriter.WriteField("delete", strconv.FormatBool(deleteMissing)); err != nil { | ||||||||||||||||||
| return fail(err) | ||||||||||||||||||
| } | ||||||||||||||||||
| part, err := multipartWriter.CreateFormFile("archive", "content.tar.gz") | ||||||||||||||||||
| if err != nil { | ||||||||||||||||||
| return fail(err) | ||||||||||||||||||
| } | ||||||||||||||||||
| gzipWriter := gzip.NewWriter(part) | ||||||||||||||||||
| tarWriter := tar.NewWriter(gzipWriter) | ||||||||||||||||||
| if err := writeDirectoryArchive(tarWriter, source); err != nil { | ||||||||||||||||||
| return fail(err) | ||||||||||||||||||
| } | ||||||||||||||||||
| if err := tarWriter.Close(); err != nil { | ||||||||||||||||||
| return fail(err) | ||||||||||||||||||
| } | ||||||||||||||||||
| if err := gzipWriter.Close(); err != nil { | ||||||||||||||||||
| return fail(err) | ||||||||||||||||||
| } | ||||||||||||||||||
| if err := multipartWriter.Close(); err != nil { | ||||||||||||||||||
| return fail(err) | ||||||||||||||||||
| } | ||||||||||||||||||
| return pipe.Close() | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| func writeDirectoryArchive(writer *tar.Writer, source string) error { | ||||||||||||||||||
| return filepath.WalkDir(source, func(path string, entry os.DirEntry, walkErr error) error { | ||||||||||||||||||
| if walkErr != nil { | ||||||||||||||||||
| return walkErr | ||||||||||||||||||
| } | ||||||||||||||||||
| if path == source { | ||||||||||||||||||
| return nil | ||||||||||||||||||
| } | ||||||||||||||||||
| if entry.Type()&os.ModeSymlink != 0 { | ||||||||||||||||||
| return fmt.Errorf("symbolic links are not supported: %s", path) | ||||||||||||||||||
| } | ||||||||||||||||||
| info, err := entry.Info() | ||||||||||||||||||
|
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. Optimization: You can check if the entry is a symbolic link using
Suggested change
|
||||||||||||||||||
| if err != nil { | ||||||||||||||||||
| return err | ||||||||||||||||||
| } | ||||||||||||||||||
| relative, err := filepath.Rel(source, path) | ||||||||||||||||||
| if err != nil { | ||||||||||||||||||
| return err | ||||||||||||||||||
| } | ||||||||||||||||||
| header, err := tar.FileInfoHeader(info, "") | ||||||||||||||||||
| if err != nil { | ||||||||||||||||||
| return err | ||||||||||||||||||
| } | ||||||||||||||||||
| header.Name = filepath.ToSlash(relative) | ||||||||||||||||||
| if entry.IsDir() { | ||||||||||||||||||
| header.Name += "/" | ||||||||||||||||||
| } | ||||||||||||||||||
| if err := writer.WriteHeader(header); err != nil { | ||||||||||||||||||
| return err | ||||||||||||||||||
| } | ||||||||||||||||||
| if !info.Mode().IsRegular() { | ||||||||||||||||||
| return nil | ||||||||||||||||||
| } | ||||||||||||||||||
| file, err := os.Open(path) | ||||||||||||||||||
| if err != nil { | ||||||||||||||||||
| return err | ||||||||||||||||||
| } | ||||||||||||||||||
| _, copyErr := io.Copy(writer, file) | ||||||||||||||||||
| closeErr := file.Close() | ||||||||||||||||||
| if copyErr != nil { | ||||||||||||||||||
| return copyErr | ||||||||||||||||||
| } | ||||||||||||||||||
| return closeErr | ||||||||||||||||||
| }) | ||||||||||||||||||
| } | ||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Authorization header setting is duplicated here and in the main request logic (as seen in
internal/flatrun/client.go). To ensure consistency and simplify future changes (e.g., changing auth schemes), consider extracting this into a private helper method on theClientstruct.