// Run executes the build process. func (b *Builder) Run() error { // Parse the Dockerfile. dockerfile, err := os.Open(b.dockerfilePath) if err != nil { return fmt.Errorf("unable to open Dockerfile: %s", err) } defer dockerfile.Close() commands, err := parser.Parse(dockerfile) if err != nil { return fmt.Errorf("unable to parse Dockerfile: %s", err) } if len(commands) == 0 { return fmt.Errorf("no commands found in Dockerfile") } for i, command := range commands { if err := b.dispatch(i, command); err != nil { return err } } // create container and commit if we need to (because of trailing // metadata directives). if b.uncommitted && !b.probeCache() { b.containerID, err = b.createContainer([]string{"/bin/sh", "-c"}, []string{"#(nop)"}, false) if err != nil { return fmt.Errorf("unable to create container: %s", err) } if err := b.commit(); err != nil { return fmt.Errorf("unable to commit container image: %s", err) } } imageName := b.imageID if named, hasName := b.ref.(reference.Named); hasName { imageName = b.ref.String() var tag string if tagged, isTagged := named.(reference.Tagged); isTagged { tag = tagged.Tag() } if err := b.setTag(b.imageID, named.Name(), tag); err != nil { return fmt.Errorf("unable to tag built image: %s", err) } } fmt.Fprintf(b.out, "Successfully built %s\n", imageName) return nil }
func main() { input := os.Stdin if len(os.Args) > 1 { var err error input, err = os.Open(os.Args[1]) if err != nil { log.Fatal(err) } defer input.Close() } commands, err := parser.Parse(input) if err != nil { log.Fatalf("unable to parse input: %s", err) } for _, cmd := range commands { fmt.Printf("%#v\n", cmd) } }