Skip to content

Commit

Permalink
Merge pull request #12 from sigmonsays/script-options
Browse files Browse the repository at this point in the history
Script options
  • Loading branch information
sigmonsays authored Nov 30, 2022
2 parents 813cf1d + 43dcdeb commit 03d69fe
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ Example
command: |
chmod 0400 ~/.ssh/config
chmod 0400 ~/.ssh/config.d/*
- id: example3
shell: /bin/bash
quiet: true
command: |
set -x
date > /tmp/test.txt

For the above examples, example1 is a 'pre' script which runs before
symlinks and example2 is a 'post' script which runs after symlinks.
Expand Down
21 changes: 17 additions & 4 deletions script.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@ import (
)

var (
DefaultScriptType = "post"
DefaultScriptType = "post"
DefaultScriptShell = "/bin/bash"
)

type Script struct {
Id string
Command string
Disabled bool

// do not print commands stdout and stderr to logs
Quiet bool

// default shell
Shell string

// post or pre; default is 'post'
Type string
}
Expand All @@ -34,6 +41,9 @@ func (me *Script) SetDefaults() {
me.Type = DefaultScriptType
}
me.Type = strings.ToLower(me.Type)
if me.Shell == "" {
me.Shell = DefaultScriptShell
}
}

func (me *Script) Run() (*ScriptResult, error) {
Expand All @@ -43,13 +53,16 @@ func (me *Script) Run() (*ScriptResult, error) {
stdin := bytes.NewBufferString(me.Command)

cmdline := []string{
"/bin/bash",
me.Shell,
"-",
}
c := exec.CommandContext(ctx, cmdline[0], cmdline[1:]...)
c.Stdin = stdin
c.Stdout = os.Stdout
c.Stderr = os.Stdout

if me.Quiet == false {
c.Stdout = os.Stdout
c.Stderr = os.Stdout
}
c.Env = os.Environ()

err := c.Run()
Expand Down

0 comments on commit 03d69fe

Please sign in to comment.