github.com/upcmd/up@v0.8.1-0.20230108151705-ad8b797bf04f/biz/impl/blockfunc.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  type BlockFuncAction struct {
    17  	Do        interface{}
    18  	Vars      *core.Cache
    19  	Tasknames []string
    20  	Steps     *Steps
    21  }
    22  
    23  func (f *BlockFuncAction) Adapt() {
    24  	var flowname string
    25  	var flow Steps
    26  
    27  	switch f.Do.(type) {
    28  	case string:
    29  		//a flow name + refdir to load the flow
    30  		raw := f.Do.(string)
    31  		flowname = Render(raw, f.Vars)
    32  		u.P(flowname)
    33  
    34  	case []interface{}:
    35  		err := ms.Decode(f.Do, &flow)
    36  		u.LogErrorAndPanic("load steps", err, "steps has configuration problem, please fix it")
    37  
    38  	default:
    39  		u.LogWarn("Block func", "Not implemented or void for no action!")
    40  	}
    41  
    42  	f.Steps = &flow
    43  }
    44  
    45  func (f *BlockFuncAction) Exec() {
    46  	BlockFlowRun(f.Steps, f.Vars)
    47  }
    48  
    49  func BlockFlowRun(flow *Steps, execVars *core.Cache) {
    50  	rtContext := BlockRuntimeContext{
    51  		BlockBaseVars: execVars,
    52  	}
    53  	BlockStack().Push(&rtContext)
    54  
    55  	flow.Exec(true)
    56  	BlockStack().Pop()
    57  }