Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Jun 23, 2024
1 parent cc4c2a9 commit a0ac7c7
Showing 1 changed file with 8 additions and 19 deletions.
27 changes: 8 additions & 19 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/essentialkaos/ek/v12/strutil"
"github.com/essentialkaos/ek/v12/support"
"github.com/essentialkaos/ek/v12/support/deps"
"github.com/essentialkaos/ek/v12/terminal"
"github.com/essentialkaos/ek/v12/terminal/tty"
"github.com/essentialkaos/ek/v12/usage"
"github.com/essentialkaos/ek/v12/usage/completion/bash"
Expand All @@ -36,7 +37,7 @@ import (
// App info
const (
APP = "MDToc"
VER = "1.2.6"
VER = "1.2.7"
DESC = "Utility for generating table of contents for markdown files"
)

Expand Down Expand Up @@ -91,11 +92,9 @@ func Init(gitRev string, gomod []byte) {

args, errs := options.Parse(optMap)

if len(errs) != 0 {
for _, err := range errs {
printError(err.Error())
}

if !errs.IsEmpty() {
terminal.Error("Options parsing errors:")
terminal.Error(errs.String())
os.Exit(1)
}

Expand Down Expand Up @@ -176,7 +175,7 @@ func process(file string) {
headers := extractHeaders(file)

if len(headers) == 0 {
printWarn("Headers not found in given file")
terminal.Warn("Headers not found in given file")
return
}

Expand Down Expand Up @@ -225,7 +224,7 @@ func printTOC(headers []*Header) {
}

if toc == "" {
printWarn("Suitable headers not found in given file")
terminal.Warn("Suitable headers not found in given file")
return
}

Expand Down Expand Up @@ -373,19 +372,9 @@ func removeBadges(text string) string {
return badgeRegExp.ReplaceAllString(text, "")
}

// printError prints error message to console
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 prints error message and exit with exit code 1
func printErrorAndExit(f string, a ...interface{}) {
printError(f, a...)
terminal.Error(f, a...)
os.Exit(1)
}

Expand Down

0 comments on commit a0ac7c7

Please sign in to comment.