From f4cd7fa88f5575554489aacf13ea215cdcf5a212 Mon Sep 17 00:00:00 2001 From: Ira Limitanei Date: Tue, 25 Jun 2024 18:30:42 +0900 Subject: [PATCH] feat: add support for multiple adds like copy --- api/structs.go | 2 +- core/build.go | 36 +++++++++++++++++++++--------------- core/loader.go | 12 +++++++----- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/api/structs.go b/api/structs.go index f1bbcc2..e7ef3c7 100644 --- a/api/structs.go +++ b/api/structs.go @@ -32,7 +32,7 @@ type Stage struct { Copy []Copy `json:"copy"` Labels map[string]string `json:"labels"` Env map[string]string `json:"env"` - Adds Add `json:"adds"` + Adds []Add `json:"adds"` Args map[string]string `json:"args"` Runs Run `json:"runs"` Expose map[string]string `json:"expose"` diff --git a/core/build.go b/core/build.go index 46cdfbb..5c2c808 100644 --- a/core/build.go +++ b/core/build.go @@ -179,21 +179,27 @@ func BuildContainerfile(recipe *api.Recipe) error { } // ADDS - if stage.Adds.Workdir != "" && stage.Adds.Workdir != recipe.Cwd { - _, err = containerfile.WriteString( - fmt.Sprintf("WORKDIR %s\n", stage.Adds.Workdir), - ) - recipe.Cwd = stage.Adds.Workdir - if err != nil { - return err - } - } - for key, value := range stage.Adds.SrcDst { - _, err = containerfile.WriteString( - fmt.Sprintf("ADD %s %s\n", key, value), - ) - if err != nil { - return err + if len(stage.Adds) > 0 { + for _, add := range stage.Adds { + if len(add.SrcDst) > 0 { + if add.Workdir != "" && add.Workdir != recipe.Cwd { + _, err = containerfile.WriteString( + fmt.Sprintf("WORKDIR %s\n", add.Workdir), + ) + recipe.Cwd = add.Workdir + if err != nil { + return err + } + } + for key, value := range add.SrcDst { + _, err = containerfile.WriteString( + fmt.Sprintf("ADD %s %s\n", key, value), + ) + if err != nil { + return err + } + } + } } } diff --git a/core/loader.go b/core/loader.go index 60ea32f..6d50da7 100644 --- a/core/loader.go +++ b/core/loader.go @@ -109,11 +109,13 @@ func LoadRecipe(path string) (*api.Recipe, error) { for i, stage := range recipe.Stages { // here we check if the extra Adds path exists - for src := range stage.Adds.SrcDst { - fullPath := filepath.Join(filepath.Dir(recipePath), src) - _, err = os.Stat(fullPath) - if os.IsNotExist(err) { - return nil, err + for _, add := range stage.Adds { + for src := range add.SrcDst { + fullPath := filepath.Join(filepath.Dir(recipePath), src) + _, err = os.Stat(fullPath) + if os.IsNotExist(err) { + return nil, err + } } }