initial commit
This commit is contained in:
34
tplCmd/funcs_cmd.go
Normal file
34
tplCmd/funcs_cmd.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package tplCmd
|
||||
|
||||
import (
|
||||
`os`
|
||||
`os/exec`
|
||||
)
|
||||
|
||||
/*
|
||||
ToCmd returns an (os/)exec.Cmd from a Cmd.
|
||||
|
||||
err will always be nil for now, but is still returned and should be handled
|
||||
for future-proofing.
|
||||
*/
|
||||
func (c *Cmd) ToCmd() (cmd *exec.Cmd, err error) {
|
||||
|
||||
var envs []string
|
||||
|
||||
if c.Args != nil && len(c.Args) > 0 {
|
||||
cmd = exec.Command(c.Program, c.Args...)
|
||||
} else {
|
||||
cmd = exec.Command(c.Program)
|
||||
}
|
||||
|
||||
if !c.IsolateEnv {
|
||||
envs = os.Environ()
|
||||
}
|
||||
if c.EnvVars != nil && len(c.EnvVars) > 0 {
|
||||
envs = append(envs, c.EnvVars...)
|
||||
}
|
||||
|
||||
cmd.Env = envs
|
||||
|
||||
return
|
||||
}
|
||||
73
tplCmd/funcs_templatecmd.go
Normal file
73
tplCmd/funcs_templatecmd.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package tplCmd
|
||||
|
||||
import (
|
||||
`bytes`
|
||||
`os`
|
||||
`os/exec`
|
||||
`text/template`
|
||||
)
|
||||
|
||||
// ToCmd returns an (os/)exec.Cmd from a TemplateCmd. t should be a tunnelbroker.FetchedTunnel, generally.
|
||||
func (c *TemplateCmd) ToCmd(t any) (cmd *exec.Cmd, err error) {
|
||||
|
||||
var progName string
|
||||
var envs []string
|
||||
var tpl *template.Template
|
||||
var args []string
|
||||
var buf *bytes.Buffer = new(bytes.Buffer)
|
||||
|
||||
buf.Reset()
|
||||
if tpl, err = template.New("").Parse(c.Program); err != nil {
|
||||
return
|
||||
}
|
||||
if err = tpl.Execute(buf, t); err != nil {
|
||||
return
|
||||
}
|
||||
progName = buf.String()
|
||||
|
||||
if c.Args != nil && len(c.Args) > 0 {
|
||||
args = make([]string, len(c.Args))
|
||||
for idx, arg := range c.Args {
|
||||
buf.Reset()
|
||||
if tpl, err = template.New("").Parse(arg); err != nil {
|
||||
return
|
||||
}
|
||||
if err = tpl.Execute(buf, t); err != nil {
|
||||
return
|
||||
}
|
||||
args[idx] = buf.String()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if c.EnvVars != nil && len(c.EnvVars) > 0 {
|
||||
envs = make([]string, len(c.EnvVars))
|
||||
for idx, env := range c.EnvVars {
|
||||
buf.Reset()
|
||||
if tpl, err = template.New("").Parse(env); err != nil {
|
||||
return
|
||||
}
|
||||
if err = tpl.Execute(buf, t); err != nil {
|
||||
return
|
||||
}
|
||||
envs[idx] = buf.String()
|
||||
}
|
||||
}
|
||||
if !c.IsolateEnv {
|
||||
if envs != nil && len(envs) > 0 {
|
||||
envs = append(os.Environ(), envs...)
|
||||
} else {
|
||||
envs = os.Environ()
|
||||
}
|
||||
}
|
||||
|
||||
if args != nil && len(args) > 0 {
|
||||
cmd = exec.Command(progName, args...)
|
||||
} else {
|
||||
cmd = exec.Command(progName)
|
||||
}
|
||||
|
||||
cmd.Env = envs
|
||||
|
||||
return
|
||||
}
|
||||
21
tplCmd/types.go
Normal file
21
tplCmd/types.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package tplCmd
|
||||
|
||||
import (
|
||||
`encoding/xml`
|
||||
)
|
||||
|
||||
// Cmd is a command that is executed without any templating.
|
||||
type Cmd struct {
|
||||
XMLName xml.Name `json:"-" toml:"-" xml:"cmd" yaml:"-"`
|
||||
Program string `json:"bin" toml:"ProgramPath" xml:"bin,attr" yaml:"Program Path" validate:"required,file"`
|
||||
Args []string `json:"args" toml:"Argument" xml:"args>arg" yaml:"Arguments"`
|
||||
IsolateEnv bool `json:"isol8_env" toml:"IsolatedEnv" xml:"isol8Env,attr" yaml:"Isolated Environment"`
|
||||
EnvVars []string `json:"env" toml:"EnvVars" xml:"envs>env" yaml:"Environment Variables"`
|
||||
OnChanges *bool `json:"on_change,omitempty" toml:"OnChange,omitempty" xml:"onChange,attr,omitempty" yaml:"On Change,omitempty"`
|
||||
}
|
||||
|
||||
// TemplateCmd is a command that supports templating. (It's in its own package to avoid a cyclic dependency.)
|
||||
type TemplateCmd struct {
|
||||
*Cmd `yaml:",inline"`
|
||||
IsTemplate bool `json:"is_tpl" toml:"IsTemplate" xml:"isTpl,attr" yaml:"Is Template"`
|
||||
}
|
||||
Reference in New Issue
Block a user