From 3a42871750abb345ed02bc7cdc8e4837d02b8406 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Wed, 9 Oct 2024 12:32:50 +0300 Subject: [PATCH] [support/deps] Improve collecting and filtering dependencies info --- CHANGELOG.md | 1 + support/deps/deps.go | 53 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a30f991..9bc89d7e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### [13.6.0](https://kaos.sh/ek/13.6.0) - `[setup]` Added package to install/uninstall application as a service +- `[support/deps]` Improved collecting and filtering dependencies info - `[support/kernel]` Added simple globs support for parameter names ### [13.5.1](https://kaos.sh/ek/13.5.1) diff --git a/support/deps/deps.go b/support/deps/deps.go index 3b10c0b0..dbdae68a 100644 --- a/support/deps/deps.go +++ b/support/deps/deps.go @@ -9,6 +9,9 @@ package deps // ////////////////////////////////////////////////////////////////////////////////// // import ( + "runtime/debug" + "strings" + "github.com/essentialkaos/ek/v13/support" "github.com/essentialkaos/depsy" @@ -17,16 +20,58 @@ import ( // ////////////////////////////////////////////////////////////////////////////////// // // Extract extracts dependencies info from gomod data -func Extract(gomod []byte) []support.Dep { +func Extract(gomod []byte, withIndirect ...bool) []support.Dep { + if len(withIndirect) > 0 && withIndirect[0] { + return filterDeps(depsy.Extract(gomod, true)) + } + + return filterDeps(depsy.Extract(gomod, false)) +} + +// ////////////////////////////////////////////////////////////////////////////////// // + +// filterDeps filters dependencies from gomod using information from bundled build info +func filterDeps(deps depsy.Dependencies) []support.Dep { var result []support.Dep - for _, dep := range depsy.Extract(gomod, false) { - result = append(result, support.Dep{ + buildInfo, _ := debug.ReadBuildInfo() + + for _, dep := range deps { + depInfo := support.Dep{ Version: dep.Version, Path: dep.PrettyPath(), Extra: dep.Extra, - }) + } + + if buildInfo != nil { + hasDep, version := hasBuiltDep(dep, buildInfo) + + if !hasDep { + continue + } + + if version != "" && strings.Contains(version, "(") { + depInfo.Version = strings.Trim(version, "()") + } + } + + result = append(result, depInfo) } return result } + +// hasBuiltDep checks if given dependency is present in build info +func hasBuiltDep(dep depsy.Dependency, buildInfo *debug.BuildInfo) (bool, string) { + for _, bDep := range buildInfo.Deps { + if bDep.Path == dep.Path { + if bDep.Replace != nil { + return true, bDep.Replace.Version + } + + return true, "" + } + } + + return false, "" +}