github.com/cvmfs/docker-graphdriver@v0.0.0-20181206110523-155ec6df0521/repository-manager/lib/recipe.go (about) 1 package lib 2 3 import ( 4 "strings" 5 6 log "github.com/sirupsen/logrus" 7 8 "gopkg.in/yaml.v2" 9 ) 10 11 type YamlRecipeV1 struct { 12 Version int `yaml:"version"` 13 User string `yaml:"user"` 14 CVMFSRepo string `yaml:"cvmfs_repo"` 15 OutputFormat string `yaml:"output_format"` 16 Input []string `yaml:"input"` 17 } 18 19 type Recipe struct { 20 Wishes []WishFriendly 21 } 22 23 func ParseYamlRecipeV1(data []byte) (Recipe, error) { 24 recipeYamlV1 := YamlRecipeV1{} 25 err := yaml.Unmarshal(data, &recipeYamlV1) 26 if err != nil { 27 return Recipe{}, err 28 } 29 recipe := Recipe{} 30 for _, inputImage := range recipeYamlV1.Input { 31 input, err := ParseImage(inputImage) 32 if err != nil { 33 LogE(err).WithFields(log.Fields{"image": inputImage}).Warning("Impossible to parse the image") 34 continue 35 } 36 output := formatOutputImage(recipeYamlV1.OutputFormat, input) 37 wish, err := CreateWish(inputImage, output, recipeYamlV1.CVMFSRepo, "", recipeYamlV1.User) 38 if err != nil { 39 LogE(err).Warning("Error in creating the wish") 40 continue 41 } else { 42 recipe.Wishes = append(recipe.Wishes, wish) 43 } 44 } 45 return recipe, nil 46 } 47 48 func formatOutputImage(OutputFormat string, inputImage Image) string { 49 50 s := strings.Replace(OutputFormat, "$(scheme)", inputImage.Scheme, 5) 51 s = strings.Replace(s, "$(registry)", inputImage.Registry, 5) 52 s = strings.Replace(s, "$(repository)", inputImage.Repository, 5) 53 s = strings.Replace(s, "$(digest)", inputImage.Digest, 5) 54 s = strings.Replace(s, "$(tag)", inputImage.Tag, 5) 55 s = strings.Replace(s, "$(reference)", inputImage.GetReference(), 5) 56 s = strings.Replace(s, "$(image)", inputImage.Repository+inputImage.GetReference(), 5) 57 58 return s 59 }