|
| 1 | +package parse |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | +) |
| 7 | + |
| 8 | +type BuildOutputType int |
| 9 | + |
| 10 | +const ( |
| 11 | + BuildOutputStdout BuildOutputType = 0 // stream tar to stdout |
| 12 | + BuildOutputLocalDir BuildOutputType = 1 |
| 13 | + BuildOutputTar BuildOutputType = 2 |
| 14 | +) |
| 15 | + |
| 16 | +// BuildOutputOptions contains the the outcome of parsing the value of a build --output flag |
| 17 | +type BuildOutputOption struct { |
| 18 | + Type BuildOutputType |
| 19 | + Path string // Only valid if Type is local dir or tar |
| 20 | +} |
| 21 | + |
| 22 | +// GetBuildOutput is responsible for parsing custom build output argument i.e `build --output` flag. |
| 23 | +// Takes `buildOutput` as string and returns BuildOutputOption |
| 24 | +func GetBuildOutput(buildOutput string) (BuildOutputOption, error) { |
| 25 | + // Support simple values, in the form --output ./mydir |
| 26 | + if !strings.Contains(buildOutput, ",") && !strings.Contains(buildOutput, "=") { |
| 27 | + if buildOutput == "-" { |
| 28 | + // Feature parity with buildkit, output tar to stdout |
| 29 | + // Read more here: https://docs.docker.com/engine/reference/commandline/build/#custom-build-outputs |
| 30 | + return BuildOutputOption{ |
| 31 | + Type: BuildOutputStdout, |
| 32 | + Path: "", |
| 33 | + }, nil |
| 34 | + } |
| 35 | + |
| 36 | + return BuildOutputOption{ |
| 37 | + Type: BuildOutputLocalDir, |
| 38 | + Path: buildOutput, |
| 39 | + }, nil |
| 40 | + } |
| 41 | + |
| 42 | + // Support complex values, in the form --output type=local,dest=./mydir |
| 43 | + var typeSelected BuildOutputType = -1 |
| 44 | + pathSelected := "" |
| 45 | + for option := range strings.SplitSeq(buildOutput, ",") { |
| 46 | + key, value, found := strings.Cut(option, "=") |
| 47 | + if !found { |
| 48 | + return BuildOutputOption{}, fmt.Errorf("invalid build output options %q, expected format key=value", buildOutput) |
| 49 | + } |
| 50 | + switch key { |
| 51 | + case "type": |
| 52 | + if typeSelected != -1 { |
| 53 | + return BuildOutputOption{}, fmt.Errorf("duplicate %q not supported", key) |
| 54 | + } |
| 55 | + switch value { |
| 56 | + case "local": |
| 57 | + typeSelected = BuildOutputLocalDir |
| 58 | + case "tar": |
| 59 | + typeSelected = BuildOutputTar |
| 60 | + default: |
| 61 | + return BuildOutputOption{}, fmt.Errorf("invalid type %q selected for build output options %q", value, buildOutput) |
| 62 | + } |
| 63 | + case "dest": |
| 64 | + if pathSelected != "" { |
| 65 | + return BuildOutputOption{}, fmt.Errorf("duplicate %q not supported", key) |
| 66 | + } |
| 67 | + pathSelected = value |
| 68 | + default: |
| 69 | + return BuildOutputOption{}, fmt.Errorf("unrecognized key %q in build output option: %q", key, buildOutput) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // Validate there is a type |
| 74 | + if typeSelected == -1 { |
| 75 | + return BuildOutputOption{}, fmt.Errorf("missing required key %q in build output option: %q", "type", buildOutput) |
| 76 | + } |
| 77 | + |
| 78 | + // Validate path |
| 79 | + if typeSelected == BuildOutputLocalDir || typeSelected == BuildOutputTar { |
| 80 | + if pathSelected == "" { |
| 81 | + return BuildOutputOption{}, fmt.Errorf("missing required key %q in build output option: %q", "dest", buildOutput) |
| 82 | + } |
| 83 | + } else { |
| 84 | + // Clear path when not needed by type |
| 85 | + pathSelected = "" |
| 86 | + } |
| 87 | + |
| 88 | + // Handle redirecting stdout for tar output |
| 89 | + if pathSelected == "-" { |
| 90 | + if typeSelected == BuildOutputTar { |
| 91 | + typeSelected = BuildOutputStdout |
| 92 | + pathSelected = "" |
| 93 | + } else { |
| 94 | + return BuildOutputOption{}, fmt.Errorf(`invalid build output option %q, only "type=tar" can be used with "dest=-"`, buildOutput) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return BuildOutputOption{ |
| 99 | + Type: typeSelected, |
| 100 | + Path: pathSelected, |
| 101 | + }, nil |
| 102 | +} |
0 commit comments