Commit f3a9b7a9 authored by Andy Cobaugh's avatar Andy Cobaugh

add ci for-each-image subcommand

parent a1a09e14
Pipeline #108710 waiting for manual action with stages
in 1 minute and 31 seconds
package cmd
import (
"fmt"
"os"
"git.psu.edu/k8s/devtool/environment"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
// ciForEachImageCmd represents the build command
var ciForEachImageCmd = &cobra.Command{
Use: "for-each-image",
Short: "run a command for each deployable image in config",
Long: "The given command is run for each deployable defined in the config file. The DT_DEPLOYABLE and DT_DOCKERFILE env vars are set to the deployable name and dockerfile. A non-zero exit code for any invocation will result in devtool exiting with error code 1. All commands will be run in succession to completion, with exit codes reported at the end.",
Args: cobra.MinimumNArgs(1),
RunE: ciForEachImage,
}
func init() {
ciCmd.AddCommand(ciForEachImageCmd)
}
type forEachImageResult struct {
name string
e error
}
func ciForEachImage(cmd *cobra.Command, args []string) error {
config := getCiConfiguration()
color.Blue("Running for-each-image: %+v", args)
var results []forEachImageResult
var errors []error
for _, d := range config.Deployables {
color.New(color.FgGreen).Printf("=== Deployable: %s\n", d.Name)
if d.LocalOnly {
color.New(color.FgYellow).Printf("Deployable %s is set to LocalOnly, skipping.\n", d.Name)
continue
}
if d.Dockerfile == "" {
color.New(color.FgYellow).Printf("Deployable %s has no Dockerfile, skipping.\n", d.Name)
continue
}
os.Setenv("DT_DEPLOYABLE", d.Name)
os.Setenv("DT_DOCKERFILE", d.Dockerfile)
for i := range args {
args[i] = os.ExpandEnv(args[i])
}
err := environment.Run(true, args[0], args[1:]...)
results = append(results, forEachImageResult{name: d.Name, e: err})
if err != nil {
color.New(color.FgHiRed).Printf("error: %s\n", err)
}
}
fmt.Println()
color.New(color.FgHiGreen).Print("Results:\n")
for _, r := range results {
fmt.Printf("%s: ", r.name)
if r.e != nil {
color.New(color.FgRed).Printf("%s\n", r.e)
errors = append(errors, r.e)
} else {
color.New(color.FgGreen).Print("Success\n")
}
}
fmt.Println()
if len(errors) > 0 {
return fmt.Errorf("%+v", errors)
}
return nil
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment