From 2f87f4919dd1127cca493381b0f821b04cd0c377 Mon Sep 17 00:00:00 2001 From: Aine Date: Mon, 15 Jul 2024 14:04:17 +0300 Subject: [PATCH] add option to list already installed roles --- README.md | 8 +++++--- cmd/agru/main.go | 19 ++++++++++++++++--- internal/parser/roles.go | 12 ++++++++++++ justfile | 4 ++++ 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ccc070c..77fa41c 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ * [What's the catch?](#whats-the-catch) * [only git repos are supported](#only-git-repos-are-supported) * [only roles are supported](#only-roles-are-supported) - * [only update/install operations are supported](#only-updateinstall-operations-are-supported) + * [only list/update/install operations are supported](#only-listupdateinstall-operations-are-supported) * [Where to get?](#where-to-get) * [Binaries and distro-specific packages](#binaries-and-distro-specific-packages) * [Build yourself](#build-yourself) @@ -37,6 +37,8 @@ Because `ansible-galaxy` is slow, **very** slow. And irrational. And miss some f ``` Usage of agru: -c cleanup temporary files (default true) + -i install missing roles (default true) + -l list installed roles -p string path to install roles (default "roles/galaxy/") -r string @@ -69,9 +71,9 @@ does **not** work: No collections at this moment, at all. -### only update/install operations are supported +### only list/update/install operations are supported -No list, no role upload to galaxy, no role removal from galaxy. +No role upload to galaxy, no role removal from galaxy. In fact, galaxy API is not used at all, thus no API-related actions are supported ## Where to get? diff --git a/cmd/agru/main.go b/cmd/agru/main.go index 20fdcde..7c64953 100644 --- a/cmd/agru/main.go +++ b/cmd/agru/main.go @@ -12,6 +12,8 @@ var ( rolesPath string requirementsPath string updateRequirementsFile bool + listInstalled bool + installMissing bool verbose bool cleanup bool ) @@ -19,6 +21,8 @@ var ( func main() { flag.StringVar(&requirementsPath, "r", "requirements.yml", "ansible-galaxy requirements file") flag.StringVar(&rolesPath, "p", "roles/galaxy/", "path to install roles") + flag.BoolVar(&listInstalled, "l", false, "list installed roles") + flag.BoolVar(&installMissing, "i", true, "install missing roles") flag.BoolVar(&updateRequirementsFile, "u", false, "update requirements file if newer versions are available") flag.BoolVar(&cleanup, "c", true, "cleanup temporary files") flag.BoolVar(&verbose, "v", false, "verbose output") @@ -26,7 +30,7 @@ func main() { utils.Verbose = verbose - utils.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)) + utils.Log(fmt.Sprintf("\033[1ma\033[0mnsible-\033[1mg\033[0malaxy \033[1mr\033[0mequirements.yml \033[1mu\033[0mpdater (list=%t update=%t cleanup=%t verbose=%t)", listInstalled, updateRequirementsFile, cleanup, verbose)) utils.Log("parsing", requirementsPath) entries, installOnly := parser.ParseFile(requirementsPath) if updateRequirementsFile { @@ -34,8 +38,17 @@ func main() { parser.UpdateFile(entries, requirementsPath) } - utils.Log("installing/updating roles (if any)") - parser.InstallMissingRoles(rolesPath, parser.MergeFiles(entries, installOnly), cleanup) + if listInstalled { + installed := parser.GetInstalledRoles(rolesPath, parser.MergeFiles(entries, installOnly)) + for _, entry := range installed { + fmt.Println("-", entry.GetName()+",", entry.GetInstallInfo(rolesPath).Version) + } + } + + if installMissing { + utils.Log("installing/updating roles (if any)") + parser.InstallMissingRoles(rolesPath, parser.MergeFiles(entries, installOnly), cleanup) + } utils.Log("done") } diff --git a/internal/parser/roles.go b/internal/parser/roles.go index 5a1a4f9..497a7e4 100644 --- a/internal/parser/roles.go +++ b/internal/parser/roles.go @@ -37,6 +37,18 @@ func InstallMissingRoles(rolesPath string, entries models.File, cleanup bool) { wg.Wait() } +// GetInstalledRoles returns all roles that are already installed +func GetInstalledRoles(rolesPath string, entries models.File) models.File { + installed := models.File{} + for _, entry := range entries { + entry := entry + if entry.GetInstallInfo(rolesPath).Version != "" { + installed = append(installed, entry) + } + } + return installed +} + // getNewVersion checks for newer git tag available on the src's remote func getNewVersion(src, version string) string { if ignoredVersions[version] { diff --git a/justfile b/justfile index 5053689..ebe1ab4 100644 --- a/justfile +++ b/justfile @@ -8,6 +8,10 @@ update *flags: go mod tidy go mod vendor +# install app +install: + go install ./cmd/agru + # run linter lint: golangci-lint run ./...