ccl/internal/pkg/command/command.go

186 lines
4.5 KiB
Go
Raw Normal View History

/*
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"
)
type CommandType int
const (
CT_NOP CommandType = iota
CT_SHELL
CT_FUNC
CT_INDIRECT
CT_SET
CT_DEBUG
CT_CONDITIONAL
)
func (ct CommandType) String() string {
switch ct {
case CT_NOP:
return "NOP"
case CT_SHELL:
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{}
}
2022-07-29 19:42:46 -05:00
type Commands []Command
type CommandSet struct{
ID string
Commands
}
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_SHELL, cmd}
}
2022-07-29 18:58:52 -05:00
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 CommandSet) Command {
return Command{CT_SET, cs.Commands}
}
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 (cmds CommandSet) Execute(output io.Writer, fake bool) error {
for _, c := range cmds.Commands {
if err := c.Execute(output, fake, cmds.ID); err != nil {
2022-07-29 19:42:46 -05:00
return err
}
}
return nil
}
func (c Command) Execute(output io.Writer, fake bool, commandSetID string) error {
switch c.Type {
case CT_NOP:
fmt.Fprintf(output, "%s: %s\n", commandSetID, c.Type)
case CT_SHELL:
cmd := c.Command.(string)
fmt.Fprintf(output, "%s: %s sh -c \"%s\"\n", commandSetID, c.Type, cmd)
2022-07-19 01:38:25 -05:00
if !fake {
out, err := exec.Command("sh", "-c", cmd).CombinedOutput()
2022-07-19 19:05:42 -05:00
fmt.Fprint(output, string(out))
return err
}
case CT_FUNC:
ef := c.Command.(errFunc)
fmt.Fprintf(output, "%s: %s %s\n", commandSetID, c.Type, ef.Name)
if !fake {
if err := ef.Func(); err != nil {
return err
}
return nil
}
case CT_INDIRECT:
ct := c.Command.(Command)
return ct.Execute(output, fake, commandSetID)
case CT_SET:
cs := c.Command.(Commands)
for i := range cs {
err := cs[i].Execute(output, fake, commandSetID)
if err != nil {
return err
}
}
case CT_CONDITIONAL:
cond := c.Command.(conditional)
if fake {
// in a fake setting, we don't know which branch will be followed,
// so show what we would do in either branch
fmt.Fprintf(output, "%s: %s %s\n", commandSetID, c.Type, cond.Name)
fmt.Fprintf(output, "%s: -- True branch\n", commandSetID)
cond.ThenCmd.Execute(output, fake, commandSetID)
fmt.Fprintf(output, "%s: -- False branch\n", commandSetID)
cond.ElseCmd.Execute(output, fake, commandSetID)
fmt.Fprintf(output, "%s: -- End conditional\n", commandSetID)
} else {
branch := cond.Condition()
fmt.Fprintf(output, "%s: %s %s: %v\n", commandSetID, c.Type, cond.Name, branch)
if branch {
return cond.ThenCmd.Execute(output, fake, commandSetID)
} else {
return cond.ElseCmd.Execute(output, fake, commandSetID)
}
}
2022-07-19 19:47:35 -05:00
case CT_DEBUG:
msg := c.Command.(string)
fmt.Fprintf(output, "%s: %s %s\n", commandSetID, c.Type, msg)
}
return nil
}