Skip to content

Commit

Permalink
Merge pull request #87 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 0.7.0
  • Loading branch information
andyone authored Dec 4, 2023
2 parents 70cb7bf + 0db0fe9 commit 00dfacf
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 83 deletions.
29 changes: 18 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,40 @@ concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

env:
SRC_DIR: src/github.com/${{ github.repository }}

jobs:
Go:
name: Go
runs-on: ubuntu-latest

strategy:
matrix:
go: [ '1.18.x', '1.19.x', '1.20.x' ]
go: [ '1.19.x', '1.20.x', '1.21.x' ]

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

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go }}

- name: Checkout
uses: actions/checkout@v3
with:
path: ${{env.SRC_DIR}}

- name: Download dependencies
working-directory: ${{env.SRC_DIR}}
run: make deps

- name: Run test script
working-directory: ${{env.SRC_DIR}}
run: .scripts/script.sh

Typos:
name: Typos
runs-on: ubuntu-latest

needs: Go

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

- name: Check spelling
continue-on-error: true
uses: crate-ci/typos@master
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor
goheft
2 changes: 1 addition & 1 deletion .scripts/script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ main() {

mv goheft goheft-binary

./goheft-binary --raw goheft.go
./goheft-binary goheft.go | cat

if [[ $? -ne 0 ]] ; then
exit 1
Expand Down
2 changes: 2 additions & 0 deletions .typos.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[files]
extend-exclude = ["go.sum"]
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@
<a href="#license"><img src="https://gh.kaos.st/apache2.svg"></a>
</p>

<p align="center"><a href="#screenshots">Screenshots</a> • <a href="#installation">Installation</a> • <a href="#usage">Usage</a> • <a href="#build-status">Build Status</a> • <a href="#contributing">Contributing</a> • <a href="#license">License</a></p>
<p align="center"><a href="#usage-demo">Usage demo</a> • <a href="#installation">Installation</a> • <a href="#usage">Usage</a> • <a href="#build-status">Build Status</a> • <a href="#contributing">Contributing</a> • <a href="#license">License</a></p>

<br/>

`goheft` is simple utility for listing sizes of all used static libraries compiled into golang binary.

### Screenshots
### Usage demo

![Screenshot](https://gh.kaos.st/goheft.png)
[![demo](https://gh.kaos.st/goheft-070.gif)](#usage-demo)

### Installation

#### From source

To build the GoHeft from scratch, make sure you have a working Go 1.18+ workspace ([instructions](https://go.dev/doc/install)), then:
To build the GoHeft from scratch, make sure you have a working Go 1.19+ workspace ([instructions](https://go.dev/doc/install)), then:

```
go install github.com/essentialkaos/goheft@latest
Expand Down Expand Up @@ -72,9 +72,9 @@ Usage: goheft {options} go-file
Options
--external, -e Shadow internal packages
--external, -E Shadow internal packages
--pager, -P Use pager for long output
--min-size, -m size Don't show with size less than defined
--raw, -r Print raw data
--no-color, -nc Disable colors in output
--help, -h Show this help message
--version, -v Show version
Expand All @@ -86,7 +86,6 @@ Examples
goheft application.go -m 750kb
Show size of each used library which greater than 750kb
```

### Build Status
Expand Down
102 changes: 58 additions & 44 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import (
"github.com/essentialkaos/ek/v12/fmtutil"
"github.com/essentialkaos/ek/v12/fsutil"
"github.com/essentialkaos/ek/v12/options"
"github.com/essentialkaos/ek/v12/pager"
"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"
Expand All @@ -35,14 +37,14 @@ import (

const (
APP = "GoHeft"
VER = "0.6.2"
VER = "0.7.0"
DESC = "Utility for listing sizes of used static libraries"
)

const (
OPT_EXTERNAL = "e:external"
OPT_EXTERNAL = "E:external"
OPT_PAGER = "P:pager"
OPT_MIN_SIZE = "m:min-size"
OPT_RAW = "r:raw"
OPT_NO_COLOR = "nc:no-color"
OPT_HELP = "h:help"
OPT_VER = "v:version"
Expand Down Expand Up @@ -77,8 +79,8 @@ func (s LibInfoSlice) Less(i, j int) bool { return s[i].Size < s[j].Size }

var optMap = options.Map{
OPT_EXTERNAL: {Type: options.BOOL},
OPT_PAGER: {Type: options.BOOL},
OPT_MIN_SIZE: {},
OPT_RAW: {Type: options.BOOL},
OPT_NO_COLOR: {Type: options.BOOL},
OPT_HELP: {Type: options.BOOL},
OPT_VER: {Type: options.BOOL},
Expand All @@ -88,13 +90,16 @@ var optMap = options.Map{
OPT_GENERATE_MAN: {Type: options.BOOL},
}

var colorTagApp string
var colorTagVer string
var useRawOutput bool
var isCI bool

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

// Run is main utility function
func Run(gitRev string, gomod []byte) {
runtime.GOMAXPROCS(1)
runtime.GOMAXPROCS(2)

preConfigureUI()

Expand Down Expand Up @@ -129,27 +134,33 @@ func Run(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 fmtc.IsColorsSupported() {
fmtc.DisableColors = false
}

if !fsutil.IsCharacterDevice("/dev/stdout") && os.Getenv("FAKETTY") == "" {
if !tty.IsTTY() {
fmtc.DisableColors = true
useRawOutput = true
}

if os.Getenv("NO_COLOR") != "" {
fmtc.DisableColors = true
}

if os.Getenv("CI") != "" {
isCI = true
}

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

// configureUI configures user interface
Expand All @@ -159,13 +170,13 @@ func configureUI() {
}
}

// process start build
// process processes libs data
func process(file string) {
if !fsutil.IsExist(file) {
printErrorAndExit("Can't build binary - file %s does not exist", file)
}

workDir, err := buildBinary(file)
workDir, err := compileBinary(file)

if err != nil {
os.RemoveAll(workDir)
Expand All @@ -183,6 +194,12 @@ func process(file string) {
return
}

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

printStats(libsInfo)

os.RemoveAll(workDir)
Expand Down Expand Up @@ -262,9 +279,7 @@ func printStats(libs LibInfoSlice) {
minSize = fmtutil.ParseSize(options.GetS(OPT_MIN_SIZE))
}

if !useRawOutput {
fmtc.NewLine()
}
fmtc.If(!useRawOutput).NewLine()

for _, lib := range libs {
if lib.Size < minSize {
Expand All @@ -288,26 +303,24 @@ func printStats(libs LibInfoSlice) {
}

if options.GetB(OPT_EXTERNAL) && !strings.Contains(lib.Package, ".") {
fmtc.Printf(" {s-}%7s %s{!}\n", fmtutil.PrettySize(lib.Size), lib.Package)
fmtc.Printf(" {s-}%8s %s{!}\n", fmtutil.PrettySize(lib.Size), lib.Package)
} else {
fmtc.Printf(" "+colorTag+"%7s{!} %s\n", fmtutil.PrettySize(lib.Size), lib.Package)
fmtc.Printf(" "+colorTag+"%8s{!} %s\n", fmtutil.PrettySize(lib.Size), lib.Package)
}
}

if !useRawOutput && minSize == 0 {
fmtc.Printf(
"\n %7s {*}Total{!} {s-}(packages: %d){!}\n",
"\n %8s {*}Total{!} {s-}(packages: %d){!}\n",
fmtutil.PrettySize(libs.Total()), len(libs),
)
}

if !useRawOutput {
fmtc.NewLine()
}
fmtc.If(!useRawOutput).NewLine()
}

// buildBinary run `go build` command and parse output
func buildBinary(file string) (string, error) {
// compileBinary run `go build` command and parse output
func compileBinary(file string) (string, error) {
var workDir string

cmd := exec.Command("go", "build", "-work", "-a", "-v", file)
Expand All @@ -334,9 +347,7 @@ func buildBinary(file string) (string, error) {

text = normalizePackageName(text)

if !useRawOutput {
fmtc.TPrintf("Building {*}%s{!}…", text)
}
fmtc.If(!useRawOutput && !isCI).TPrintf("Compiling {*}%s{!}…", text)
}
}()

Expand All @@ -346,15 +357,11 @@ func buildBinary(file string) (string, error) {
return "", fmt.Errorf("Can't start build process: %v", err)
}

if !useRawOutput {
fmtc.TPrintf("Processing sources…")
}
fmtc.If(!useRawOutput && !isCI).TPrintf("Processing sources…")

err = cmd.Wait()

if !useRawOutput {
fmtc.TPrintf("")
}
fmtc.If(!useRawOutput && !isCI).TPrintf("")

if err != nil {
return "", fmt.Errorf("Can't start build process: %v", err)
Expand Down Expand Up @@ -382,7 +389,7 @@ func printWarn(f string, a ...interface{}) {
fmtc.Fprintf(os.Stderr, "{y}"+f+"{!}\n", a...)
}

// printErrorAndExit print error mesage and exit with exit code 1
// printErrorAndExit print error message and exit with exit code 1
func printErrorAndExit(f string, a ...interface{}) {
printError(f, a...)
os.Exit(1)
Expand Down Expand Up @@ -435,9 +442,11 @@ func printMan() {
func genUsage() *usage.Info {
info := usage.NewInfo("", "go-file")

info.AppNameColorTag = colorTagApp

info.AddOption(OPT_EXTERNAL, "Shadow internal packages")
info.AddOption(OPT_PAGER, "Use pager for long output")
info.AddOption(OPT_MIN_SIZE, "Don't show with size less than defined", "size")
info.AddOption(OPT_RAW, "Print raw data")
info.AddOption(OPT_NO_COLOR, "Disable colors in output")
info.AddOption(OPT_HELP, "Show this help message")
info.AddOption(OPT_VER, "Show version")
Expand All @@ -451,11 +460,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: 2006,
Owner: "ESSENTIAL KAOS",
App: APP,
Version: VER,
Desc: DESC,
Year: 2006,
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/goheft", update.GitHubChecker},
}
Expand Down
Loading

0 comments on commit 00dfacf

Please sign in to comment.