RunDexecContainer runs an anonymous Docker container with a Docker Exec image, mounting the specified sources and includes and passing the list of sources and arguments to the entrypoint.
(cliParser cli.CLI)
| 17 | // image, mounting the specified sources and includes and passing the |
| 18 | // list of sources and arguments to the entrypoint. |
| 19 | func RunDexecContainer(cliParser cli.CLI) int { |
| 20 | options := cliParser.Options |
| 21 | |
| 22 | shouldClean := len(options[cli.CleanFlag]) > 0 |
| 23 | updateImage := len(options[cli.UpdateFlag]) > 0 |
| 24 | |
| 25 | client, err := docker.NewClientFromEnv() |
| 26 | |
| 27 | if err != nil { |
| 28 | log.Fatal(err) |
| 29 | } |
| 30 | |
| 31 | if shouldClean { |
| 32 | images, err := client.ListImages(docker.ListImagesOptions{ |
| 33 | All: true, |
| 34 | }) |
| 35 | if err != nil { |
| 36 | log.Fatal(err) |
| 37 | } |
| 38 | for _, image := range images { |
| 39 | for _, tag := range image.RepoTags { |
| 40 | repoRegex := regexp.MustCompile("^dexec/lang-[^:\\s]+(:.+)?$") |
| 41 | if match := repoRegex.MatchString(tag); match { |
| 42 | client.RemoveImage(image.ID) |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | dexecImage, err := dexec.ImageFromOptions(options) |
| 49 | if err != nil { |
| 50 | log.Fatal(err) |
| 51 | } |
| 52 | |
| 53 | dockerImage := fmt.Sprintf("%s:%s", dexecImage.Image, dexecImage.Version) |
| 54 | |
| 55 | if err = dexec.FetchImage( |
| 56 | dexecImage.Image, |
| 57 | dexecImage.Version, |
| 58 | updateImage, |
| 59 | client); err != nil { |
| 60 | log.Fatal(err) |
| 61 | } |
| 62 | |
| 63 | var sourceBasenames []string |
| 64 | for _, source := range options[cli.Source] { |
| 65 | basename, _ := dexec.ExtractBasenameAndPermission(source) |
| 66 | sourceBasenames = append(sourceBasenames, []string{basename}...) |
| 67 | } |
| 68 | |
| 69 | entrypointArgs := util.JoinStringSlices( |
| 70 | sourceBasenames, |
| 71 | util.AddPrefix(options[cli.BuildArg], "-b"), |
| 72 | util.AddPrefix(options[cli.Arg], "-a"), |
| 73 | ) |
| 74 | |
| 75 | container, err := client.CreateContainer(docker.CreateContainerOptions{ |
| 76 | Config: &docker.Config{ |