/* Copyright © 2022 Joel D. Elkins 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_SH CT_REF 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_REF: 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 namedFunc struct { Name string Func func() string } type conditional struct { Name string Condition func() bool ThenCmd Command ElseCmd Command } func (f namedFunc) String() string { return f.Name + "()" } func NewShell(cmd string) Command { return Command{CT_SH, cmd} } func NewFunc(name string, f func() string) Command { return Command{CT_REF, namedFunc{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 = %d, Type = %d", CT_SH, c.Type) } func (c Command) CallRef() (string, error) { f, ok := c.Command.(namedFunc) if ok { s := f.Func() return s, nil } return "", fmt.Errorf("Type error. Requested = %d, Type = %d", CT_REF, c.Type) } 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.Fprintln(output, c.Type, "sh", "-c", cmd) if !fake { out, err := exec.Command("sh", "-c", cmd).CombinedOutput() fmt.Fprintln(output, string(out)) return err } case CT_REF: fmt.Fprintln(output, c.Type, c.Command) if !fake { s, err := c.CallRef() fmt.Fprintln(output, s) return err } case CT_INDIRECT: ct, ok := c.Command.(Command) if !ok { return fmt.Errorf("Type error: Requested = %d, Type = %d", 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 = %d, Type = %d", 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 = %d, Type = %d", 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) } } } return nil }