agru/main.go

68 lines
1.7 KiB
Go
Raw Normal View History

2023-03-16 22:50:02 +02:00
package main
import (
"flag"
2024-05-30 12:50:47 +03:00
"fmt"
2023-03-16 22:50:02 +02:00
"os/exec"
"strings"
)
var (
rolesPath string
requirementsPath string
updateRequirementsFile bool
2023-03-25 16:43:50 +02:00
verbose bool
cleanup bool
)
2023-03-16 22:50:02 +02:00
func main() {
flag.StringVar(&requirementsPath, "r", "requirements.yml", "ansible-galaxy requirements file")
flag.StringVar(&rolesPath, "p", "roles/galaxy/", "path to install roles")
flag.BoolVar(&updateRequirementsFile, "u", false, "update requirements file if newer versions are available")
flag.BoolVar(&cleanup, "c", true, "cleanup temporary files")
2023-03-25 16:43:50 +02:00
flag.BoolVar(&verbose, "v", false, "verbose output")
2023-03-16 22:50:02 +02:00
flag.Parse()
2024-05-30 12:50:47 +03:00
log(fmt.Sprintf("\033[1ma\033[0mnsible-\033[1mg\033[0malaxy \033[1mr\033[0mequirements.yml \033[1mu\033[0mpdater (update=%t cleanup=%t verbose=%t)", updateRequirementsFile, cleanup, verbose))
log("parsing", requirementsPath)
entries, installOnly := parseRequirements(requirementsPath)
if updateRequirementsFile {
2024-05-30 12:50:47 +03:00
log("updating", requirementsPath)
updateRequirements(entries)
2023-03-16 22:50:02 +02:00
}
2024-05-30 12:50:47 +03:00
log("installing/updating roles (if any)")
installMissingRoles(mergeRequirementsEntries(entries, installOnly))
2024-05-30 12:50:47 +03:00
log("done")
2023-03-16 22:50:02 +02:00
}
2023-10-13 23:34:01 +03:00
func execute(command, dir string) (string, error) {
2023-03-16 22:50:02 +02:00
slice := strings.Split(command, " ")
2023-10-13 23:34:01 +03:00
cmd := exec.Command(slice[0], slice[1:]...) //nolint:gosec // that's intended
cmd.Dir = dir
out, err := cmd.CombinedOutput()
2024-05-30 12:50:47 +03:00
debug("execute")
debug(" command:", command)
debug(" chdir:", dir)
if out != nil {
debug(" output:", strings.TrimSuffix(string(out), "\n"))
2023-03-25 16:43:50 +02:00
}
2023-03-16 22:50:02 +02:00
if out == nil {
return "", err
}
2023-03-16 23:29:28 +02:00
2023-03-16 22:50:02 +02:00
return strings.TrimSuffix(string(out), "\n"), err
}
2024-05-30 12:50:47 +03:00
func log(v ...any) {
v = append([]any{"[a.g.r.u]"}, v...)
fmt.Println(v...)
}
func debug(v ...any) {
if verbose {
log(v...)
}
}