Skip to content

Commit

Permalink
fix: when scanning appdir, avoid reading in files we will ignore
Browse files Browse the repository at this point in the history
Also wrap a `strings.TrimSpace()` around the string-form usage.

Signed-off-by: Nick Mitchell <nickm@us.ibm.com>
  • Loading branch information
starpit committed Nov 28, 2024
1 parent e307176 commit 27ef22d
Showing 1 changed file with 36 additions and 12 deletions.
48 changes: 36 additions & 12 deletions pkg/fe/builder/overlay/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ func copySourceIntoTemplate(appname, sourcePath, templatePath string, opts Optio
return appVersion, nil
}

func readString(path string) (string, error) {
b, err := os.ReadFile(path)
if err != nil {
return "", err
}
return strings.TrimSpace(string(b)), nil
}

// Formulate an HLIR for the source in the given `sourcePath`
func applicationFromSource(appname, sourcePath, templatePath string, opts Options) (appVersion string, app hlir.Application, err error) {
app = hlir.NewWorkerApplication(appname)
Expand All @@ -57,14 +65,13 @@ func applicationFromSource(appname, sourcePath, templatePath string, opts Option
case path[len(path)-1] == '~':
// skip emacs temporary files
default:
b, err := os.ReadFile(path)
if err != nil {
return err
}

if strings.HasPrefix(path, srcPrefix) {
// Handle src/ artifacts
spec.Code = append(spec.Code, hlir.Code{Name: d.Name(), Source: string(b)})
source, err := readString(path)
if err != nil {
return err
}
spec.Code = append(spec.Code, hlir.Code{Name: d.Name(), Source: source})

switch d.Name() {
case "main.sh":
Expand All @@ -84,16 +91,33 @@ func applicationFromSource(appname, sourcePath, templatePath string, opts Option
return err
}
case "requirements.txt":
spec.Needs = append(spec.Needs, hlir.Needs{Name: "python", Version: "latest", Requirements: string(b)})
req, err := readString(path)
if err != nil {
return err
}
spec.Needs = append(spec.Needs, hlir.Needs{Name: "python", Version: "latest", Requirements: req})
case "memory", "memory.txt":
spec.MinMemory = string(b)
mem, err := readString(path)
if err != nil {
return err
}
spec.MinMemory = mem
case "image":
spec.Image = string(b)
image, err := readString(path)
if err != nil {
return err
}
spec.Image = image
case "command":
spec.Command = string(b)
case "env.yaml":
err := yaml.Unmarshal(b, &spec.Env)
command, err := readString(path)
if err != nil {
return err
}
spec.Command = command
case "env.yaml":
if b, err := os.ReadFile(path); err != nil {
return err
} else if err := yaml.Unmarshal(b, &spec.Env); err != nil {
return fmt.Errorf("Error parsing env.yaml: %v", err)
}
default:
Expand Down

0 comments on commit 27ef22d

Please sign in to comment.