Skip to content

Commit

Permalink
refactored cli, added rule edit commands, fixes
Browse files Browse the repository at this point in the history
fixes #3
  • Loading branch information
tg44 committed Jan 2, 2022
1 parent 9ccc0d6 commit 7a33ca5
Show file tree
Hide file tree
Showing 13 changed files with 551 additions and 253 deletions.
30 changes: 18 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 +22,50 @@ brew install heptapod

### Usage

To print help;
```
heptapod -h
heptapod <action> -h
```
Will print help!

Move (and use) the default ruleset;
```
mkdir -p ~/.heptapod/rules
cp -R $(brew --prefix heptapod)/rules ~/.heptapod
```
This will move the currently added ruleset to `~/.heptapod/rules`

Lists all the rules (you get 4 tables, enabled, disabled, parseable but unrunable and unparsable).
```
heptapod ls -a
heptapod rules ls -a
```

Enable/disable rules (by name), or add/remove ignore folders;
```
heptapod rules disable node bower
heptapod rules enable bower
heptapod rules ignoreAdd ~/.Trash ~/.yarn/cache ~/Library
heptapod rules ignoreRemove ~/.Trash
```
Lists all the rules (you get 4 tables, enabled, disabled, parseable but unrunable and unparsable).

List all the currently excluded TM paths;
```
heptapod lse
heptapod tm ls
```
Could list all the currently excluded TM paths.

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

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.
```
heptapod run
```
Will run the current rules, and add them to the TM exclude list. Also writes exclude logs to `~/.heptapod/logs` (or the given `--logDir dir`) for easier revert.

To revert all the previously added paths from the run-exclude-logs. (`prune -h` could tell you the other useful revert options).
```
heptapod prune -a
```
Will revert all the previously added paths from the run-exclude-logs. (`prune -h` could tell you the other useful revert options).




### Notes for TM migrating to a new machine
When you try to migrate your TM state to a new machine
Expand Down Expand Up @@ -117,6 +122,7 @@ done:
- we should write down what paths excluded by us, and include them back
- brew package
- ghactions
- rule manage commands (list/enable/disable/ignoreAdd/ignoreRemove)

todos:
- handle global deps (m2, ivy, nvm, npm)
Expand Down
244 changes: 10 additions & 234 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,196 +1,33 @@
package main

import (
"fmt"
"github.com/olekukonko/tablewriter"
"github.com/tg44/heptapod/pkg"
"github.com/tg44/heptapod/pkg/parser"
"github.com/tg44/heptapod/pkg/tmutil"
cli_utils "github.com/tg44/heptapod/pkg/cli-utils"
"github.com/urfave/cli/v2"
"log"
"os"
"sort"
"strings"
)

var version string = "local-build"
var commit string = ""
var date string = ""

