github.com/kubeshop/testkube@v1.17.23/cmd/kubectl-testkube/commands/testworkflowtemplates/create.go (about) 1 package testworkflowtemplates 2 3 import ( 4 "io" 5 "os" 6 7 "github.com/spf13/cobra" 8 9 testworkflowsv1 "github.com/kubeshop/testkube-operator/api/testworkflows/v1" 10 "github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common" 11 common2 "github.com/kubeshop/testkube/internal/common" 12 "github.com/kubeshop/testkube/pkg/tcl/mapperstcl/testworkflows" 13 "github.com/kubeshop/testkube/pkg/ui" 14 ) 15 16 func NewCreateTestWorkflowTemplateCmd() *cobra.Command { 17 var ( 18 name string 19 filePath string 20 update bool 21 ) 22 23 cmd := &cobra.Command{ 24 Use: "testworkflowtemplate", 25 Aliases: []string{"testworkflowtemplates", "twt"}, 26 Args: cobra.MaximumNArgs(0), 27 Short: "Create test workflow template", 28 29 Run: func(cmd *cobra.Command, _ []string) { 30 namespace := cmd.Flag("namespace").Value.String() 31 32 var input io.Reader 33 if filePath == "" { 34 fi, err := os.Stdin.Stat() 35 ui.ExitOnError("reading stdin", err) 36 if fi.Mode()&os.ModeDevice != 0 { 37 ui.Failf("you need to pass stdin or --file argument with file path") 38 } 39 input = cmd.InOrStdin() 40 } else { 41 file, err := os.Open(filePath) 42 ui.ExitOnError("reading "+filePath+" file", err) 43 input = file 44 } 45 46 bytes, err := io.ReadAll(input) 47 ui.ExitOnError("reading input", err) 48 49 obj := new(testworkflowsv1.TestWorkflowTemplate) 50 err = common2.DeserializeCRD(obj, bytes) 51 ui.ExitOnError("deserializing input", err) 52 if obj.Kind != "" && obj.Kind != "TestWorkflowTemplate" { 53 ui.Failf("Only TestWorkflowTemplate objects are accepted. Received: %s", obj.Kind) 54 } 55 common2.AppendTypeMeta("TestWorkflowTemplate", testworkflowsv1.GroupVersion, obj) 56 obj.Namespace = namespace 57 if name != "" { 58 obj.Name = name 59 } 60 61 client, _, err := common.GetClient(cmd) 62 ui.ExitOnError("getting client", err) 63 64 workflow, _ := client.GetTestWorkflowTemplate(obj.Name) 65 if workflow.Name != "" { 66 if !update { 67 ui.Failf("Test workflow template with name '%s' already exists in namespace %s, use --update flag for upsert", obj.Name, namespace) 68 } 69 _, err = client.UpdateTestWorkflowTemplate(testworkflows.MapTestWorkflowTemplateKubeToAPI(*obj)) 70 ui.ExitOnError("updating test workflow template "+obj.Name+" in namespace "+obj.Namespace, err) 71 ui.Success("Test workflow template updated", namespace, "/", obj.Name) 72 } else { 73 _, err = client.CreateTestWorkflowTemplate(testworkflows.MapTestWorkflowTemplateKubeToAPI(*obj)) 74 ui.ExitOnError("creating test workflow "+obj.Name+" in namespace "+obj.Namespace, err) 75 ui.Success("Test workflow template created", namespace, "/", obj.Name) 76 } 77 }, 78 } 79 80 cmd.Flags().StringVar(&name, "name", "", "test workflow template name") 81 cmd.Flags().BoolVar(&update, "update", false, "update, if test workflow template already exists") 82 cmd.Flags().StringVarP(&filePath, "file", "f", "", "file path to get the test workflow template specification") 83 84 return cmd 85 }