github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow@v0.28.1-0.20240311201729-34c6856b157f/pkg/command/create.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 command
    21  
    22  import (
    23  	"fmt"
    24  	"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/common"
    25  	"github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow/pkg/metadata"
    26  	"github.com/ory/viper"
    27  	"github.com/spf13/cobra"
    28  	"os"
    29  )
    30  
    31  type CreateCmdConfig struct {
    32  	ProjectName string
    33  	YAML        bool
    34  }
    35  
    36  func NewCreateCommand() *cobra.Command {
    37  	var cmd = &cobra.Command{
    38  		Use:   "create",
    39  		Short: "Creates a new SonataFlow project",
    40  		Long: `
    41  	Creates a Workflow file in the specified directory (new-project is the default).
    42  
    43  	This SonataFlow project targets use cases requiring a single Serverless
    44  	Workflow file definition.
    45  
    46  	Additionally, you can define the configurable parameters of your application in the 
    47  	"application.properties" file (inside the root project directory).
    48  	You can also store your spec files (i.e., Open API files) inside the "specs" folder,
    49  	schemas file inside "schemas" folder and also subflows inside "subflows" folder.
    50  
    51  	A SonataFlow project, as the following structure by default:
    52  
    53  	Workflow project root
    54  		/specs (optional)
    55  		/schemas (optional)
    56  		/subflows (optional)
    57  		workflow.sw.{json|yaml|yml} (mandatory)
    58  
    59  	`,
    60  		Example: `
    61  	# Create a project in the local directory
    62  	# By default the project is named "new-project"
    63  	{{.Name}} create
    64  
    65  	# Create a project with an specific name
    66  	{{.Name}} create --name myproject
    67  
    68  	# Creates a YAML sample workflow file (JSON is default)
    69  	{{.Name}} create --yaml-workflow
    70  		`,
    71  		SuggestFor: []string{"vreate", "creaet", "craete", "new"}, //nolint:misspell
    72  		PreRunE:    common.BindEnv("name", "yaml-workflow"),
    73  	}
    74  
    75  	cmd.RunE = func(cmd *cobra.Command, args []string) error {
    76  		cfg, err := runCreateCmdConfig()
    77  		if err != nil {
    78  			return fmt.Errorf("initializing create config: %w", err)
    79  		}
    80  		return runCreate(cfg)
    81  	}
    82  
    83  	cmd.Flags().StringP("name", "n", "new-project", "Project name created in the current directory.")
    84  	cmd.Flags().Bool("yaml-workflow", false, "Create a sample YAML workflow file.")
    85  	cmd.SetHelpFunc(common.DefaultTemplatedHelp)
    86  
    87  	return cmd
    88  }
    89  
    90  func runCreate(cfg CreateCmdConfig) error {
    91  	fmt.Println("🛠️ Creating SonataFlow project")
    92  
    93  	if err := os.Mkdir(cfg.ProjectName, os.ModePerm); err != nil {
    94  		return fmt.Errorf("❌ ERROR: Error creating project directory: %w", err)
    95  	}
    96  
    97  	var workflowFormat string
    98  	if cfg.YAML {
    99  		workflowFormat = metadata.WorkflowSwYaml
   100  	} else {
   101  		workflowFormat = metadata.WorkflowSwJson
   102  	}
   103  
   104  	workflowPath := fmt.Sprintf("./%s/%s", cfg.ProjectName, workflowFormat)
   105  	if err := common.CreateWorkflow(workflowPath, cfg.YAML); err != nil {
   106  		return fmt.Errorf("❌ ERROR: Error creating workflow file: %w", err)
   107  	}
   108  
   109  	fmt.Println("🎉 SonataFlow project successfully created")
   110  
   111  	return nil
   112  
   113  }
   114  
   115  func runCreateCmdConfig() (cfg CreateCmdConfig, err error) {
   116  
   117  	cfg = CreateCmdConfig{
   118  		ProjectName: viper.GetString("name"),
   119  		YAML:        viper.GetBool("yaml-workflow"),
   120  	}
   121  	return cfg, nil
   122  }