Skip to content

Commit

Permalink
Merge pull request #116 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 2.1.0
  • Loading branch information
andyone authored Dec 5, 2023
2 parents 48bdc23 + 166b1ba commit 612a4ce
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 68 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
Expand All @@ -56,7 +56,7 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
Expand All @@ -79,7 +79,7 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Check spelling
continue-on-error: true
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 2

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ Options
--arch, -a name Architecture name
--struct, -s name Print info only about struct with given name
--tags, -t tag… Build tags (mergeble)
--pager, -P Use pager for long output
--no-color, -nc Disable colors in output
--help, -h Show this help message
--version, -v Show version
Expand Down
99 changes: 68 additions & 31 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/essentialkaos/ek/v12/fmtc"
"github.com/essentialkaos/ek/v12/fmtutil"
"github.com/essentialkaos/ek/v12/options"
"github.com/essentialkaos/ek/v12/pager"
"github.com/essentialkaos/ek/v12/strutil"
"github.com/essentialkaos/ek/v12/usage"
"github.com/essentialkaos/ek/v12/usage/completion/bash"
Expand All @@ -35,15 +36,16 @@ import (
// App info
const (
APP = "aligo"
VER = "2.0.2"
DESC = "Utility for viewing and checking Golang struct alignment"
VER = "2.1.0"
DESC = "Utility for viewing and checking Go struct alignment"
)

// Constants with options names
const (
OPT_ARCH = "a:arch"
OPT_STRUCT = "s:struct"
OPT_TAGS = "t:tags"
OPT_PAGER = "P:pager"
OPT_NO_COLOR = "nc:no-color"
OPT_HELP = "h:help"
OPT_VER = "v:version"
Expand All @@ -60,6 +62,7 @@ var optMap = options.Map{
OPT_ARCH: {},
OPT_STRUCT: {},
OPT_TAGS: {Mergeble: true},
OPT_PAGER: {Type: options.BOOL},
OPT_NO_COLOR: {Type: options.BOOL},
OPT_HELP: {Type: options.BOOL},
OPT_VER: {Type: options.MIXED},
Expand All @@ -69,6 +72,8 @@ var optMap = options.Map{
OPT_GENERATE_MAN: {Type: options.BOOL},
}

var colorTagApp, colorTagVer string

// ////////////////////////////////////////////////////////////////////////////////// //

// Run is main CLI function
Expand Down Expand Up @@ -103,8 +108,15 @@ func Run(gitRev string, gomod []byte) {
os.Exit(0)
}

prepare()
process(args)
err, ok := process(args)

if err != nil {
printError(err.Error())
}

if !ok {
os.Exit(1)
}
}

// preConfigureUI preconfigures UI based on information about user terminal
Expand All @@ -121,11 +133,20 @@ func configureUI() {
}

strutil.EllipsisSuffix = "…"
fmtutil.SeparatorTitleColorTag = "{*}"
fmtutil.SeparatorSymbol = "–"

switch {
case fmtc.IsTrueColorSupported():
colorTagApp, colorTagVer = "{*}{&}{#00ADD8}", "{#5DC9E2}"
case fmtc.Is256ColorsSupported():
colorTagApp, colorTagVer = "{*}{&}{#38}", "{#74}"
default:
colorTagApp, colorTagVer = "{*}{&}{c}", "{c}"
}
}

// prepare configures inspector
func prepare() {
func prepare() error {
arch := build.Default.GOARCH

if options.Has(OPT_ARCH) {
Expand All @@ -135,24 +156,38 @@ func prepare() {
inspect.Sizes = types.SizesFor("gc", arch)

if inspect.Sizes == nil {
printErrorAndExit("Unknown arch %s", arch)
return fmt.Errorf("Unknown arch %s", arch)
}

return nil
}

// process starts processing
func process(args options.Arguments) {
// process starts source code processing
func process(args options.Arguments) (error, bool) {
err := prepare()

if err != nil {
return err, false
}

cmd := args.Get(0).ToLower().String()
dirs := args.Strings()[1:]
tags := strings.Split(options.GetS(OPT_TAGS), ",")

report, err := inspect.ProcessSources(dirs, tags)

if err != nil {
printErrorAndExit(err.Error())
return err, false
}

if report == nil && err == nil {
os.Exit(1)
if report == nil {
return nil, true
}

if options.GetB(OPT_PAGER) {
if pager.Setup() == nil {
defer pager.Complete()
}
}

switch cmd {
Expand All @@ -166,15 +201,15 @@ func process(args options.Arguments) {
case "check", "c":
if options.Has(OPT_STRUCT) {
PrintStruct(report, options.GetS(OPT_STRUCT), true)
} else {
if Check(report) {
os.Exit(1)
}
} else if !Check(report) {
return nil, false
}

default:
printErrorAndExit("Command %s is unsupported", cmd)
return fmt.Errorf("Command %s is unsupported", cmd), false
}

return nil, true
}

// printError prints error message to console
Expand All @@ -187,12 +222,6 @@ 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...)
os.Exit(1)
}

// ////////////////////////////////////////////////////////////////////////////////// //

// printCompletion prints completion for given shell
Expand All @@ -201,11 +230,11 @@ func printCompletion() int {

switch options.GetS(OPT_COMPLETION) {
case "bash":
fmt.Printf(bash.Generate(info, "aligo"))
fmt.Print(bash.Generate(info, "aligo"))
case "fish":
fmt.Printf(fish.Generate(info, "aligo"))
fmt.Print(fish.Generate(info, "aligo"))
case "zsh":
fmt.Printf(zsh.Generate(info, optMap, "aligo"))
fmt.Print(zsh.Generate(info, optMap, "aligo"))
default:
return 1
}
Expand All @@ -227,12 +256,15 @@ func printMan() {
func genUsage() *usage.Info {
info := usage.NewInfo("", "package…")

info.AppNameColorTag = colorTagApp

info.AddCommand("check", "Check package for alignment problems")
info.AddCommand("view", "Print alignment info for all structs")

info.AddOption(OPT_ARCH, "Architecture name", "name")
info.AddOption(OPT_STRUCT, "Print info only about struct with given name", "name")
info.AddOption(OPT_TAGS, "Build tags {s-}(mergeble){!}", "tag…")
info.AddOption(OPT_PAGER, "Use pager for long output")
info.AddOption(OPT_NO_COLOR, "Disable colors in output")
info.AddOption(OPT_HELP, "Show this help message")
info.AddOption(OPT_VER, "Show version")
Expand Down Expand Up @@ -264,11 +296,16 @@ func genUsage() *usage.Info {
// genAbout generates info about version
func genAbout(gitRev string) *usage.About {
about := &usage.About{
App: APP,
Version: VER,
Desc: DESC,
Year: 2009,
Owner: "ESSENTIAL KAOS",
App: APP,
Version: VER,
Desc: DESC,
Year: 2009,
Owner: "ESSENTIAL KAOS",

AppNameColorTag: colorTagApp,
VersionColorTag: colorTagVer,
DescSeparator: "—",

License: "Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>",
UpdateChecker: usage.UpdateChecker{"essentialkaos/aligo", update.GitHubChecker},
}
Expand Down
8 changes: 6 additions & 2 deletions cli/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func PrintStruct(r *report.Report, strName string, optimal bool) {

if pkg == nil && str == nil {
printWarn("Can't find struct with name \"%s\"", strName)
return
}

printPackageSeparator(pkg.Path)
Expand Down Expand Up @@ -196,7 +197,10 @@ func printPackageSeparator(path string) {
path = "{GOPATH}" + path[1:]
}

fmtc.Printf("{s}{@*} ••• %s{!}\n\n", fmtutil.Align(path, fmtutil.LEFT, 86))
fmtutil.Separator(true)
fmtc.Printf(" ▾ {s}%s{!}\n", path)
fmtutil.Separator(true)
fmtc.NewLine()
}

// printPackageInfo prints package info
Expand Down Expand Up @@ -274,7 +278,7 @@ func printCurrentFieldsInfo(fields []*report.Field) {
for index, field := range fields {
r.PrintField(field)

fmt.Printf(strings.Repeat(" ", int(counter+1)))
fmt.Print(strings.Repeat(" ", int(counter+1)))

for i := int64(0); i < field.Size; i++ {
fmtc.Printf("{g}■ {!}")
Expand Down
2 changes: 1 addition & 1 deletion cli/support/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,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 == "" {
Expand Down
14 changes: 6 additions & 8 deletions cli/support/support_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ 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)
Expand All @@ -38,11 +38,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)
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ go 1.18

require (
github.com/essentialkaos/depsy v1.1.0
github.com/essentialkaos/ek/v12 v12.75.1
github.com/essentialkaos/ek/v12 v12.90.1
github.com/kisielk/gotool v1.0.0
golang.org/x/tools v0.12.0
golang.org/x/tools v0.16.0
)

require (
golang.org/x/mod v0.12.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/sys v0.15.0 // indirect
)
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
github.com/essentialkaos/check v1.4.0 h1:kWdFxu9odCxUqo1NNFNJmguGrDHgwi3A8daXX1nkuKk=
github.com/essentialkaos/depsy v1.1.0 h1:U6dp687UkQwXlZU17Hg2KMxbp3nfZAoZ8duaeUFYvJI=
github.com/essentialkaos/depsy v1.1.0/go.mod h1:kpiTAV17dyByVnrbNaMcZt2jRwvuXClUYOzpyJQwtG8=
github.com/essentialkaos/ek/v12 v12.75.1 h1:HE8/uWED+QgyT6HIcRULqjMYWKhqQ1rkfa/ozcva1eQ=
github.com/essentialkaos/ek/v12 v12.75.1/go.mod h1:juDcZWOWaj1QmYShZkT9RzdqJ3n0tmeP/iq4sw5fQF0=
github.com/essentialkaos/ek/v12 v12.90.1 h1:ID950cnz4xgpqqFzhleP5xaVoLnPwuiykdH3FrogD/E=
github.com/essentialkaos/ek/v12 v12.90.1/go.mod h1:9efMqo1S8EtYhmeelOSTmMQDGC2vRgPkjkKKfvUD2eU=
github.com/kisielk/gotool v1.0.0 h1:AV2c/EiW3KqPNT9ZKl07ehoAGi4C5/01Cfbblndcapg=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
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.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
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/tools v0.16.0 h1:GO788SKMRunPIBCXiQyo2AaexLstOrVhuAL5YwsckQM=
golang.org/x/tools v0.16.0/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
10 changes: 2 additions & 8 deletions inspect/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ func formatValueType(typ string, mappings map[string]string) string {
// formatPackageName formats package name
func formatPackageName(p string) string {
p = path.Base(p)
p = strings.Replace(p, "go-", "", -1)
p = strings.Replace(p, "go.", "", -1)
p = strings.ReplaceAll(p, "go-", "")
p = strings.ReplaceAll(p, "go.", "")

if strings.Contains(p, ".") {
p = p[:strings.Index(p, ".")]
Expand Down Expand Up @@ -318,10 +318,4 @@ func (s *optimalSorter) Less(i, j int) bool {
}
}

type pkgSlice []*report.Package

func (s pkgSlice) Len() int { return len(s) }
func (s pkgSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s pkgSlice) Less(i, j int) bool { return s[i].Path < s[j].Path }

// ////////////////////////////////////////////////////////////////////////////////// //

0 comments on commit 612a4ce

Please sign in to comment.