Skip to content

Commit

Permalink
Remove useless log
Browse files Browse the repository at this point in the history
Add color wrapper for element in logs
  • Loading branch information
efortin committed Jan 22, 2021
1 parent 7430462 commit 7fdd89b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
7 changes: 3 additions & 4 deletions internal/ansible/playbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,17 @@ func (p *playbooks) LoadFromPath(rootPath string) (result []Playbook, err error)
for _, role := range play.Roles {
err := role.LoadFromPath(rootPath)
if err != nil {
log.Debug(err)
log.Warn("Cannot load role: ", utils.WrapRed(err.Error()))
} else {
log.Debug(" Role info", role.AllTags())
allTags.Concat(role.AllTags().List())
}
allTags.Concat(role.AllTags().List())
}
}
playbook.absolutePath = osPathname
playbook.rootPath = &rootPath

result = append(result, *playbook)
log.Debug("Available tags are :", playbook.AllTags())
log.Debug("Available tags for playbook", utils.WrapGrey(osPathname), " are: ", playbook.AllTags().List())
return nil
},
ErrorCallback: func(osPathname string, err error) godirwalk.ErrorAction {
Expand Down
8 changes: 3 additions & 5 deletions internal/ansible/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func (role *Role) AllTags() (tags *utils.Set) {
// All sub parent directory added like label in the inventory
func (role *Role) LoadFromPath(rootPath string) (err error) {
absRoot, err := filepath.Abs(rootPath + "/roles/" + role.Name)
log.Debug("reading ", role.Name, "at: ", absRoot)

if err != nil {
log.Debug("The role ", role.Name, "can't be read. Error:", err.Error())
Expand All @@ -55,15 +54,14 @@ func (role *Role) LoadFromPath(rootPath string) (err error) {
err = yaml.Unmarshal(binData, &tasks)

if err != nil {
log.Debug("Error reading role", osPathname, "err:", err.Error())
log.Warn("Error during role parsing: ", utils.WrapRed(osPathname), ". More info in trace level.")
log.Trace("Err:", err.Error())
}

for _, task := range tasks {
role.Tasks = append(role.Tasks, task)
}

log.Debug(osPathname, "tags in role tags:", role.AllTags())

log.Debug("Available tags for role ", utils.WrapGrey(osPathname), " are: ", role.AllTags().List())
return nil
},
ErrorCallback: func(osPathname string, err error) godirwalk.ErrorAction {
Expand Down
32 changes: 32 additions & 0 deletions internal/utils/logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package utils

import (
"fmt"
"os"
"strings"
)

func isTerminal() bool {
fileInfo, _ := os.Stdout.Stat()
return (fileInfo.Mode() & os.ModeCharDevice) != 0
}

func WrapYellow(msg ...string) string {
return wrapLog("33", msg...)
}

func WrapGrey(msg ...string) string {
return wrapLog("37", msg...)
}

func WrapRed(msg ...string) string {
return wrapLog("91", msg...)
}

func wrapLog(color string, msg ...string) string {
if isTerminal() {
return fmt.Sprintf("\033[1;"+color+"m%v\033[0m", strings.Join(msg, ""))
} else {
return fmt.Sprintf(strings.Join(msg, ""))
}
}

0 comments on commit 7fdd89b

Please sign in to comment.