github.com/upcmd/up@v0.8.1-0.20230108151705-ad8b797bf04f/biz/impl/callfunc.go (about)

     1  // Ultimate Provisioner: UP cmd
     2  // Copyright (c) 2019 Stephen Cheng and contributors
     3  
     4  /* This Source Code Form is subject to the terms of the Mozilla Public
     5   * License, v. 2.0. If a copy of the MPL was not distributed with this
     6   * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
     7  
     8  package impl
     9  
    10  import (
    11  	ms "github.com/mitchellh/mapstructure"
    12  	"github.com/upcmd/up/model/core"
    13  	u "github.com/upcmd/up/utils"
    14  )
    15  
    16  func runTask(f *CallFuncAction, taskname string) {
    17  	u.PpmsgvvvvvhintHigh(u.Spf("caller's vars to task (%s):", taskname), f.Vars)
    18  	ExecTask(taskname, f.Vars)
    19  }
    20  
    21  type CallFuncAction struct {
    22  	Do        interface{}
    23  	Vars      *core.Cache
    24  	Tasknames []string
    25  }
    26  
    27  //adapt the abstract step.Do to concrete ShellFuncAction Cmds
    28  func (f *CallFuncAction) Adapt() {
    29  	var taskname string
    30  	var tasknames []string
    31  
    32  	switch f.Do.(type) {
    33  	case string:
    34  		taskname = f.Do.(string)
    35  		tasknames = append(tasknames, taskname)
    36  
    37  	case []interface{}:
    38  		err := ms.Decode(f.Do, &tasknames)
    39  		u.LogErrorAndPanic("call func adapter", err, "please ref to a task name only")
    40  
    41  	default:
    42  		u.LogWarn("call func", "Not implemented or void for no action!")
    43  	}
    44  	f.Tasknames = tasknames
    45  }
    46  
    47  func (f *CallFuncAction) Exec() {
    48  	for _, tmptaskname := range f.Tasknames {
    49  		taskname := Render(tmptaskname, f.Vars)
    50  		runTask(f, taskname)
    51  	}
    52  }