Remove "STRFUNC" command type

This commit is contained in:
Joel Elkins 2022-07-29 18:58:52 -05:00
parent 2f79041162
commit fc40b5c4a3
No known key found for this signature in database
GPG Key ID: 133589DC38921AE2
2 changed files with 16 additions and 58 deletions

View File

@ -34,7 +34,6 @@ type CommandType int
const (
CT_NOP CommandType = iota
CT_SH
CT_STRFUNC
CT_FUNC
CT_INDIRECT
CT_SET
@ -48,8 +47,6 @@ func (ct CommandType) String() string {
return "NOP"
case CT_SH:
return "SHELL"
case CT_STRFUNC:
return "STRFUNC"
case CT_FUNC:
return "FUNC"
case CT_INDIRECT:
@ -70,11 +67,6 @@ type Command struct {
Command interface{}
}
type namedFunc struct {
Name string
Func func() string
}
type errFunc struct {
Name string
Func func() error
@ -87,23 +79,11 @@ type conditional struct {
ElseCmd Command
}
func (f namedFunc) String() string {
return f.Name + "()"
}
func (f errFunc) 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_STRFUNC, namedFunc{name, f}}
}
func NewErrFunc(name string, f func() error) Command {
func NewFunc(name string, f func() error) Command {
return Command{CT_FUNC, errFunc{name, f}}
}
@ -140,15 +120,6 @@ func (c Command) GetShell() (string, error) {
return s, fmt.Errorf("Type error. Requested = %s, Type = %s", CT_SH, c.Type)
}
func (c Command) CallStrFunc() (string, error) {
f, ok := c.Command.(namedFunc)
if ok {
s := f.Func()
return s, nil
}
return "", fmt.Errorf("Type error. Requested = %s, Type = %s", CT_STRFUNC, c.Type)
}
func (c Command) Execute(output io.Writer, fake bool) error {
switch c.Type {
case CT_NOP:
@ -164,20 +135,12 @@ func (c Command) Execute(output io.Writer, fake bool) error {
fmt.Fprint(output, string(out))
return err
}
case CT_STRFUNC:
fmt.Fprintln(output, c.Type, c.Command)
if !fake {
s, err := c.CallStrFunc()
fmt.Fprintln(output, s)
return err
}
case CT_FUNC:
ef := c.Command.(errFunc)
msg := log.WithFields(log.Fields{
log.WithFields(log.Fields{
"type": CT_FUNC.String(),
"func": ef.Name,
})
msg.Debugf("Calling")
}).Debugf("Calling")
fmt.Fprintln(output, c.Type, ef.Name)
if !fake {
if err := ef.Func(); err != nil {

View File

@ -113,7 +113,7 @@ func (c *Container) pull() error {
func (c *Container) PullCommands() []command.Command {
return []command.Command{
command.NewErrFunc("do_pull", func() error {
command.NewFunc("do_pull", func() error {
return c.pull()
}),
}
@ -122,7 +122,7 @@ func (c *Container) PullCommands() []command.Command {
func (c *Container) CreateCommands() []command.Command {
if c.Image == "" {
return []command.Command{
command.NewErrFunc("image_error", func() error {
command.NewFunc("image_error", func() error {
return fmt.Errorf("Image not configured")
}),
}
@ -172,11 +172,9 @@ func (c *Container) CreateCommands() []command.Command {
Umask: fmt.Sprintf("%#o", c.Umask.Int64),
},
}
if err := spec.Validate(); err != nil {
c.LogEntry().WithField("error", err).Warnf("Spec does not validate")
}
return []command.Command{
command.NewErrFunc("bail_if_exists", func() error {
command.NewFunc("bail_if_exists", func() error {
if ex, err := containers.Exists(c.conn, c.Name, &containers.ExistsOptions{}); err != nil || ex {
if err != nil {
return err
@ -185,7 +183,7 @@ func (c *Container) CreateCommands() []command.Command {
}
return nil
}),
command.NewErrFunc("pull_if_necessary", func() error {
command.NewFunc("pull_if_necessary", func() error {
if ex, err := images.Exists(c.conn, c.Image, &images.ExistsOptions{}); err != nil || !ex {
if err != nil {
return err
@ -194,7 +192,8 @@ func (c *Container) CreateCommands() []command.Command {
}
return nil
}),
command.NewErrFunc("do_create", func() error {
command.NewFunc("validate_spec", spec.Validate),
command.NewFunc("do_create", func() error {
_, err := containers.CreateWithSpec(c.conn, &spec, nil)
if err != nil {
return err
@ -207,13 +206,9 @@ func (c *Container) CreateCommands() []command.Command {
func (c *Container) RecreateCommands() []command.Command {
wasRunning := false
return []command.Command{
command.NewFunc("stash_run_state", func() string {
command.NewFunc("stash_run_state", func() error {
wasRunning = c.IsRunning()
runMsg := "not running. Will not start it after recreating."
if wasRunning {
runMsg = "running. Will restart after recreating."
}
return fmt.Sprintf("Container %s is %s", c.Name, runMsg)
return nil
}),
command.NewSet(c.DestroyCommands()),
command.NewSet(c.CreateCommands()),
@ -227,7 +222,7 @@ func (c *Container) RecreateCommands() []command.Command {
func (c *Container) DestroyCommands() []command.Command {
cmds := c.StopCommands()
cmds = append(cmds, command.NewErrFunc("remove_if_exists", func() error {
cmds = append(cmds, command.NewFunc("remove_if_exists", func() error {
if c.cdata.ID == "" {
return nil
}
@ -240,7 +235,7 @@ func (c *Container) DestroyCommands() []command.Command {
func (c *Container) StartCommands() []command.Command {
return []command.Command{
command.NewErrFunc("start_container", func() error {
command.NewFunc("start_container", func() error {
if c.cdata.State != nil && c.cdata.State.Running {
return nil
}
@ -289,7 +284,7 @@ func (c *Container) IsCreated() bool {
func (c *Container) UpdateCommands() []command.Command {
wasRunning := false
return []command.Command{
command.NewErrFunc("do_update_and_stop", func() error {
command.NewFunc("do_update_and_stop", func() error {
err := c.pull()
if err != nil {
return err
@ -320,7 +315,7 @@ func (c *Container) UpdateCommands() []command.Command {
func (c *Container) StopCommands() []command.Command {
return []command.Command{
command.NewErrFunc("stop_if_running", func() error {
command.NewFunc("stop_if_running", func() error {
if c.IsRunning() {
var timeout uint = 10
err := containers.Stop(c.conn, c.cdata.ID, &containers.StopOptions{Timeout: &timeout})