github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow@v0.28.1-0.20240311201729-34c6856b157f/pkg/common/create_workflow.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one
     3   * or more contributor license agreements.  See the NOTICE file
     4   * distributed with this work for additional information
     5   * regarding copyright ownership.  The ASF licenses this file
     6   * to you under the Apache License, Version 2.0 (the
     7   * "License"); you may not use this file except in compliance
     8   * with the License.  You may obtain a copy of the License at
     9   *
    10   *  http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing,
    13   * software distributed under the License is distributed on an
    14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   * KIND, either express or implied.  See the License for the
    16   * specific language governing permissions and limitations
    17   * under the License.
    18   */
    19  
    20  package common
    21  
    22  import (
    23  	"encoding/json"
    24  	"fmt"
    25  	"github.com/spf13/afero"
    26  	"gopkg.in/yaml.v2"
    27  )
    28  
    29  type WorkflowStates struct {
    30  	Name string            `json:"name"`
    31  	Type string            `json:"type"`
    32  	Data map[string]string `json:"data"`
    33  	End  bool              `json:"end"`
    34  }
    35  
    36  type Workflow struct {
    37  	Id          string           `json:"id"`
    38  	Version     string           `json:"version"`
    39  	SpecVersion string           `json:"specVersion" yaml:"specVersion"`
    40  	Name        string           `json:"name"`
    41  	Description string           `json:"description"`
    42  	Start       string           `json:"start"`
    43  	States      []WorkflowStates `json:"states"`
    44  }
    45  
    46  func GetWorkflowTemplate(yamlWorkflow bool) (workflowByte []byte, err error) {
    47  	workflowStates := WorkflowStates{
    48  		Name: "HelloWorld",
    49  		Type: "inject",
    50  		Data: map[string]string{
    51  			"message": "Hello World",
    52  		},
    53  		End: true,
    54  	}
    55  
    56  	workflow := Workflow{
    57  		Id:          "hello",
    58  		Version:     "1.0",
    59  		SpecVersion: "0.8.0",
    60  		Name:        "Hello World",
    61  		Description: "Description",
    62  		Start:       "HelloWorld",
    63  		States:      []WorkflowStates{workflowStates},
    64  	}
    65  
    66  	if yamlWorkflow {
    67  		workflowByte, err = yaml.Marshal(workflow)
    68  		if err != nil {
    69  			return nil, fmt.Errorf("error marshaling the workflow file. %w", err)
    70  		}
    71  	} else {
    72  		workflowByte, err = json.MarshalIndent(workflow, "", "  ")
    73  		if err != nil {
    74  			return nil, fmt.Errorf("error marshaling the workflow file. %w", err)
    75  		}
    76  	}
    77  
    78  	return workflowByte, nil
    79  }
    80  
    81  func CreateWorkflow(workflowFilePath string, yamlWorkflow bool) (err error) {
    82  
    83  	workflowByte, err := GetWorkflowTemplate(yamlWorkflow)
    84  	err = afero.WriteFile(FS, workflowFilePath, workflowByte, 0644)
    85  	if err != nil {
    86  		return fmt.Errorf("error writing the workflow file: %w", err)
    87  	}
    88  
    89  	fmt.Printf("Workflow file created at %s \n", workflowFilePath)
    90  	return nil
    91  }