func main() {
var rulePath string
var logDir string
var file string
var all bool
var dry bool
var current bool
var verbose bool
var par int
var buffer int

app := &cli.App{
Name: "heptapod",
Usage: "Fine-tune your TimeMachine excludes!",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "rules",
Aliases: []string{"r"},
Value: "~/.heptapod/rules",
Usage: "the directory containing rule yamls",
Destination: &rulePath,
},
&cli.StringFlag{
Name: "logDir",
Aliases: []string{"ld"},
Value: "~/.heptapod/logs",
Usage: "the directory where excluded dirs logged for reliable prune/uninstall",
Destination: &logDir,
},
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
Value: false,
Usage: "prints out performance logs (and other logs in general)",
Destination: &verbose,
},
&cli.IntFlag{
Name: "parallelism",
Aliases: []string{"p", "par"},
Value: 4,
Usage: "number of workers where the code is multithreaded",
Destination: &par,
},
&cli.IntFlag{
Name: "bufferSize",
Aliases: []string{"b", "buffer"},
Value: 2048,
Usage: "number of elements buffered, can cause deadlocks",
Destination: &buffer,
},
cli_utils.RulesPathFlag,
cli_utils.LogDirFlag,
cli_utils.VerboseFlag,
cli_utils.ParallelismFlag,
cli_utils.BufferSizeFlag,
},
Commands: []*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 {
fmt.Println("version: ", version)
fmt.Println("commit: ", commit)
fmt.Println("date: ", date)
} else {
fmt.Println(version)
}
return nil
},
},
{
Name: "list",
Aliases: []string{"ls"},
Usage: "list the rules",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "all",
Aliases: []string{"a"},
Value: false,
Usage: "show all tables",
Destination: &all,
},
},
Action: func(c *cli.Context) error {
return ruleTable(rulePath, all)
},
},
{
Name: "run",
Aliases: []string{},
Usage: "run the exclude detection, and also exclude the dirs",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "dry",
Aliases: []string{"d"},
Value: false,
Usage: "only prints the paths we should exclude (if exists)",
Destination: &dry,
},
},
Action: func(c *cli.Context) error {
if dry {
res := pkg.GetExcludedPaths(rulePath, par, buffer, verbose)
fmt.Println("-----")
fmt.Print(strings.Join(res,"\r\n"))
fmt.Print("\n")
} else {
res := pkg.GetExcludedPaths(rulePath, par, buffer, verbose)
tmutil.AddPathsToTM(res, logDir, buffer, verbose)
if verbose {
log.Printf("total %d paths found\n", len(res))
}
}
return nil
},
},
{
Name: "excluded",
Aliases: []string{"lse"},
Usage: "lists all the excluded dirs from tmutil",
Flags: []cli.Flag{},
Action: func(c *cli.Context) error {
res := tmutil.GetExcludeList()
fmt.Println(res)
return nil
},
},
{
Name: "prune",
Aliases: []string{},
Usage: "removes excludes from TM",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "all",
Aliases: []string{"a"},
Value: false,
Usage: "remove all previously added exclude paths from the log dir",
Destination: &all,
},
&cli.StringFlag{
Name: "file",
Aliases: []string{"f"},
Value: "",
Usage: "remove previously added exclude paths from the file in the log dir",
Destination: &file,
},
&cli.BoolFlag{
Name: "run",
Aliases: []string{"r"},
Value: false,
Usage: "remove previously added exclude paths based on the current rules",
Destination: &current,
},
},
Action: func(c *cli.Context) error {
if current {
res := pkg.GetExcludedPaths(rulePath, par, buffer, verbose)
tmutil.RemovePathsFromTM(res, buffer, verbose)
} else if all {
tmutil.RemoveAllFromLogs(logDir, buffer, verbose)
} else if file != "" {
tmutil.RemoveFileFromLogs(logDir, file, buffer, verbose)
} else {
log.Fatal("one of the options is mandatory, please add current, all, or a file")
}
return nil
},
},
cli_utils.VersionCommand(version, commit, date),
cli_utils.RuleCommands,
cli_utils.RunCommand,
cli_utils.TmCommands,
},
}

Expand All @@ -202,64 +39,3 @@ func main() {
log.Fatal(err)
}
}

func ruleTable(path string, all bool) error {
list, err := parser.ParseRulesFromDir(path)
if err != nil {
return err
}
fmt.Println("Enabled rules:")
writeRules(list.Enabled)
if all {
fmt.Println("Disabled rules:")
writeRules(list.Disabled)
}
if all {
fmt.Println("Errors:")
writeErrorRules(list.FileErrors)
}
if all {
fmt.Println("Type parse errors:")
writeTypeErrorRules(list.TypeErrors)
}
return nil
}

func writeErrorRules(paths []string) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"nonparseable files"})
table.SetBorder(false)

for _, row := range paths {
table.Append([]string{row})
}

table.SetAutoMergeCells(false)
table.Render()
}

func writeTypeErrorRules(tes map[string]parser.Rule) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"path", "name", "type"})
table.SetBorder(false)

for k, v := range tes {
table.Append([]string{k, v.Name, v.RuleType})
}

table.SetAutoMergeCells(false)
table.Render()
}

func writeRules(tes map[string]parser.Rule) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"path", "name", "type", "search", "ignore"})
table.SetBorder(false)

for k, v := range tes {
table.Append([]string{k, v.Name, v.RuleType, strings.Join(v.SearchPaths, ", "), strings.Join(v.IgnorePaths, ", ")})
}

table.SetAutoMergeCells(false)
table.Render()
}
51 changes: 51 additions & 0 deletions pkg/cli-utils/global-flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cli_utils

import (
"github.com/urfave/cli/v2"
)

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

var RulesPathFlag = &cli.StringFlag{
Name: "rules",
Aliases: []string{"r"},
Value: "~/.heptapod/rules",
Usage: "the directory containing rule yamls",
Destination: &rulePath,
}

var LogDirFlag = &cli.StringFlag{
Name: "logDir",
Aliases: []string{"ld"},
Value: "~/.heptapod/logs",
Usage: "the directory where excluded dirs logged for reliable prune/uninstall",
Destination: &logDir,
}

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

var ParallelismFlag = &cli.IntFlag{
Name: "parallelism",
Aliases: []string{"p", "par"},
Value: 4,
Usage: "number of workers where the code is multithreaded",
Destination: &par,
}

var BufferSizeFlag = &cli.IntFlag{
Name: "bufferSize",
Aliases: []string{"b", "buffer"},
Value: 2048,
Usage: "number of elements buffered, can cause deadlocks",
Destination: &buffer,
}
Loading

0 comments on commit 7a33ca5

Please sign in to comment.