2022-07-18 10:10:24 -05:00
|
|
|
/*
|
|
|
|
Copyright © 2022 Joel D. Elkins <joel@elkins.co>
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os/exec"
|
2022-07-25 10:13:36 -05:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2022-07-18 10:10:24 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type CommandType int
|
|
|
|
|
2022-07-18 19:47:05 -05:00
|
|
|
const (
|
|
|
|
CT_NOP CommandType = iota
|
|
|
|
CT_SH
|
2022-07-25 10:13:36 -05:00
|
|
|
CT_STRFUNC
|
|
|
|
CT_FUNC
|
2022-07-18 19:47:05 -05:00
|
|
|
CT_INDIRECT
|
|
|
|
CT_SET
|
|
|
|
CT_DEBUG
|
|
|
|
CT_CONDITIONAL
|
|
|
|
)
|
|
|
|
|
2022-07-18 10:10:24 -05:00
|
|
|
func (ct CommandType) String() string {
|
|
|
|
switch ct {
|
2022-07-18 19:47:05 -05:00
|
|
|
case CT_NOP:
|
|
|
|
return "NOP"
|
2022-07-18 10:10:24 -05:00
|
|
|
case CT_SH:
|
|
|
|
return "SHELL"
|
2022-07-25 10:13:36 -05:00
|
|
|
case CT_STRFUNC:
|
|
|
|
return "STRFUNC"
|
|
|
|
case CT_FUNC:
|
2022-07-18 10:10:24 -05:00
|
|
|
return "FUNC"
|
|
|
|
case CT_INDIRECT:
|
|
|
|
return "INDIRECT"
|
|
|
|
case CT_SET:
|
|
|
|
return "SET"
|
|
|
|
case CT_DEBUG:
|
|
|
|
return "DEBUG"
|
2022-07-18 19:47:05 -05:00
|
|
|
case CT_CONDITIONAL:
|
|
|
|
return "CONDITIONAL"
|
2022-07-18 10:10:24 -05:00
|
|
|
default:
|
|
|
|
return "UNKOWN"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Command struct {
|
|
|
|
Type CommandType
|
|
|
|
Command interface{}
|
|
|
|
}
|
|
|
|
|
2022-07-18 19:47:05 -05:00
|
|
|
type namedFunc struct {
|
|
|
|
Name string
|
|
|
|
Func func() string
|
|
|
|
}
|
|
|
|
|
2022-07-25 10:13:36 -05:00
|
|
|
type errFunc struct {
|
|
|
|
Name string
|
|
|
|
Func func() error
|
|
|
|
}
|
|
|
|
|
2022-07-18 19:47:05 -05:00
|
|
|
type conditional struct {
|
|
|
|
Name string
|
|
|
|
Condition func() bool
|
|
|
|
ThenCmd Command
|
|
|
|
ElseCmd Command
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f namedFunc) String() string {
|
|
|
|
return f.Name + "()"
|
|
|
|
}
|
|
|
|
|
2022-07-25 10:13:36 -05:00
|
|
|
func (f errFunc) String() string {
|
|
|
|
return f.Name + "()"
|
|
|
|
}
|
|
|
|
|
2022-07-18 10:10:24 -05:00
|
|
|
func NewShell(cmd string) Command {
|
|
|
|
return Command{CT_SH, cmd}
|
|
|
|
}
|
|
|
|
|
2022-07-18 19:47:05 -05:00
|
|
|
func NewFunc(name string, f func() string) Command {
|
2022-07-25 10:13:36 -05:00
|
|
|
return Command{CT_STRFUNC, namedFunc{name, f}}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewErrFunc(name string, f func() error) Command {
|
|
|
|
return Command{CT_FUNC, errFunc{name, f}}
|
2022-07-18 10:10:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIndirect(c Command) Command {
|
|
|
|
return Command{CT_INDIRECT, c}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSet(cs []Command) Command {
|
|
|
|
return Command{CT_SET, cs}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDebug(msg string) Command {
|
|
|
|
return Command{CT_DEBUG, msg}
|
|
|
|
}
|
|
|
|
|
2022-07-18 19:47:05 -05:00
|
|
|
func NewNop() Command {
|
|
|
|
return Command{CT_NOP, nil}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConditional(name string, ifPart func() bool, thenPart Command, elsePart Command) Command {
|
|
|
|
return Command{CT_CONDITIONAL, conditional{
|
|
|
|
Name: name,
|
|
|
|
Condition: ifPart,
|
|
|
|
ThenCmd: thenPart,
|
|
|
|
ElseCmd: elsePart,
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2022-07-18 10:10:24 -05:00
|
|
|
func (c Command) GetShell() (string, error) {
|
|
|
|
s, ok := c.Command.(string)
|
|
|
|
if ok {
|
|
|
|
return s, nil
|
|
|
|
}
|
2022-07-19 19:47:35 -05:00
|
|
|
return s, fmt.Errorf("Type error. Requested = %s, Type = %s", CT_SH, c.Type)
|
2022-07-18 10:10:24 -05:00
|
|
|
}
|
|
|
|
|
2022-07-25 10:13:36 -05:00
|
|
|
func (c Command) CallStrFunc() (string, error) {
|
2022-07-18 19:47:05 -05:00
|
|
|
f, ok := c.Command.(namedFunc)
|
2022-07-18 10:10:24 -05:00
|
|
|
if ok {
|
2022-07-18 19:47:05 -05:00
|
|
|
s := f.Func()
|
2022-07-18 10:10:24 -05:00
|
|
|
return s, nil
|
|
|
|
}
|
2022-07-25 10:13:36 -05:00
|
|
|
return "", fmt.Errorf("Type error. Requested = %s, Type = %s", CT_STRFUNC, c.Type)
|
2022-07-18 10:10:24 -05:00
|
|
|
}
|
|
|
|
|
2022-07-18 19:47:05 -05:00
|
|
|
func (c Command) Execute(output io.Writer, fake bool) error {
|
2022-07-18 10:10:24 -05:00
|
|
|
switch c.Type {
|
2022-07-18 19:47:05 -05:00
|
|
|
case CT_NOP:
|
|
|
|
fmt.Fprintln(output, c.Type)
|
2022-07-18 10:10:24 -05:00
|
|
|
case CT_SH:
|
|
|
|
cmd, err := c.GetShell()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-19 19:05:42 -05:00
|
|
|
fmt.Fprintf(output, "%s sh -c \"%s\"\n", c.Type, cmd)
|
2022-07-19 01:38:25 -05:00
|
|
|
if !fake {
|
2022-07-18 19:47:05 -05:00
|
|
|
out, err := exec.Command("sh", "-c", cmd).CombinedOutput()
|
2022-07-19 19:05:42 -05:00
|
|
|
fmt.Fprint(output, string(out))
|
2022-07-18 19:47:05 -05:00
|
|
|
return err
|
|
|
|
}
|
2022-07-25 10:13:36 -05:00
|
|
|
case CT_STRFUNC:
|
2022-07-19 01:38:25 -05:00
|
|
|
fmt.Fprintln(output, c.Type, c.Command)
|
|
|
|
if !fake {
|
2022-07-25 10:13:36 -05:00
|
|
|
s, err := c.CallStrFunc()
|
2022-07-19 01:38:25 -05:00
|
|
|
fmt.Fprintln(output, s)
|
2022-07-18 19:47:05 -05:00
|
|
|
return err
|
|
|
|
}
|
2022-07-25 10:13:36 -05:00
|
|
|
case CT_FUNC:
|
|
|
|
ef := c.Command.(errFunc)
|
|
|
|
msg := log.WithFields(log.Fields{
|
|
|
|
"type": CT_FUNC.String(),
|
|
|
|
"func": ef.Name,
|
|
|
|
})
|
|
|
|
msg.Debugf("Calling")
|
|
|
|
fmt.Fprintln(output, c.Type, ef.Name)
|
|
|
|
if !fake {
|
|
|
|
if err := ef.Func(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-07-18 10:10:24 -05:00
|
|
|
case CT_INDIRECT:
|
|
|
|
ct, ok := c.Command.(Command)
|
|
|
|
if !ok {
|
2022-07-19 19:47:35 -05:00
|
|
|
return fmt.Errorf("Type error: Requested = %s, Type = %s", CT_INDIRECT, c.Type)
|
2022-07-18 10:10:24 -05:00
|
|
|
}
|
2022-07-18 19:47:05 -05:00
|
|
|
return ct.Execute(output, fake)
|
2022-07-18 10:10:24 -05:00
|
|
|
case CT_SET:
|
|
|
|
cs, ok := c.Command.([]Command)
|
|
|
|
if !ok {
|
2022-07-19 19:47:35 -05:00
|
|
|
return fmt.Errorf("Type error: Requested = %s, Type = %s", CT_SET, c.Type)
|
2022-07-18 10:10:24 -05:00
|
|
|
}
|
|
|
|
for i := range cs {
|
2022-07-18 19:47:05 -05:00
|
|
|
err := cs[i].Execute(output, fake)
|
2022-07-18 10:10:24 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-07-18 19:47:05 -05:00
|
|
|
case CT_CONDITIONAL:
|
|
|
|
cond, ok := c.Command.(conditional)
|
|
|
|
if !ok {
|
2022-07-19 19:47:35 -05:00
|
|
|
return fmt.Errorf("Type error: Requested = %s, Type = %s", CT_CONDITIONAL, c.Type)
|
2022-07-18 19:47:05 -05:00
|
|
|
}
|
|
|
|
if fake {
|
|
|
|
fmt.Fprintln(output, c.Type, cond.Name)
|
|
|
|
fmt.Fprintln(output, "-- True branch")
|
|
|
|
cond.ThenCmd.Execute(output, fake)
|
|
|
|
fmt.Fprintln(output, "-- False branch")
|
|
|
|
cond.ElseCmd.Execute(output, fake)
|
|
|
|
fmt.Fprintln(output, "-- End conditional")
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
branch := cond.Condition()
|
|
|
|
fmt.Fprintln(output, c.Type, cond.Name, ":", branch)
|
|
|
|
if branch {
|
|
|
|
return cond.ThenCmd.Execute(output, fake)
|
|
|
|
} else {
|
|
|
|
return cond.ElseCmd.Execute(output, fake)
|
|
|
|
}
|
|
|
|
}
|
2022-07-19 19:47:35 -05:00
|
|
|
case CT_DEBUG:
|
|
|
|
msg, ok := c.Command.(string)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Type error: Requested = %s, Type = %s", CT_DEBUG, c.Type)
|
|
|
|
}
|
|
|
|
fmt.Fprintln(output, c.Type, msg)
|
2022-07-18 10:10:24 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|