|
| 1 | +package support |
| 2 | + |
| 3 | +// ////////////////////////////////////////////////////////////////////////////////// // |
| 4 | +// // |
| 5 | +// Copyright (c) 2023 ESSENTIAL KAOS // |
| 6 | +// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // |
| 7 | +// // |
| 8 | +// ////////////////////////////////////////////////////////////////////////////////// // |
| 9 | + |
| 10 | +import ( |
| 11 | + "fmt" |
| 12 | + "os" |
| 13 | + "runtime" |
| 14 | + "strings" |
| 15 | + |
| 16 | + "github.com/essentialkaos/ek/v12/fmtc" |
| 17 | + "github.com/essentialkaos/ek/v12/fmtutil" |
| 18 | + "github.com/essentialkaos/ek/v12/fsutil" |
| 19 | + "github.com/essentialkaos/ek/v12/hash" |
| 20 | + "github.com/essentialkaos/ek/v12/strutil" |
| 21 | + "github.com/essentialkaos/ek/v12/system" |
| 22 | + |
| 23 | + "github.com/essentialkaos/depsy" |
| 24 | +) |
| 25 | + |
| 26 | +// ////////////////////////////////////////////////////////////////////////////////// // |
| 27 | + |
| 28 | +// ShowSupportInfo prints verbose info about application, system, dependencies and |
| 29 | +// important environment |
| 30 | +func ShowSupportInfo(app, ver, gitRev string, gomod []byte) { |
| 31 | + fmtutil.SeparatorTitleColorTag = "{s-}" |
| 32 | + fmtutil.SeparatorFullscreen = false |
| 33 | + fmtutil.SeparatorColorTag = "{s-}" |
| 34 | + fmtutil.SeparatorSize = 80 |
| 35 | + |
| 36 | + showApplicationInfo(app, ver, gitRev) |
| 37 | + showOSInfo() |
| 38 | + showDepsInfo(gomod) |
| 39 | + |
| 40 | + fmtutil.Separator(false) |
| 41 | +} |
| 42 | + |
| 43 | +// ////////////////////////////////////////////////////////////////////////////////// // |
| 44 | + |
| 45 | +// showApplicationInfo shows verbose information about application |
| 46 | +func showApplicationInfo(app, ver, gitRev string) { |
| 47 | + fmtutil.Separator(false, "APPLICATION INFO") |
| 48 | + |
| 49 | + printInfo(7, "Name", app) |
| 50 | + printInfo(7, "Version", ver) |
| 51 | + |
| 52 | + printInfo(7, "Go", fmtc.Sprintf( |
| 53 | + "%s {s}(%s/%s){!}", |
| 54 | + strings.TrimLeft(runtime.Version(), "go"), |
| 55 | + runtime.GOOS, runtime.GOARCH, |
| 56 | + )) |
| 57 | + |
| 58 | + if gitRev != "" { |
| 59 | + if !fmtc.DisableColors && fmtc.IsTrueColorSupported() { |
| 60 | + printInfo(7, "Git SHA", gitRev+getHashColorBullet(gitRev)) |
| 61 | + } else { |
| 62 | + printInfo(7, "Git SHA", gitRev) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + bin, _ := os.Executable() |
| 67 | + binSHA := hash.FileHash(bin) |
| 68 | + |
| 69 | + if binSHA != "" { |
| 70 | + binSHA = strutil.Head(binSHA, 7) |
| 71 | + if !fmtc.DisableColors && fmtc.IsTrueColorSupported() { |
| 72 | + printInfo(7, "Bin SHA", binSHA+getHashColorBullet(binSHA)) |
| 73 | + } else { |
| 74 | + printInfo(7, "Bin SHA", binSHA) |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// showOSInfo shows verbose information about system |
| 80 | +func showOSInfo() { |
| 81 | + osInfo, err := system.GetOSInfo() |
| 82 | + |
| 83 | + if err == nil { |
| 84 | + fmtutil.Separator(false, "OS INFO") |
| 85 | + |
| 86 | + printInfo(12, "Name", osInfo.Name) |
| 87 | + printInfo(12, "Pretty Name", osInfo.PrettyName) |
| 88 | + printInfo(12, "Version", osInfo.VersionID) |
| 89 | + printInfo(12, "ID", osInfo.ID) |
| 90 | + printInfo(12, "ID Like", osInfo.IDLike) |
| 91 | + printInfo(12, "Version ID", osInfo.VersionID) |
| 92 | + printInfo(12, "Version Code", osInfo.VersionCodename) |
| 93 | + printInfo(12, "CPE", osInfo.CPEName) |
| 94 | + } |
| 95 | + |
| 96 | + systemInfo, err := system.GetSystemInfo() |
| 97 | + |
| 98 | + if err != nil { |
| 99 | + return |
| 100 | + } else { |
| 101 | + if osInfo == nil { |
| 102 | + fmtutil.Separator(false, "SYSTEM INFO") |
| 103 | + printInfo(12, "Name", systemInfo.OS) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + printInfo(12, "Arch", systemInfo.Arch) |
| 108 | + printInfo(12, "Kernel", systemInfo.Kernel) |
| 109 | + |
| 110 | + containerEngine := "No" |
| 111 | + |
| 112 | + switch { |
| 113 | + case fsutil.IsExist("/.dockerenv"): |
| 114 | + containerEngine = "Yes (Docker)" |
| 115 | + case fsutil.IsExist("/run/.containerenv"): |
| 116 | + containerEngine = "Yes (Podman)" |
| 117 | + } |
| 118 | + |
| 119 | + fmtc.NewLine() |
| 120 | + |
| 121 | + printInfo(12, "Container", containerEngine) |
| 122 | +} |
| 123 | + |
| 124 | +// showDepsInfo shows information about all dependencies |
| 125 | +func showDepsInfo(gomod []byte) { |
| 126 | + deps := depsy.Extract(gomod, false) |
| 127 | + |
| 128 | + if len(deps) == 0 { |
| 129 | + return |
| 130 | + } |
| 131 | + |
| 132 | + fmtutil.Separator(false, "DEPENDENCIES") |
| 133 | + |
| 134 | + for _, dep := range deps { |
| 135 | + if dep.Extra == "" { |
| 136 | + fmtc.Printf(" {s}%8s{!} %s\n", dep.Version, dep.Path) |
| 137 | + } else { |
| 138 | + fmtc.Printf(" {s}%8s{!} %s {s-}(%s){!}\n", dep.Version, dep.Path, dep.Extra) |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +// getHashColorBullet return bullet with color from hash |
| 144 | +func getHashColorBullet(v string) string { |
| 145 | + if len(v) > 6 { |
| 146 | + v = strutil.Head(v, 6) |
| 147 | + } |
| 148 | + |
| 149 | + return fmtc.Sprintf(" {#" + strutil.Head(v, 6) + "}● {!}") |
| 150 | +} |
| 151 | + |
| 152 | +// printInfo formats and prints info record |
| 153 | +func printInfo(size int, name, value string) { |
| 154 | + name = name + ":" |
| 155 | + size++ |
| 156 | + |
| 157 | + if value == "" { |
| 158 | + fm := fmt.Sprintf(" {*}%%-%ds{!} {s-}—{!}\n", size) |
| 159 | + fmtc.Printf(fm, name) |
| 160 | + } else { |
| 161 | + fm := fmt.Sprintf(" {*}%%-%ds{!} %%s\n", size) |
| 162 | + fmtc.Printf(fm, name, value) |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +// ////////////////////////////////////////////////////////////////////////////////// // |
0 commit comments