Skip to content

Commit

Permalink
verbose level 0-4 introduced
Browse files Browse the repository at this point in the history
  • Loading branch information
tg44 committed Jan 12, 2022
1 parent 7a33ca5 commit f81a58f
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ heptapod tm ls

Dryrun the current rules, in verbose mode, also log speed and debug informations. (Potentially list nonexistent directories and files!)
```
heptapod -v run -d
heptapod -v 1 run -d
```

To run the current rules, and add the dirs to the TM exclude list. Also writes exclude logs to `~/.heptapod/logs` (or the given `--logDir dir`) for easier revert.
Expand Down
8 changes: 4 additions & 4 deletions pkg/cli-utils/global-flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

var rulePath string
var logDir string
var verbose bool
var verbose int
var par int
var buffer int

Expand All @@ -26,11 +26,11 @@ var LogDirFlag = &cli.StringFlag{
Destination: &logDir,
}

var VerboseFlag = &cli.BoolFlag{
var VerboseFlag = &cli.IntFlag{
Name: "verbose",
Aliases: []string{"v"},
Value: false,
Usage: "prints out performance logs (and other logs in general)",
Value: 0,
Usage: "prints out performance logs (and other logs in general) (0-4)",
Destination: &verbose,
}

Expand Down
9 changes: 5 additions & 4 deletions pkg/cli-utils/run-command.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ var RunCommand = &cli.Command{
fmt.Print(strings.Join(res, "\r\n"))
fmt.Print("\n")
} else {
log.Printf("path detection started")
res := pkg.GetExcludedPaths(rulePath, par, buffer, verbose)
tmutil.AddPathsToTM(res, logDir, buffer, verbose)
if verbose {
log.Printf("total %d paths found\n", len(res))
}
log.Printf("total %d paths found\n", len(res))
log.Printf("tm excludes started")
added := tmutil.AddPathsToTM(res, logDir, buffer, verbose)
log.Printf("added %d paths to exclude", added)
}
return nil
},
Expand Down
11 changes: 1 addition & 10 deletions pkg/cli-utils/version-command.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,8 @@ func VersionCommand(version string, commit string, date string) *cli.Command {
Name: "version",
Aliases: []string{"v"},
Usage: "version info",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
Value: false,
Usage: "more detaild version info",
Destination: &verbose,
},
},
Action: func(c *cli.Context) error {
if verbose {
if verbose > 0 {
fmt.Println("version: ", version)
fmt.Println("commit: ", commit)
fmt.Println("date: ", date)
Expand Down
2 changes: 1 addition & 1 deletion pkg/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"log"
)

func GetExcludedPaths(ruleDir string, par int, bufferSize int, verbose bool) []string {
func GetExcludedPaths(ruleDir string, par int, bufferSize int, verbose int) []string {
path, err := utils.FixupPathsToHandleHome(ruleDir)
if err != nil {
log.Fatal(err)
Expand Down
17 changes: 9 additions & 8 deletions pkg/tmutil/tmutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"time"
)

func AddPathsToTM(paths []string, logDir string, bufferSize int, verbose bool) {
func AddPathsToTM(paths []string, logDir string, bufferSize int, verbose int) int {
logPath, err := utils.FixupPathsToHandleHome(logDir)
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -65,12 +65,13 @@ func AddPathsToTM(paths []string, logDir string, bufferSize int, verbose bool) {
}
close(check)
<-finished
if verbose {
if verbose > 0 {
log.Printf("added %d", added)
}
return added
}

func checkPath(path string, verbose bool) (bool, error) {
func checkPath(path string, verbose int) (bool, error) {
if _, err := os.Stat(path); os.IsNotExist(err) {
return false, err
}
Expand All @@ -82,7 +83,7 @@ func checkPath(path string, verbose bool) (bool, error) {
cmd.Stderr = &outErr
err := cmd.Run()
if err != nil {
if verbose {
if verbose > 1 {
log.Println(out.String())
log.Println(outErr.String())
}
Expand Down Expand Up @@ -123,7 +124,7 @@ func removePath(path string) error {
return err
}

func RemoveAllFromLogs(logPath string, bufferSize int, verbose bool) {
func RemoveAllFromLogs(logPath string, bufferSize int, verbose int) {
files, err := ioutil.ReadDir(logPath)
if err != nil {
log.Fatal(err)
Expand All @@ -132,7 +133,7 @@ func RemoveAllFromLogs(logPath string, bufferSize int, verbose bool) {
RemoveFileFromLogs(logPath, file.Name(), bufferSize, verbose)
}
}
func RemoveFileFromLogs(logPath string, fileName string, bufferSize int, verbose bool) {
func RemoveFileFromLogs(logPath string, fileName string, bufferSize int, verbose int) {
file, err := os.Open(filepath.Join(logPath, fileName))
if err != nil {
log.Println(err)
Expand All @@ -149,7 +150,7 @@ func RemoveFileFromLogs(logPath string, fileName string, bufferSize int, verbose
}
RemovePathsFromTM(lines, bufferSize, verbose)
}
func RemovePathsFromTM(paths []string, bufferSize int, verbose bool) {
func RemovePathsFromTM(paths []string, bufferSize int, verbose int) {
defer utils.TimeTrack(time.Now(), "tmutil run", verbose)

check := make(chan string, bufferSize)
Expand Down Expand Up @@ -184,7 +185,7 @@ func RemovePathsFromTM(paths []string, bufferSize int, verbose bool) {
}
close(check)
<-finished
if verbose {
if verbose > 0 {
log.Printf("removed %d", removed)
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/time-utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"time"
)

func TimeTrack(start time.Time, name string, verbose bool) {
func TimeTrack(start time.Time, name string, verbose int) {
elapsed := time.Since(start)
if verbose {
if verbose > 0 {
log.Printf("%s took %s", name, elapsed)
}
}
28 changes: 16 additions & 12 deletions pkg/walker/walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type WalkJob struct {
AlreadyFiltered []string
}

func Run(jobs []WalkJob, par int, bufferSize int, verbose bool) []string {
func Run(jobs []WalkJob, par int, bufferSize int, verbose int) []string {
defer utils.TimeTrack(time.Now(), "walker run", verbose)
if len(jobs) == 0 {
return []string{}
Expand Down Expand Up @@ -64,13 +64,13 @@ func Run(jobs []WalkJob, par int, bufferSize int, verbose bool) []string {
return res
}

func worker(id int, spawn chan WalkJob, start chan bool, end chan []string, verbose bool) {
func worker(id int, spawn chan WalkJob, start chan bool, end chan []string, verbose int) {
for j := range spawn {
walk(id, j.Rootpath, j.Walkers, j.AlreadyFiltered, spawn, start, end, verbose)
}
}

func walk(runnerId int, rootpath string, walkers []Walker, alreadyFiltered []string, spawn chan WalkJob, start chan bool, end chan []string, verbose bool) {
func walk(runnerId int, rootpath string, walkers []Walker, alreadyFiltered []string, spawn chan WalkJob, start chan bool, end chan []string, verbose int) {
defer utils.TimeTrack(time.Now(), fmt.Sprintf("(runner-%d) walk on %s", runnerId, rootpath), verbose)
start <- true
hasNext := true
Expand All @@ -89,7 +89,7 @@ func walk(runnerId int, rootpath string, walkers []Walker, alreadyFiltered []str
next := l.Next
files, err := ioutil.ReadDir(l.Data)
if err != nil {
if (verbose) {
if verbose > 0 {
fmt.Println("!!! There was an error: ", err.Error())
}
if next == nil {
Expand All @@ -104,20 +104,24 @@ func walk(runnerId int, rootpath string, walkers []Walker, alreadyFiltered []str
globalIgnores := []string{}
for _, walker := range walkers {
r, i1, i2, name := walker(l.Data, files)
if(verbose && len(r) > 0) {
if verbose > 1 && len(r) > 0 {
fmt.Println("-----")
fmt.Println(name)
fmt.Println(l.Data)
for _, v := range r {
fmt.Println("\t", v)
}
fmt.Println("-")
for _, v := range i1 {
fmt.Println("\t", v)
}
fmt.Println("-")
for _, v := range i2 {
fmt.Println("\t", v)
if verbose > 2 {
fmt.Println("-")
for _, v := range i1 {
fmt.Println("\t", v)
}
if verbose > 3 {
fmt.Println("-")
for _, v := range i2 {
fmt.Println("\t", v)
}
}
}
}
res = res.Prepend(r)
Expand Down

0 comments on commit f81a58f

Please sign in to comment.