spud/cli/build.go

51 lines
1.3 KiB
Go

package cli
import (
"fmt"
"github.com/spf13/cobra"
"log"
podman "spud/podman"
service_definition "spud/service_definition"
)
func getBuildCommand() *cobra.Command {
build := &cobra.Command{
Use: "build",
Short: "Build service images.",
RunE: func(cmd *cobra.Command, args []string) error {
var pathProvided string
var fetcher service_definition.DefinitionFetcher
var def service_definition.ServiceDefinition
var err error
if pathProvided, err = cmd.PersistentFlags().GetString("definition"); err != nil {
return fmt.Errorf("%+v", err)
}
defPathType := service_definition.GetPathType(pathProvided)
if fetcher, err = service_definition.NewDefinitionFetcher(defPathType); err != nil {
return fmt.Errorf("Couldn't set up fetcher from the path provided.")
}
if def, err = fetcher.GetDefinition(pathProvided); err != nil {
return fmt.Errorf("Failed to read service definition from file: %+v", err)
}
imagesToBuild := def.Build.Images
if len(imagesToBuild) == 0 {
log.Print("No images defined - nothing to build!")
}
for _, imageDef := range imagesToBuild {
podman.Build(imageDef)
}
return nil
},
}
build.PersistentFlags().StringP("definition", "d", "./service.yml", "Path to the service definition to use.")
return build
}