Skip to content

Commit

Permalink
Colored text output
Browse files Browse the repository at this point in the history
  • Loading branch information
krystian-panek-vmltech committed Feb 9, 2023
1 parent 8a16f40 commit 18f487f
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 16 deletions.
17 changes: 13 additions & 4 deletions cmd/aem/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"fmt"
"github.com/fatih/color"
"github.com/samber/lo"
"github.com/segmentio/textio"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -160,10 +161,18 @@ func (c *CLI) exit() {
}

func (c *CLI) printCommandResult() {
fmt.Print(fmtx.TblList("command result", [][]any{
changedColored := "false"
if c.outputResponse.Changed {
changedColored = color.YellowString("true")
}
failedColored := "false"
if c.outputResponse.Failed {
failedColored = color.RedString("true")
}
fmt.Print(fmtx.TblList(color.BlueString("command result"), [][]any{
{"message", c.outputResponse.Msg},
{"changed", c.outputResponse.Changed},
{"failed", c.outputResponse.Failed},
{"changed", changedColored},
{"failed", failedColored},
{"elapsed", c.outputResponse.Elapsed},
{"ended", timex.Human(c.outputResponse.Ended)},
}))
Expand Down Expand Up @@ -207,7 +216,7 @@ func (c *CLI) printOutputDataIndented(writer *textio.PrefixWriter, value any) {
return strings.Compare(fmt.Sprintf("%v", keys[k1].Interface()), fmt.Sprintf("%v", keys[k2].Interface())) < 0
})
for _, k := range keys {
_, _ = writer.WriteString(stringsx.HumanCase(fmt.Sprintf("%s", k)) + "\n")
_, _ = writer.WriteString(color.BlueString(stringsx.HumanCase(fmt.Sprintf("%s", k))) + "\n")
mv := rv.MapIndex(k).Interface()
c.printOutputDataIndented(dw, mv)
}
Expand Down
27 changes: 18 additions & 9 deletions cmd/aem/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,15 @@ func (c *CLI) initCmd() *cobra.Command {
return
}
}
scriptFiles, err := os.ReadDir(common.ScriptDir)
scripts, err := c.initFindScriptNames()
if err != nil {
c.Error(fmt.Errorf("cannot list scripts in dir '%s'", common.ScriptDir))
}
var scripts []string
for _, file := range scriptFiles {
if strings.HasSuffix(file.Name(), ".sh") {
scripts = append(scripts, strings.TrimSuffix(file.Name(), ".sh"))
}
c.Error(err)
return
}
c.SetOutput("gettingStarted", fmt.Sprintf(strings.Join([]string{
"The next step is providing AEM files (JAR or SDK ZIP, license, service packs) to directory '" + common.LibDir + "'.",
"Alternatively, instruct the tool where these files are located by adjusting properties: 'dist_file', 'license_file' in configuration file '" + cfg.FileDefault + "'.",
"To avoid problems with IDE performance, make sure to exclude from indexing the directory '" + common.HomeDir + "'.",
"Make sure to exclude the directory '" + common.HomeDir + "'from VCS versioning and IDE indexing.",
"Finally, use control scripts to manage AEM instances:",
"",

Expand All @@ -52,3 +47,17 @@ func (c *CLI) initCmd() *cobra.Command {
}
return cmd
}

func (c *CLI) initFindScriptNames() ([]string, error) {
scriptFiles, err := os.ReadDir(common.ScriptDir)
if err != nil {
return nil, fmt.Errorf("cannot list scripts in dir '%s'", common.ScriptDir)
}
var scripts []string
for _, file := range scriptFiles {
if strings.HasSuffix(file.Name(), ".sh") {
scripts = append(scripts, strings.TrimSuffix(file.Name(), ".sh"))
}
}
return scripts, nil
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ require (
github.com/antchfx/xpath v1.2.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 // indirect
github.com/fatih/color v1.14.1 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/klauspost/compress v1.15.5 // indirect
github.com/klauspost/pgzip v1.2.5 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/nwaples/rardecode v1.1.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+m
github.com/essentialkaos/check v1.2.1 h1:avvyFy/1acUNwfxwuOLsHeCjfXtMygtbu0lVDr3nxFs=
github.com/essentialkaos/go-jar v1.0.6 h1:BAzvYTsXurNd/FcL4nYSH2pWV9DfavVAzlyYqULxcPE=
github.com/essentialkaos/go-jar v1.0.6/go.mod h1:HZPc/zrzjoE2FjCRmIXZj7zQ6RN33/qjtf6ZxaEy7Hw=
github.com/fatih/color v1.14.1 h1:qfhVLaG5s+nCROl1zJsZRxFeYrHLqWroPOQ8BWiNb4w=
github.com/fatih/color v1.14.1/go.mod h1:2oHN61fhTpgcxD3TSWCgKDiH1+x4OiDVVGH8WlgGZGg=
github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI=
github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU=
github.com/go-resty/resty/v2 v2.7.0 h1:me+K9p3uhSmXtrBZ4k9jcEAfJmuC8IivWHwaLZwPrFY=
Expand Down Expand Up @@ -52,6 +54,11 @@ github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mholt/archiver/v3 v3.5.1 h1:rDjOBX9JSF5BvoJGvjqK479aL70qh9DIpZCl+k7Clwo=
Expand Down Expand Up @@ -141,6 +148,7 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
12 changes: 12 additions & 0 deletions pkg/common/filex/filex.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ func ReadString(path string) (string, error) {
return string(bytes), nil
}

func AppendString(path string, text string) error {
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("cannot append to file '%s': %w", path, err)
}
defer f.Close()
if _, err := f.WriteString(text); err != nil {
return fmt.Errorf("cannot append to file '%s': %w", path, err)
}
return nil
}

func AmendString(file string, updater func(string) string) error {
content, err := ReadString(file)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions pkg/common/osx/osx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package osx

import (
"github.com/samber/lo"
"github.com/wttech/aemc/pkg/common/pathx"
"github.com/wttech/aemc/pkg/common/stringsx"
"os"
"runtime"
Expand Down Expand Up @@ -40,3 +41,10 @@ func EnvVarsWithout(names ...string) []string {
}
return result
}

func LineSep() string {
if pathx.Sep() == "\\" {
return "\r\n"
}
return "\n"
}
5 changes: 2 additions & 3 deletions project/common/aemw
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#!/usr/bin/env sh

VERSION=${AEM_CLI_VERSION:-"0.16.5"}
COLORS=${AEM_CLI_COLORS:-"true"}

# Define API
# ==========

# print provisioning step header
step () {
DATE=$(date "+%Y-%m-%d %H:%M:%S")
if [ "$COLORS" = "true" ]; then
CL='\033[0;33m'
if [ -z "$NO_COLOR" ]; then
CL='\033[0;36m'
NC='\033[0m'
echo "${CL}[$DATE]" "$@" "${NC}"
else
Expand Down

0 comments on commit 18f487f

Please sign in to comment.