Created
November 19, 2017 22:33
-
-
Save andygarfield/2cc5b6bb98ddf5f4187e51a214eb5273 to your computer and use it in GitHub Desktop.
Convert Dockerfile RUN's
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// Dockerfiles build faster when Docker caches the result of your RUN commands. But, the | |
// resulting image can only be small if the the RUN commands are combined into only one | |
// RUN command. This script converts Dockerfiles with multiple RUN commands in a row in | |
// a single RUN command separated by "&& \". | |
// Use: Run the command while in the current directory to convert the Dockerfile there, | |
// or give it an optional folder path. | |
// Install: | |
// Install Go | |
// Build with "go build r2a.go" | |
import ( | |
"io/ioutil" | |
"os" | |
"strings" | |
) | |
func main() { | |
dir := "./" | |
if len(os.Args) > 1 { | |
arg := os.Args[1] | |
if !strings.HasSuffix(arg, "/") { | |
dir = os.Args[1] + "/" | |
} else { | |
dir = os.Args[1] | |
} | |
if !strings.HasPrefix(arg, "/") { | |
dir = "./" + dir | |
} | |
} | |
df, err := ioutil.ReadFile(dir + "Dockerfile") | |
if err != nil { | |
panic("No Dockerfile in this directory") | |
} | |
lineSlice := strings.Split(string(df), "\n") | |
newDF := "" | |
runSeq := false | |
for _, line := range lineSlice { | |
if strings.HasPrefix(line, "RUN") { | |
if runSeq { | |
newDF = newDF[:len(newDF)-1] | |
newDF += " && \\\n " + line[3:] | |
} | |
runSeq = true | |
newDF += line + "\n" | |
continue | |
} else { | |
runSeq = false | |
newDF += line + "\n" | |
continue | |
} | |
} | |
ioutil.WriteFile(dir+"Dockerfile", []byte(newDF), 0644) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment