From dabfd25fb291326ff7b8552bc94fc6683313d54a Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Mon, 25 Dec 2023 14:09:00 +0300 Subject: [PATCH] Code refactoring --- cli/cli.go | 57 +++++++++++------------------------------- cli/support/support.go | 53 ++++++++++++++++++++++++++++----------- go.mod | 2 +- go.sum | 27 ++------------------ 4 files changed, 56 insertions(+), 83 deletions(-) diff --git a/cli/cli.go b/cli/cli.go index a4e57f1..356e685 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -19,6 +19,7 @@ import ( "github.com/essentialkaos/ek/v12/fsutil" "github.com/essentialkaos/ek/v12/options" "github.com/essentialkaos/ek/v12/strutil" + "github.com/essentialkaos/ek/v12/terminal/tty" "github.com/essentialkaos/ek/v12/usage" "github.com/essentialkaos/ek/v12/usage/completion/bash" "github.com/essentialkaos/ek/v12/usage/completion/fish" @@ -36,7 +37,7 @@ import ( // Basic utility info const ( APP = "rsz" - VER = "0.0.5" + VER = "0.0.6" DESC = "Simple utility for image resizing" ) @@ -62,8 +63,8 @@ var optMap = options.Map{ OPT_FILTER: {Value: "CatmullRom"}, OPT_LIST_FILTERS: {Type: options.BOOL}, OPT_NO_COLOR: {Type: options.BOOL}, - OPT_HELP: {Type: options.BOOL, Alias: "u:usage"}, - OPT_VER: {Type: options.BOOL, Alias: "ver"}, + OPT_HELP: {Type: options.BOOL}, + OPT_VER: {Type: options.MIXED}, OPT_VERB_VER: {Type: options.BOOL}, OPT_COMPLETION: {}, @@ -113,16 +114,16 @@ func Init(gitRev string, gomod []byte) { case options.Has(OPT_GENERATE_MAN): os.Exit(genMan()) case options.GetB(OPT_VER): - showAbout(gitRev) + genAbout(gitRev).Print(options.GetS(OPT_VER)) return case options.GetB(OPT_VERB_VER): - support.ShowSupportInfo(APP, VER, gitRev, gomod) + support.Print(APP, VER, gitRev, gomod) return case options.GetB(OPT_LIST_FILTERS): listFilters() return case options.GetB(OPT_HELP) || len(args) < 3: - showUsage() + genUsage().Print() return } @@ -131,24 +132,7 @@ func Init(gitRev string, gomod []byte) { // preConfigureUI preconfigures UI based on information about user terminal func preConfigureUI() { - term := os.Getenv("TERM") - - fmtc.DisableColors = true - - if term != "" { - switch { - case strings.Contains(term, "xterm"), - strings.Contains(term, "color"), - term == "screen": - fmtc.DisableColors = false - } - } - - if !fsutil.IsCharacterDevice("/dev/stdout") && os.Getenv("FAKETTY") == "" { - fmtc.DisableColors = true - } - - if os.Getenv("NO_COLOR") != "" { + if !tty.IsTTY() { fmtc.DisableColors = true } } @@ -217,6 +201,10 @@ func resizeImage(srcImage, outImage, size string) error { w, h, err := parseSize(size, img.Bounds()) + if err != nil { + return fmt.Errorf("Can't get image size: %v", err.Error()) + } + img = imaging.Resize(img, w, h, filter) err = imaging.Save(img, outImage) @@ -346,11 +334,6 @@ func printError(f string, a ...interface{}) { fmtc.Fprintf(os.Stderr, "{r}"+f+"{!}\n", a...) } -// printError prints warning message to console -func printWarn(f string, a ...interface{}) { - fmtc.Fprintf(os.Stderr, "{y}"+f+"{!}\n", a...) -} - // printErrorAndExit print error message and exit with exit code 1 func printErrorAndExit(f string, a ...interface{}) { printError(f, a...) @@ -359,27 +342,17 @@ func printErrorAndExit(f string, a ...interface{}) { // ////////////////////////////////////////////////////////////////////////////////// // -// showUsage prints usage info -func showUsage() { - genUsage().Render() -} - -// showAbout prints info about version -func showAbout(gitRev string) { - genAbout(gitRev).Render() -} - // genCompletion generates completion for different shells func genCompletion() int { info := genUsage() switch options.GetS(OPT_COMPLETION) { case "bash": - fmt.Printf(bash.Generate(info, "rsz")) + fmt.Print(bash.Generate(info, "rsz")) case "fish": - fmt.Printf(fish.Generate(info, "rsz")) + fmt.Print(fish.Generate(info, "rsz")) case "zsh": - fmt.Printf(zsh.Generate(info, optMap, "rsz")) + fmt.Print(zsh.Generate(info, optMap, "rsz")) default: return 1 } diff --git a/cli/support/support.go b/cli/support/support.go index 6fb1347..74070a5 100644 --- a/cli/support/support.go +++ b/cli/support/support.go @@ -11,23 +11,24 @@ import ( "fmt" "os" "runtime" + "runtime/debug" "strings" "github.com/essentialkaos/ek/v12/fmtc" "github.com/essentialkaos/ek/v12/fmtutil" - "github.com/essentialkaos/ek/v12/fsutil" "github.com/essentialkaos/ek/v12/hash" "github.com/essentialkaos/ek/v12/strutil" "github.com/essentialkaos/ek/v12/system" + "github.com/essentialkaos/ek/v12/system/container" "github.com/essentialkaos/depsy" ) // ////////////////////////////////////////////////////////////////////////////////// // -// ShowSupportInfo prints verbose info about application, system, dependencies and +// Print prints verbose info about application, system, dependencies and // important environment -func ShowSupportInfo(app, ver, gitRev string, gomod []byte) { +func Print(app, ver, gitRev string, gomod []byte) { fmtutil.SeparatorTitleColorTag = "{s-}" fmtutil.SeparatorFullscreen = false fmtutil.SeparatorColorTag = "{s-}" @@ -55,6 +56,10 @@ func showApplicationInfo(app, ver, gitRev string) { runtime.GOOS, runtime.GOARCH, )) + if gitRev == "" { + gitRev = extractGitRevFromBuildInfo() + } + if gitRev != "" { if !fmtc.DisableColors && fmtc.IsTrueColorSupported() { printInfo(7, "Git SHA", gitRev+getHashColorBullet(gitRev)) @@ -83,13 +88,14 @@ func showOSInfo() { if err == nil { fmtutil.Separator(false, "OS INFO") - printInfo(12, "Name", osInfo.Name) - printInfo(12, "Pretty Name", osInfo.PrettyName) - printInfo(12, "Version", osInfo.VersionID) + printInfo(12, "Name", osInfo.ColoredName()) + printInfo(12, "Pretty Name", osInfo.ColoredPrettyName()) + printInfo(12, "Version", osInfo.Version) printInfo(12, "ID", osInfo.ID) printInfo(12, "ID Like", osInfo.IDLike) printInfo(12, "Version ID", osInfo.VersionID) printInfo(12, "Version Code", osInfo.VersionCodename) + printInfo(12, "Platform ID", osInfo.PlatformID) printInfo(12, "CPE", osInfo.CPEName) } @@ -97,11 +103,9 @@ func showOSInfo() { if err != nil { return - } else { - if osInfo == nil { - fmtutil.Separator(false, "SYSTEM INFO") - printInfo(12, "Name", systemInfo.OS) - } + } else if osInfo == nil { + fmtutil.Separator(false, "SYSTEM INFO") + printInfo(12, "Name", systemInfo.OS) } printInfo(12, "Arch", systemInfo.Arch) @@ -109,11 +113,13 @@ func showOSInfo() { containerEngine := "No" - switch { - case fsutil.IsExist("/.dockerenv"): + switch container.GetEngine() { + case container.DOCKER: containerEngine = "Yes (Docker)" - case fsutil.IsExist("/run/.containerenv"): + case container.PODMAN: containerEngine = "Yes (Podman)" + case container.LXC: + containerEngine = "Yes (LXC)" } fmtc.NewLine() @@ -140,6 +146,23 @@ func showDepsInfo(gomod []byte) { } } +// extractGitRevFromBuildInfo extracts git SHA from embedded build info +func extractGitRevFromBuildInfo() string { + info, ok := debug.ReadBuildInfo() + + if !ok { + return "" + } + + for _, s := range info.Settings { + if s.Key == "vcs.revision" && len(s.Value) > 7 { + return s.Value[:7] + } + } + + return "" +} + // getHashColorBullet return bullet with color from hash func getHashColorBullet(v string) string { if len(v) > 6 { @@ -151,7 +174,7 @@ func getHashColorBullet(v string) string { // printInfo formats and prints info record func printInfo(size int, name, value string) { - name = name + ":" + name += ":" size++ if value == "" { diff --git a/go.mod b/go.mod index 536d3c6..ae9b933 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,6 @@ require ( ) require ( - golang.org/x/image v0.5.0 // indirect + golang.org/x/image v0.14.0 // indirect golang.org/x/sys v0.15.0 // indirect ) diff --git a/go.sum b/go.sum index 61259d6..ec2818a 100644 --- a/go.sum +++ b/go.sum @@ -8,32 +8,9 @@ github.com/essentialkaos/ek/v12 v12.92.0/go.mod h1:9efMqo1S8EtYhmeelOSTmMQDGC2vR github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= -github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= -golang.org/x/image v0.5.0 h1:5JMiNunQeQw++mMOz48/ISeNu3Iweh/JaZU8ZLqHRrI= -golang.org/x/image v0.5.0/go.mod h1:FVC7BI/5Ym8R25iw5OLsgshdUBbT1h5jZTpA+mvAdZ4= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4= +golang.org/x/image v0.14.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE= golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=