diff --git a/cli/build.go b/cli/build.go new file mode 100644 index 0000000..b0134e2 --- /dev/null +++ b/cli/build.go @@ -0,0 +1,31 @@ +package cli + +import ( + "github.com/spf13/cobra" + "log" + podman "spud/podman" + service_definition "spud/service_definition" +) + +var build = &cobra.Command{ + Use: "build", + Short: "Build service images.", + Run: func(cmd *cobra.Command, args []string) { + pathProvided := args[0] + def, err := service_definition.GetServiceDefinitionFromFile(pathProvided) + + if err != nil { + log.Fatal(err) + } + + imagesToBuild := def.Build.Images + + if len(imagesToBuild) == 0 { + log.Print("No images defined - nothing to build!") + } + + for _, imageDef := range imagesToBuild { + podman.Build(imageDef) + } + }, +} diff --git a/cli/cli.go b/cli/cli.go index b92545a..8ce360a 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -10,6 +10,8 @@ var allCommands = []*cobra.Command{ start, // cli/stop_service.go stop, + // cli/build.go + build, } // Creates the root of the CLI, with all available commands diff --git a/podman/main.go b/podman/main.go index d47497a..77a0a54 100644 --- a/podman/main.go +++ b/podman/main.go @@ -3,8 +3,8 @@ package podman import ( "log" + "os" "os/exec" - service_definition "spud/service_definition" ) @@ -141,3 +141,19 @@ func CreateContainer(definition service_definition.ContainerDefinition, knownVol return nil } + +// Builds a container image. +func Build(imageDefinition service_definition.BuildImage) error { + args := []string{"build", "-f", imageDefinition.Path, "-t", imageDefinition.TagPrefix} + + command := exec.Command("podman", args...) + + command.Stdout = os.Stdout + command.Stderr = os.Stderr + + if err := command.Run(); err != nil { + return err + } + + return nil +} diff --git a/service_definition/main.go b/service_definition/main.go index 53f5805..5a2bae1 100644 --- a/service_definition/main.go +++ b/service_definition/main.go @@ -6,6 +6,15 @@ import ( "github.com/goccy/go-yaml" ) +type BuildImage struct { + Path string `yaml:"path"` + TagPrefix string `yaml:"tag"` +} + +type BuildConfiguration struct { + Images []BuildImage +} + type VolumeDefinition struct { Name string `yaml:"name"` } @@ -33,6 +42,7 @@ type ContainerDefinition struct { type ServiceDefinition struct { Name string `yaml:"name"` + Build BuildConfiguration `yaml:"build"` Volumes []VolumeDefinition `yaml:"volumes"` Containers []ContainerDefinition `yaml:"containers"` Ports []PortMapping `yaml:"ports"`