这是indexloc提供的服务,不要输入任何密码
Skip to content

Change prebuilt data source to follow symlinks #247

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
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type CreateFileRequest struct {
// CreateFile will upload a file to Vercel so that it can be later used for a Deployment.
func (c *Client) CreateFile(ctx context.Context, request CreateFileRequest) error {
url := fmt.Sprintf("%s/v2/now/files", c.baseURL)
if request.TeamID != "" {
if c.teamID(request.TeamID) != "" {
url = fmt.Sprintf("%s?teamId=%s", url, c.teamID(request.TeamID))
}
req, err := http.NewRequestWithContext(
Expand Down
76 changes: 68 additions & 8 deletions vercel/data_source_prebuilt_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,19 +194,79 @@ func (d *prebuiltProjectDataSource) Read(ctx context.Context, req datasource.Rea
if err != nil {
return err
}
if d.IsDir() {
return nil
}

content, err := os.ReadFile(path)
info, err := d.Info()
if err != nil {
return fmt.Errorf("could not read file %s: %w", path, err)
return fmt.Errorf("could not get file info for %s: %w", path, err)
}

// If it's a symlink, resolve it
if info.Mode()&os.ModeSymlink != 0 {
dest, err := os.Readlink(path)
if err != nil {
return fmt.Errorf("could not read symlink %s: %w", path, err)
}

// If the symlink is relative, make it absolute
if !filepath.IsAbs(dest) {
dest = filepath.Join(filepath.Dir(path), dest)
}

destInfo, err := os.Stat(dest)
if err != nil {
return fmt.Errorf("could not stat symlink destination %s: %w", dest, err)
}

if destInfo.IsDir() {
return filepath.WalkDir(dest, func(subPath string, subD fs.DirEntry, subErr error) error {
if subErr != nil {
return subErr
}
if subD.IsDir() {
return nil
}
content, err := os.ReadFile(subPath)
if err != nil {
return fmt.Errorf("could not read file %s: %w", subPath, err)
}
rawSha := sha1.Sum(content)
sha := hex.EncodeToString(rawSha[:])

// Get the subpath relative to the symlink's destination directory
subRelPath, err := filepath.Rel(dest, subPath)
if err != nil {
return fmt.Errorf("could not get relative path: %w", err)
}

// Join it with the original symlink path
fullPath := filepath.Join(path, subRelPath)
config.Output[fullPath] = fmt.Sprintf("%d~%s", len(content), sha)
return nil
})
}

// If it's a symlink to a file, read that file
content, err := os.ReadFile(dest)
if err != nil {
return fmt.Errorf("could not read symlinked file %s: %w", dest, err)
}
rawSha := sha1.Sum(content)
sha := hex.EncodeToString(rawSha[:])
config.Output[path] = fmt.Sprintf("%d~%s", len(content), sha)
return nil
}

rawSha := sha1.Sum(content)
sha := hex.EncodeToString(rawSha[:])
// Handle regular files (non-symlinks)
if !info.IsDir() {
content, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("could not read file %s: %w", path, err)
}
rawSha := sha1.Sum(content)
sha := hex.EncodeToString(rawSha[:])
config.Output[path] = fmt.Sprintf("%d~%s", len(content), sha)
}

config.Output[path] = fmt.Sprintf("%d~%s", len(content), sha)
return nil
},
)
Expand Down
Loading