|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/mattn/go-shellwords" |
| 12 | + |
| 13 | + "github.com/kubeshop/botkube/internal/loggerx" |
| 14 | +) |
| 15 | + |
| 16 | +func main() { |
| 17 | + pluginTargets := flag.String("plugin-targets", getEnv("PLUGIN_TARGETS", ""), "Comma separated list of specific targets to build. If empty, all targets are built.") |
| 18 | + outputMode := flag.String("output-mode", getEnv("OUTPUT_MODE", "archive"), "Output format. Allowed values: binary or archive.") |
| 19 | + buildSingle := flag.Bool("single-platform", os.Getenv("SINGLE_PLATFORM") != "", "If specified, builds only for current GOOS and GOARCH.") |
| 20 | + |
| 21 | + flag.Parse() |
| 22 | + |
| 23 | + switch *outputMode { |
| 24 | + case "archive": |
| 25 | + if *pluginTargets != "" { |
| 26 | + log.Fatal("Cannot build specific targets in archive mode") |
| 27 | + } |
| 28 | + // to produce archives, we need to run release instead of build |
| 29 | + releaseWithoutPublishing() |
| 30 | + case "binary": |
| 31 | + buildPlugins(*pluginTargets, *buildSingle) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func getEnv(key, defaultValue string) string { |
| 36 | + value, exists := os.LookupEnv(key) |
| 37 | + if !exists { |
| 38 | + return defaultValue |
| 39 | + } |
| 40 | + return value |
| 41 | +} |
| 42 | + |
| 43 | +func buildPlugins(pluginTargets string, single bool) { |
| 44 | + command := "goreleaser build -f .goreleaser.plugin.yaml --clean --snapshot" |
| 45 | + for _, target := range strings.Split(pluginTargets, ",") { |
| 46 | + if strings.TrimSpace(target) == "" { |
| 47 | + continue |
| 48 | + } |
| 49 | + command += fmt.Sprintf(" --id %s", target) |
| 50 | + } |
| 51 | + |
| 52 | + if single { |
| 53 | + command += "--single-target" |
| 54 | + } |
| 55 | + |
| 56 | + runCommand(command) |
| 57 | +} |
| 58 | + |
| 59 | +func runCommand(command string) { |
| 60 | + log.Printf("Running: %s", command) |
| 61 | + args, err := shellwords.Parse(command) |
| 62 | + loggerx.ExitOnError(err, "while parsing command") |
| 63 | + |
| 64 | + bin, binArgs := args[0], args[1:] |
| 65 | + |
| 66 | + //nolint:gosec |
| 67 | + cmd := exec.Command(bin, binArgs...) |
| 68 | + cmd.Stdout = os.Stdout |
| 69 | + cmd.Stderr = os.Stderr |
| 70 | + |
| 71 | + loggerx.ExitOnError(cmd.Run(), "while running command") |
| 72 | +} |
| 73 | + |
| 74 | +func releaseWithoutPublishing() { |
| 75 | + runCommand("goreleaser release -f .goreleaser.plugin.yaml --clean --snapshot") |
| 76 | +} |
0 commit comments