-
Notifications
You must be signed in to change notification settings - Fork 16
feat: add zeabur file pull command
#232
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
Merged
+206
−0
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8237de0
feat: add `zeabur file list/read` commands for uploaded project files
leechenghsiu dd53f4b
fix: add interactive fallback for file list/read commands
leechenghsiu 8c2ca39
fix: move opts into RunE scope and use cmd.Context()
leechenghsiu a836fcf
refactor: replace file list/read with file pull
leechenghsiu 0cb34fc
fix: address CodeRabbit review — validate input, prevent path travers…
leechenghsiu e7c7180
fix: use filepath.Rel for path traversal check, report skipped binaries
leechenghsiu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -14,3 +14,4 @@ dist/ | |
|
|
||
| # cli running assets | ||
| zeabur.zip | ||
| .env.local | ||
This file contains hidden or 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,20 @@ | ||
| package file | ||
|
|
||
| import ( | ||
| "github.com/spf13/cobra" | ||
|
|
||
| filePullCmd "github.com/zeabur/cli/internal/cmd/file/pull" | ||
| "github.com/zeabur/cli/internal/cmdutil" | ||
| ) | ||
|
|
||
| // NewCmdFile creates the file command. | ||
| func NewCmdFile(f *cmdutil.Factory) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "file <command>", | ||
| Short: "Manage uploaded files", | ||
| } | ||
|
|
||
| cmd.AddCommand(filePullCmd.NewCmdPull(f)) | ||
|
|
||
| return cmd | ||
| } |
This file contains hidden or 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,59 @@ | ||
| package pull | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/zeabur/cli/internal/cmdutil" | ||
| ) | ||
|
|
||
| // NewCmdPull creates the file pull command. | ||
| func NewCmdPull(f *cmdutil.Factory) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "pull <upload-id> [target-dir]", | ||
| Short: "Download uploaded project files to local directory", | ||
| Args: cobra.RangeArgs(0, 2), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| uploadID := "" | ||
| targetDir := "." | ||
| if len(args) > 0 { | ||
| uploadID = args[0] | ||
| } | ||
| if len(args) > 1 { | ||
| targetDir = args[1] | ||
| } | ||
| return runPull(cmd, f, uploadID, targetDir) | ||
| }, | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func runPull(cmd *cobra.Command, f *cmdutil.Factory, uploadID string, targetDir string) error { | ||
| if uploadID == "" { | ||
| if !f.Interactive { | ||
| return fmt.Errorf("upload-id is required") | ||
| } | ||
| id, err := f.Prompter.Input("Enter upload ID:", "") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| uploadID = strings.TrimSpace(id) | ||
| if uploadID == "" { | ||
| return fmt.Errorf("upload-id is required") | ||
| } | ||
| } | ||
|
|
||
| count, skipped, err := f.ApiClient.PullUploadFiles(cmd.Context(), uploadID, targetDir) | ||
| if err != nil { | ||
| return fmt.Errorf("pull files failed: %w", err) | ||
| } | ||
|
|
||
| f.Log.Infof("Pulled %d files to %s", count, targetDir) | ||
| if skipped > 0 { | ||
| f.Log.Infof("Skipped %d binary files (images, fonts, archives, etc.)", skipped) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or 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 hidden or 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,117 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| ) | ||
|
|
||
| var binaryExtensions = map[string]bool{ | ||
| ".png": true, ".jpg": true, ".jpeg": true, ".gif": true, ".bmp": true, | ||
| ".ico": true, ".svg": true, ".webp": true, ".avif": true, | ||
| ".woff": true, ".woff2": true, ".ttf": true, ".otf": true, ".eot": true, | ||
| ".zip": true, ".tar": true, ".gz": true, ".bz2": true, ".7z": true, ".rar": true, | ||
| ".pdf": true, ".doc": true, ".docx": true, ".xls": true, ".xlsx": true, | ||
| ".exe": true, ".dll": true, ".so": true, ".dylib": true, ".wasm": true, | ||
| ".mp3": true, ".mp4": true, ".wav": true, ".ogg": true, ".webm": true, | ||
| ".bin": true, ".dat": true, ".db": true, ".sqlite": true, | ||
| } | ||
|
|
||
| // ListUploadFiles lists files in an uploaded project. | ||
| func (c *client) ListUploadFiles(ctx context.Context, uploadID string, path *string) ([]string, error) { | ||
| var query struct { | ||
| Files []string `graphql:"files(uploadID: $uploadID, path: $path)"` | ||
| } | ||
|
|
||
| err := c.Query(ctx, &query, V{ | ||
| "uploadID": ObjectID(uploadID), | ||
| "path": path, | ||
| }) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return query.Files, nil | ||
| } | ||
|
|
||
| // ReadUploadFile reads the content of a file in an uploaded project. | ||
| func (c *client) ReadUploadFile(ctx context.Context, uploadID string, path string) (string, error) { | ||
| var query struct { | ||
| FileContent string `graphql:"fileContent(uploadID: $uploadID, path: $path)"` | ||
| } | ||
|
|
||
| err := c.Query(ctx, &query, V{ | ||
| "uploadID": ObjectID(uploadID), | ||
| "path": path, | ||
| }) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| return query.FileContent, nil | ||
| } | ||
|
|
||
| // PullUploadFiles downloads all files from an upload to a local directory. | ||
| // Returns (pulled count, skipped binary count, error). | ||
| func (c *client) PullUploadFiles(ctx context.Context, uploadID string, targetDir string) (int, int, error) { | ||
| return c.pullDir(ctx, uploadID, "", targetDir) | ||
| } | ||
|
|
||
| func (c *client) pullDir(ctx context.Context, uploadID string, remotePath string, localDir string) (int, int, error) { | ||
| baseDir := filepath.Clean(localDir) | ||
|
|
||
| var pathPtr *string | ||
| if remotePath != "" { | ||
| pathPtr = &remotePath | ||
| } | ||
|
|
||
| entries, err := c.ListUploadFiles(ctx, uploadID, pathPtr) | ||
| if err != nil { | ||
| return 0, 0, fmt.Errorf("list %q: %w", remotePath, err) | ||
| } | ||
|
|
||
| count, skipped := 0, 0 | ||
| for _, entry := range entries { | ||
| fullRemote := remotePath + entry | ||
| fullLocal := filepath.Join(baseDir, fullRemote) | ||
|
|
||
| rel, relErr := filepath.Rel(baseDir, filepath.Clean(fullLocal)) | ||
| if relErr != nil || strings.HasPrefix(rel, "..") { | ||
| return count, skipped, fmt.Errorf("path traversal detected: %q", fullRemote) | ||
| } | ||
|
|
||
|
leechenghsiu marked this conversation as resolved.
|
||
| if strings.HasSuffix(entry, "/") { | ||
| if err := os.MkdirAll(fullLocal, 0o755); err != nil { | ||
| return count, skipped, fmt.Errorf("mkdir %q: %w", fullLocal, err) | ||
| } | ||
| n, s, err := c.pullDir(ctx, uploadID, fullRemote, localDir) | ||
| count += n | ||
| skipped += s | ||
| if err != nil { | ||
| return count, skipped, err | ||
| } | ||
| } else { | ||
| ext := strings.ToLower(filepath.Ext(entry)) | ||
| if binaryExtensions[ext] { | ||
| skipped++ | ||
| continue | ||
|
leechenghsiu marked this conversation as resolved.
|
||
| } | ||
| content, err := c.ReadUploadFile(ctx, uploadID, fullRemote) | ||
| if err != nil { | ||
| return count, skipped, fmt.Errorf("read %q: %w", fullRemote, err) | ||
| } | ||
| dir := filepath.Dir(fullLocal) | ||
| if err := os.MkdirAll(dir, 0o755); err != nil { | ||
| return count, skipped, fmt.Errorf("mkdir %q: %w", dir, err) | ||
| } | ||
| if err := os.WriteFile(fullLocal, []byte(content), 0o644); err != nil { | ||
| return count, skipped, fmt.Errorf("write %q: %w", fullLocal, err) | ||
| } | ||
| count++ | ||
| } | ||
| } | ||
|
|
||
| return count, skipped, nil | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.