github.com/swaros/contxt/module/taskrun@v0.0.0-20240305083542-3dbd4436ac40/export.go (about)

     1  // Copyright (c) 2020 Thomas Ziegler <thomas.zglr@googlemail.com>. All rights reserved.
     2  //
     3  // Licensed under the MIT License
     4  //
     5  //
     6  // Permission is hereby granted, free of charge, to any person obtaining a copy
     7  // of this software and associated documentation files (the "Software"), to deal
     8  // in the Software without restriction, including without limitation the rights
     9  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    10  // copies of the Software, and to permit persons to whom the Software is
    11  // furnished to do so, subject to the following conditions:
    12  //
    13  // The above copyright notice and this permission notice shall be included in all
    14  // copies or substantial portions of the Software.
    15  //
    16  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    17  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    18  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    19  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    20  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    21  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    22  // SOFTWARE.
    23  package taskrun
    24  
    25  import (
    26  	"errors"
    27  	"strings"
    28  )
    29  
    30  func ExportTask(target string) (string, error) {
    31  	template, _, exists, terr := GetTemplate()
    32  	if terr != nil {
    33  		return "", terr
    34  	}
    35  	if !exists {
    36  		return "", errors.New("template not exists")
    37  	}
    38  	var out string = ""
    39  	for _, task := range template.Task {
    40  		if task.ID == target {
    41  			if canRun, message := checkRequirements(task.Requires); canRun {
    42  				for _, need := range task.Needs {
    43  					out = out + "\n# --- target " + need + " included ---- this is a need of " + target + "\n\n"
    44  					if needtask, nErr := ExportTask(need); nErr == nil {
    45  						out = out + needtask + "\n"
    46  					}
    47  				}
    48  				out = out + strings.Join(task.Script, "\n") + "\n"
    49  				for _, next := range task.Next {
    50  					out = out + "\n# --- target " + next + " included ---- this is a next-task of " + target + "\n\n"
    51  					if nextJob, sErr := ExportTask(next); sErr == nil {
    52  						out = out + nextJob + "\n"
    53  					}
    54  				}
    55  			} else {
    56  				out = out + "\n# --- -----------------------------------------------------------------------------------  ---- \n"
    57  				out = out + "# --- a  sequence of the target " + target + " is ignored because of a failed requirement  ---- \n"
    58  				out = out + "# --- this is might be an usual case. The reported reason to skip: " + message + "  \n"
    59  				out = out + "# --- -----------------------------------------------------------------------------------  ---- \n"
    60  			}
    61  		}
    62  	}
    63  	return out, nil
    64  }