ccl/internal/pkg/command/command.go

209 lines
4.7 KiB
Go

/*
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"
log "github.com/sirupsen/logrus"
)
type CommandType int
const (
CT_NOP CommandType = iota
CT_SH
CT_FUNC
CT_INDIRECT
CT_SET
CT_DEBUG
CT_CONDITIONAL
)
func (ct CommandType) String() string {
switch ct {
case CT_NOP:
return "NOP"
case CT_SH:
return "SHELL"
case CT_FUNC:
return "FUNC"
case CT_INDIRECT:
return "INDIRECT"
case CT_SET:
return "SET"
case CT_DEBUG:
return "DEBUG"
case CT_CONDITIONAL:
return "CONDITIONAL"
default:
return "UNKOWN"
}
}
type Command struct {
Type CommandType
Command interface{}
}
type Commands []Command
type errFunc struct {
Name string
Func func() error
}
type conditional struct {
Name string
Condition func() bool
ThenCmd Command
ElseCmd Command
}
func NewShell(cmd string) Command {
return Command{CT_SH, cmd}
}
func NewFunc(name string, f func() error) Command {
return Command{CT_FUNC, errFunc{name, f}}
}
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}
}
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,
}}
}
func (c Command) GetShell() (string, error) {
s, ok := c.Command.(string)
if ok {
return s, nil
}
return s, fmt.Errorf("Type error. Requested = %s, Type = %s", CT_SH, c.Type)
}
func (cmds Commands) Execute(output io.Writer, fake bool) error {
for _, c := range cmds {
if err := c.Execute(output, fake); err != nil {
return err
}
}
return nil
}
func (c Command) Execute(output io.Writer, fake bool) error {
switch c.Type {
case CT_NOP:
fmt.Fprintln(output, c.Type)
case CT_SH:
cmd, err := c.GetShell()
if err != nil {
return err
}
fmt.Fprintf(output, "%s sh -c \"%s\"\n", c.Type, cmd)
if !fake {
out, err := exec.Command("sh", "-c", cmd).CombinedOutput()
fmt.Fprint(output, string(out))
return err
}
case CT_FUNC:
ef := c.Command.(errFunc)
log.WithFields(log.Fields{
"type": CT_FUNC.String(),
"func": ef.Name,
}).Debugf("Calling")
fmt.Fprintln(output, c.Type, ef.Name)
if !fake {
if err := ef.Func(); err != nil {
return err
}
return nil
}
case CT_INDIRECT:
ct, ok := c.Command.(Command)
if !ok {
return fmt.Errorf("Type error: Requested = %s, Type = %s", CT_INDIRECT, c.Type)
}
return ct.Execute(output, fake)
case CT_SET:
cs, ok := c.Command.([]Command)
if !ok {
return fmt.Errorf("Type error: Requested = %s, Type = %s", CT_SET, c.Type)
}
for i := range cs {
err := cs[i].Execute(output, fake)
if err != nil {
return err
}
}
case CT_CONDITIONAL:
cond, ok := c.Command.(conditional)
if !ok {
return fmt.Errorf("Type error: Requested = %s, Type = %s", CT_CONDITIONAL, c.Type)
}
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)
}
}
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)
}
return nil
}