Skip to content

Commit

Permalink
refactor: remove recipe wide workdir state
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdaclan committed Jul 1, 2024
1 parent 51a0d4c commit 683dd07
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 17 deletions.
1 change: 0 additions & 1 deletion api/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ type Source struct {
type Recipe struct {
Name string
Id string
Cwd string
Stages []Stage
Path string
ParentPath string
Expand Down
21 changes: 7 additions & 14 deletions core/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,10 @@ func BuildContainerfile(recipe *api.Recipe) error {
if len(stage.Copy) > 0 {
for _, copy := range stage.Copy {
if len(copy.Paths) > 0 {
if copy.Workdir != "" && copy.Workdir != recipe.Cwd {
if copy.Workdir != "" {
_, err = containerfile.WriteString(
fmt.Sprintf("WORKDIR %s\n", copy.Workdir),
)
recipe.Cwd = copy.Workdir
if err != nil {
return err
}
Expand Down Expand Up @@ -148,11 +147,10 @@ func BuildContainerfile(recipe *api.Recipe) error {
// RUN(S)
if !stage.SingleLayer {
if len(stage.Runs.Commands) > 0 {
if stage.Runs.Workdir != "" && stage.Runs.Workdir != recipe.Cwd {
if stage.Runs.Workdir != "" {
_, err = containerfile.WriteString(
fmt.Sprintf("WORKDIR %s\n", stage.Runs.Workdir),
)
recipe.Cwd = stage.Runs.Workdir
if err != nil {
return err
}
Expand Down Expand Up @@ -182,11 +180,10 @@ func BuildContainerfile(recipe *api.Recipe) error {
if len(stage.Adds) > 0 {
for _, add := range stage.Adds {
if len(add.SrcDst) > 0 {
if add.Workdir != "" && add.Workdir != recipe.Cwd {
if add.Workdir != "" {
_, err = containerfile.WriteString(
fmt.Sprintf("WORKDIR %s\n", add.Workdir),
)
recipe.Cwd = add.Workdir
if err != nil {
return err
}
Expand Down Expand Up @@ -222,11 +219,10 @@ func BuildContainerfile(recipe *api.Recipe) error {
continue
}

if cmd.Workdir != "" && cmd.Workdir != recipe.Cwd {
if cmd.Workdir != "" {
_, err = containerfile.WriteString(
fmt.Sprintf("WORKDIR %s\n", cmd.Workdir),
)
recipe.Cwd = cmd.Workdir
if err != nil {
return err
}
Expand All @@ -244,11 +240,10 @@ func BuildContainerfile(recipe *api.Recipe) error {
// SINGLE LAYER
if stage.SingleLayer {
if len(stage.Runs.Commands) > 0 {
if stage.Runs.Workdir != "" && stage.Runs.Workdir != recipe.Cwd {
if stage.Runs.Workdir != "" {
_, err = containerfile.WriteString(
fmt.Sprintf("WORKDIR %s\n", stage.Runs.Workdir),
)
recipe.Cwd = stage.Runs.Workdir
if err != nil {
return err
}
Expand Down Expand Up @@ -287,11 +282,10 @@ func BuildContainerfile(recipe *api.Recipe) error {
}

// CMD
if stage.Cmd.Workdir != "" && stage.Cmd.Workdir != recipe.Cwd {
if stage.Cmd.Workdir != "" {
_, err = containerfile.WriteString(
fmt.Sprintf("WORKDIR %s\n", stage.Cmd.Workdir),
)
recipe.Cwd = stage.Cmd.Workdir
if err != nil {
return err
}
Expand All @@ -306,11 +300,10 @@ func BuildContainerfile(recipe *api.Recipe) error {
}

// ENTRYPOINT
if stage.Entrypoint.Workdir != "" && stage.Entrypoint.Workdir != recipe.Cwd {
if stage.Entrypoint.Workdir != "" {
_, err = containerfile.WriteString(
fmt.Sprintf("WORKDIR %s\n", stage.Entrypoint.Workdir),
)
recipe.Cwd = stage.Entrypoint.Workdir
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions core/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
// Does not validate the recipe but it will catch some errors
// a proper validation will be done in the future
func LoadRecipe(path string) (*api.Recipe, error) {
recipe := &api.Recipe{Cwd: "/"}
recipe := &api.Recipe{}

// we use the absolute path to the recipe file as the
// root path for the recipe and all its files
Expand Down Expand Up @@ -94,7 +94,7 @@ func LoadRecipe(path string) (*api.Recipe, error) {
// the includes directory is the place where we store all the
// files to be included in the container, this is useful for
// example to include configuration files. Each file must follow
// the File Hierachy Standard (FHS) and be placed in the correct
// the File Hierarchy Standard (FHS) and be placed in the correct
// directory. For example, if you want to include a file in
// /etc/nginx/nginx.conf you must place it in includes/etc/nginx/nginx.conf
// so it will be copied to the correct location in the container
Expand Down

0 comments on commit 683dd07

Please sign in to comment.