Skip to content

Commit

Permalink
Merge pull request #12 from sbssk8/feature/allow-setting-working-dire…
Browse files Browse the repository at this point in the history
…ctory

Allow setting working directory
  • Loading branch information
gabrielperezs authored Jan 24, 2022
2 parents c4e6cc4 + a04c9cd commit b6cbf22
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ Substitutions in args entry in the config file
- `CreationTimestampSeconds` is the message creation time in seconds _See [examples/ARRAY.md](examples/ARRAY.md) for an example_


Set working directory
---------------------

The working directory of the process can be set with `workingDirectory`.

Execute as a specific user
--------------------------

Expand Down Expand Up @@ -121,6 +126,7 @@ cond = [
cmd = "/usr/local/bin/do-something-with-the-instance"
args = ["asg=$.AutoScalingGroupName", "instance_id=$.EC2InstanceId"]
argsjson = true
workingDirectory = "/path/to/process/wd/"
```


Expand Down
24 changes: 13 additions & 11 deletions outputs/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ const (
// Cmd is the command struct that will be executed after recive the order
// from the input plugins
type Cmd struct {
r *lib.Reactor
cmd string
user string
environment []string
args []string
cond map[string]*regexp.Regexp
maximumCmdTimeLive time.Duration
r *lib.Reactor
cmd string
user string
workingDirectory string
environment []string
args []string
cond map[string]*regexp.Regexp
maximumCmdTimeLive time.Duration
}

// NewOrGet create the command struct and fill the parameters needed from the
Expand All @@ -50,6 +51,8 @@ func NewOrGet(r *lib.Reactor, c map[string]interface{}) (*Cmd, error) {
}
case "user":
o.user = v.(string)
case "workingdirectory":
o.workingDirectory = v.(string)
case "env":
for _, n := range v.([]interface{}) {
o.environment = append(o.environment, n.(string))
Expand Down Expand Up @@ -182,19 +185,18 @@ func (o *Cmd) Run(rl *lib.ReactorLog, msg lib.Msg) error {
return err
}
}
c.Dir = o.workingDirectory

c.Stdout = rl
c.Stderr = rl



if err := c.Start(); err != nil {
rl.Write([]byte("error starting process " + o.cmd + " " + strings.Join(args, " ") +": " + err.Error()))
rl.Write([]byte("error starting process " + o.cmd + " " + strings.Join(args, " ") + ": " + err.Error()))
return err
}

pid := c.Process.Pid // Since Start returned correctly, c.Process is not null.
rl.Start(pid, o.cmd + " " + strings.Join(args, " "))
rl.Start(pid, o.cmd+" "+strings.Join(args, " "))

if err := c.Wait(); err != nil {
rl.Write([]byte("error running process: " + err.Error()))
Expand Down
3 changes: 2 additions & 1 deletion outputs/cmd/user_unix.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris

package cmd
Expand All @@ -15,7 +16,7 @@ func setUserToCmd(u string, finalEnv []string, c *exec.Cmd) error {
if err != nil {
return err
}
c.Env = append(finalEnv)
c.Env = append([]string(nil), finalEnv...)
c.SysProcAttr = &syscall.SysProcAttr{}
c.SysProcAttr.Credential = userCredential
setEnvirons(u, finalEnv, c)
Expand Down

0 comments on commit b6cbf22

Please sign in to comment.