Skip to content

Commit 8c33dd2

Browse files
committed
fs.FS!
1 parent a8e7684 commit 8c33dd2

File tree

4 files changed

+32
-27
lines changed

4 files changed

+32
-27
lines changed

pkg/agentfs/client.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"context"
1919
"fmt"
2020
"io"
21+
"io/fs"
2122
"net/http"
2223
"os"
2324
"regexp"
@@ -100,7 +101,7 @@ func WithHTTPClient(httpClient *http.Client) ClientOption {
100101
// CreateAgent creates a new agent by building from source.
101102
func (c *Client) CreateAgent(
102103
ctx context.Context,
103-
workingDir string,
104+
source fs.FS,
104105
secrets []*lkproto.AgentSecret,
105106
regions []string,
106107
excludeFiles []string,
@@ -116,7 +117,7 @@ func (c *Client) CreateAgent(
116117
resp.AgentId,
117118
resp.PresignedUrl,
118119
resp.PresignedPostRequest,
119-
workingDir,
120+
source,
120121
excludeFiles,
121122
); err != nil {
122123
return nil, err
@@ -128,7 +129,7 @@ func (c *Client) CreateAgent(
128129
func (c *Client) DeployAgent(
129130
ctx context.Context,
130131
agentID string,
131-
workingDir string,
132+
source fs.FS,
132133
secrets []*lkproto.AgentSecret,
133134
excludeFiles []string,
134135
) error {
@@ -142,7 +143,7 @@ func (c *Client) DeployAgent(
142143
if !resp.Success {
143144
return fmt.Errorf("failed to deploy agent: %s", resp.Message)
144145
}
145-
return c.uploadAndBuild(ctx, agentID, resp.PresignedUrl, resp.PresignedPostRequest, workingDir, excludeFiles)
146+
return c.uploadAndBuild(ctx, agentID, resp.PresignedUrl, resp.PresignedPostRequest, source, excludeFiles)
146147
}
147148

148149
// uploadAndBuild uploads the source and triggers remote build
@@ -151,15 +152,15 @@ func (c *Client) uploadAndBuild(
151152
agentID string,
152153
presignedUrl string,
153154
presignedPostRequest *lkproto.PresignedPostRequest,
154-
workingDir string,
155+
source fs.FS,
155156
excludeFiles []string,
156157
) error {
157-
projectType, err := DetectProjectType(workingDir)
158+
projectType, err := DetectProjectType(source)
158159
if err != nil {
159160
return err
160161
}
161162
if err := UploadTarball(
162-
workingDir,
163+
source,
163164
presignedUrl,
164165
presignedPostRequest,
165166
excludeFiles,

pkg/agentfs/tar.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"compress/gzip"
2121
"fmt"
2222
"io"
23+
"io/fs"
2324
"mime/multipart"
2425
"net/http"
2526
"os"
@@ -54,17 +55,17 @@ var (
5455
)
5556

5657
func UploadTarball(
57-
directory string,
58+
directory fs.FS,
5859
presignedUrl string,
5960
presignedPostRequest *livekit.PresignedPostRequest,
6061
excludeFiles []string,
6162
projectType ProjectType,
6263
) error {
6364
excludeFiles = append(excludeFiles, defaultExcludePatterns...)
6465

65-
loadExcludeFiles := func(filename string) (bool, string, error) {
66-
if _, err := os.Stat(filename); err == nil {
67-
content, err := os.ReadFile(filename)
66+
loadExcludeFiles := func(dir fs.FS, filename string) (bool, string, error) {
67+
if _, err := fs.Stat(dir, filename); err == nil {
68+
content, err := fs.ReadFile(dir, filename)
6869
if err != nil {
6970
return false, "", err
7071
}
@@ -75,7 +76,7 @@ func UploadTarball(
7576

7677
foundDockerIgnore := false
7778
for _, exclude := range ignoreFilePatterns {
78-
found, content, err := loadExcludeFiles(path.Join(directory, exclude))
79+
found, content, err := loadExcludeFiles(directory, exclude)
7980
if err != nil {
8081
logger.Debugw("failed to load exclude file", "filename", exclude, "error", err)
8182
continue
@@ -89,7 +90,7 @@ func UploadTarball(
8990
// need to ensure we use a dockerignore file
9091
// if we fail to load a dockerignore file, we have to exit
9192
if !foundDockerIgnore {
92-
dockerIgnoreContent, err := fs.ReadFile(path.Join("examples", string(projectType)+".dockerignore"))
93+
dockerIgnoreContent, err := fs.ReadFile(directory, path.Join("examples", string(projectType)+".dockerignore"))
9394
if err != nil {
9495
return fmt.Errorf("failed to load exclude file %s: %w", string(projectType), err)
9596
}
@@ -105,7 +106,7 @@ func UploadTarball(
105106
excludeFiles[i] = strings.TrimSpace(exclude)
106107
}
107108

108-
checkFilesToInclude := func(path string, info os.FileInfo) bool {
109+
checkFilesToInclude := func(path string) bool {
109110
fileName := filepath.Base(path)
110111
// we have to include the Dockerfile in the upload, as it is required for the build
111112
if strings.Contains(fileName, "Dockerfile") {
@@ -123,17 +124,17 @@ func UploadTarball(
123124
// we walk the directory first to calculate the total size of the tarball
124125
// this lets the progress bar show the correct progress
125126
var totalSize int64
126-
err = filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
127+
err = fs.WalkDir(directory, ".", func(path string, d fs.DirEntry, err error) error {
127128
if err != nil {
128129
return err
129130
}
130131

131-
relPath, err := filepath.Rel(directory, path)
132+
info, err := d.Info()
132133
if err != nil {
133-
return nil
134+
return err
134135
}
135136

136-
if !checkFilesToInclude(relPath, info) {
137+
if !checkFilesToInclude(path) {
137138
return nil
138139
}
139140

@@ -166,17 +167,19 @@ func UploadTarball(
166167
tarWriter := tar.NewWriter(gzipWriter)
167168
defer tarWriter.Close()
168169

169-
err = filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
170+
err = fs.WalkDir(directory, ".", func(path string, d fs.DirEntry, err error) error {
170171
if err != nil {
171172
return err
172173
}
173174

174-
relPath, err := filepath.Rel(directory, path)
175+
info, err := d.Info()
175176
if err != nil {
176-
return fmt.Errorf("failed to calculate relative path for %s: %w", path, err)
177+
return err
177178
}
178179

179-
if !checkFilesToInclude(relPath, info) {
180+
relPath := path
181+
182+
if !checkFilesToInclude(relPath) {
180183
logger.Debugw("excluding file from tarball", "path", path)
181184
return nil
182185
}

pkg/agentfs/utils.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package agentfs
1717
import (
1818
"errors"
1919
"fmt"
20+
"io/fs"
2021
"os"
2122
"path/filepath"
2223
"strings"
@@ -110,7 +111,7 @@ func LocateLockfile(dir string, p ProjectType) (bool, string) {
110111
return false, ""
111112
}
112113

113-
func DetectProjectType(dir string) (ProjectType, error) {
114+
func DetectProjectType(dir fs.FS) (ProjectType, error) {
114115
// Node.js detection
115116
if util.FileExists(dir, "package.json") {
116117
return ProjectTypeNode, nil
@@ -127,8 +128,7 @@ func DetectProjectType(dir string) (ProjectType, error) {
127128
return ProjectTypePythonPip, nil
128129
}
129130
if util.FileExists(dir, "pyproject.toml") {
130-
tomlPath := filepath.Join(dir, "pyproject.toml")
131-
data, err := os.ReadFile(tomlPath)
131+
data, err := fs.ReadFile(dir, "pyproject.toml")
132132
if err == nil {
133133
var doc map[string]any
134134
if err := toml.Unmarshal(data, &doc); err == nil {

pkg/util/fs.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package util
1717
import (
1818
"fmt"
1919
"io"
20+
"io/fs"
2021
"os"
2122
"path"
2223
"path/filepath"
@@ -25,8 +26,8 @@ import (
2526
"github.com/livekit/protocol/utils/guid"
2627
)
2728

28-
func FileExists(dir, filename string) bool {
29-
_, err := os.Stat(filepath.Join(dir, filename))
29+
func FileExists(dir fs.FS, filename string) bool {
30+
_, err := fs.Stat(dir, filename)
3031
return err == nil
3132
}
3233

0 commit comments

Comments
 (0)