From 3aad554194e6a929968265f581784080add085a2 Mon Sep 17 00:00:00 2001 From: Martin Borho Date: Thu, 1 Jul 2021 23:41:27 +0200 Subject: [PATCH] Add -p flag. (#10) Add -p flag for printing command to stdout before executing. --- .github/workflows/go.yml | 4 ++-- cli.go | 3 +++ help.go | 1 + line.go | 7 ++++++- rem.go | 11 ++++++----- 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 28ae885..8f513fd 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -3,10 +3,10 @@ name: Go on: push: branches: - - "*" + - "**" pull_request: branches: - - "*" + - "**" jobs: diff --git a/cli.go b/cli.go index 8b6b28c..5b881bb 100644 --- a/cli.go +++ b/cli.go @@ -27,6 +27,7 @@ var ( helpFlag *bool addFlag *bool tagFlag *string + printFlag *bool filter *string ) @@ -36,6 +37,7 @@ func init() { helpFlag = flag.Bool("h", false, "show this help") addFlag = flag.Bool("a", false, "add a command") tagFlag = flag.String("t", "", "tag for command") + printFlag = flag.Bool("p", false, "print command before executing") filter = flag.String("f", "", "List commands by regexp filter.") } @@ -49,6 +51,7 @@ func run(remfile string) error { filename: remfile, global: *globalFlag, }, + printBeforeExec: *printFlag, } rem.read() diff --git a/help.go b/help.go index 864edd2..43d9336 100644 --- a/help.go +++ b/help.go @@ -44,6 +44,7 @@ COMMANDS: FLAGS: -g - Use global rem file ~/.rem -t - Tag for command when adding with -a/add. + -p - Print command to stdout before executing index/tag. EXAMPLES: rem add ls -la - Adds "ls -la" to list. diff --git a/line.go b/line.go index e5efd88..ca1e418 100644 --- a/line.go +++ b/line.go @@ -45,7 +45,7 @@ func (l *Line) read(line string) { } } -func (l *Line) execute() error { +func (l *Line) execute(printCmd bool) error { // get the pid of the calling shell pr, err := ps.FindProcess(os.Getppid()) if err != nil { @@ -63,6 +63,11 @@ func (l *Line) execute() error { l.execFlag = "-c" } + // print cmd before executing + if printCmd == true { + fmt.Println(l.cmd) + } + // /bin/bash -c "ls -la" execParts := []string{callerPath, l.execFlag, l.cmd} diff --git a/rem.go b/rem.go index 658c556..131882b 100644 --- a/rem.go +++ b/rem.go @@ -28,9 +28,10 @@ import ( ) type Rem struct { - path string - lines []*Line - hasTags bool + path string + lines []*Line + hasTags bool + printBeforeExec bool File } @@ -56,13 +57,13 @@ func (r *Rem) executeIndex(index int) error { if err != nil { return err } - return line.execute() + return line.execute(r.printBeforeExec) } func (r *Rem) executeTag(tag string) error { for _, line := range r.lines { if line.tag == tag { - return line.execute() + return line.execute(r.printBeforeExec) } } return errors.New("Tag not found.")