feat: add build command + build,images config

This commit is contained in:
Marc 2024-09-26 23:29:52 -04:00
parent 4c3887857f
commit b5408fda1a
Signed by: marc
GPG key ID: 048E042F22B5DC79
4 changed files with 60 additions and 1 deletions

31
cli/build.go Normal file
View file

@ -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)
}
},
}

View file

@ -10,6 +10,8 @@ var allCommands = []*cobra.Command{
start, start,
// cli/stop_service.go // cli/stop_service.go
stop, stop,
// cli/build.go
build,
} }
// Creates the root of the CLI, with all available commands // Creates the root of the CLI, with all available commands

View file

@ -3,8 +3,8 @@ package podman
import ( import (
"log" "log"
"os"
"os/exec" "os/exec"
service_definition "spud/service_definition" service_definition "spud/service_definition"
) )
@ -141,3 +141,19 @@ func CreateContainer(definition service_definition.ContainerDefinition, knownVol
return nil 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
}

View file

@ -6,6 +6,15 @@ import (
"github.com/goccy/go-yaml" "github.com/goccy/go-yaml"
) )
type BuildImage struct {
Path string `yaml:"path"`
TagPrefix string `yaml:"tag"`
}
type BuildConfiguration struct {
Images []BuildImage
}
type VolumeDefinition struct { type VolumeDefinition struct {
Name string `yaml:"name"` Name string `yaml:"name"`
} }
@ -33,6 +42,7 @@ type ContainerDefinition struct {
type ServiceDefinition struct { type ServiceDefinition struct {
Name string `yaml:"name"` Name string `yaml:"name"`
Build BuildConfiguration `yaml:"build"`
Volumes []VolumeDefinition `yaml:"volumes"` Volumes []VolumeDefinition `yaml:"volumes"`
Containers []ContainerDefinition `yaml:"containers"` Containers []ContainerDefinition `yaml:"containers"`
Ports []PortMapping `yaml:"ports"` Ports []PortMapping `yaml:"ports"`