github.com/kubeshop/testkube@v1.17.23/cmd/kubectl-testkube/commands/testsources/create.go (about) 1 package testsources 2 3 import ( 4 "fmt" 5 "os" 6 "strconv" 7 8 "github.com/spf13/cobra" 9 10 "github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common" 11 apiv1 "github.com/kubeshop/testkube/pkg/api/v1/client" 12 "github.com/kubeshop/testkube/pkg/crd" 13 "github.com/kubeshop/testkube/pkg/ui" 14 ) 15 16 func NewCreateTestSourceCmd() *cobra.Command { 17 var ( 18 name, uri string 19 sourceType string 20 file string 21 gitUri string 22 gitBranch string 23 gitCommit string 24 gitPath string 25 gitUsername string 26 gitToken string 27 gitWorkingDir string 28 labels map[string]string 29 gitUsernameSecret map[string]string 30 gitTokenSecret map[string]string 31 gitCertificateSecret string 32 gitAuthType string 33 update bool 34 ) 35 36 cmd := &cobra.Command{ 37 Use: "testsource", 38 Aliases: []string{"testsources", "tsc"}, 39 Short: "Create new TestSource", 40 Long: `Create new TestSource Custom Resource`, 41 Run: func(cmd *cobra.Command, args []string) { 42 crdOnly, err := strconv.ParseBool(cmd.Flag("crd-only").Value.String()) 43 ui.ExitOnError("parsing flag value", err) 44 45 if name == "" { 46 ui.Failf("pass valid name (in '--name' flag)") 47 } 48 49 namespace := cmd.Flag("namespace").Value.String() 50 var client apiv1.Client 51 if !crdOnly { 52 client, namespace, err = common.GetClient(cmd) 53 ui.ExitOnError("getting client", err) 54 55 testsource, _ := client.GetTestSource(name) 56 if name == testsource.Name { 57 if cmd.Flag("update").Changed { 58 if !update { 59 ui.Failf("TestSource with name '%s' already exists in namespace %s, ", testsource.Name, namespace) 60 } 61 } else { 62 var ok bool 63 if stat, _ := os.Stdin.Stat(); (stat.Mode() & os.ModeCharDevice) != 0 { 64 ok = ui.Confirm(fmt.Sprintf("TestSource with name '%s' already exists in namespace %s, ", testsource.Name, namespace) + 65 "do you want to overwrite it?") 66 } 67 68 if !ok { 69 ui.Failf("TestSource creation was aborted") 70 } 71 } 72 73 options, err := NewUpdateTestSourceOptionsFromFlags(cmd) 74 ui.ExitOnError("getting test source options", err) 75 76 _, err = client.UpdateTestSource(options) 77 ui.ExitOnError("updating test source "+name+" in namespace "+namespace, err) 78 79 ui.SuccessAndExit("TestSource updated", name) 80 } 81 } 82 83 err = common.ValidateUpsertOptions(cmd, "") 84 ui.ExitOnError("validating passed flags", err) 85 86 options, err := NewUpsertTestSourceOptionsFromFlags(cmd) 87 ui.ExitOnError("getting test source options", err) 88 89 if !crdOnly { 90 _, err := client.CreateTestSource(options) 91 ui.ExitOnError("creating test source "+name+" in namespace "+namespace, err) 92 93 ui.Success("TestSource created", name) 94 } else { 95 if options.Data != "" { 96 options.Data = fmt.Sprintf("%q", options.Data) 97 } 98 99 data, err := crd.ExecuteTemplate(crd.TemplateTestSource, options) 100 ui.ExitOnError("executing crd template", err) 101 102 ui.Info(data) 103 } 104 }, 105 } 106 107 cmd.Flags().StringVarP(&name, "name", "n", "", "unique test source name - mandatory") 108 cmd.Flags().StringToStringVarP(&labels, "label", "l", nil, "label key value pair: --label key1=value1") 109 cmd.Flags().StringVarP(&sourceType, "source-type", "", "", "source type of test one of string|file-uri|git") 110 cmd.Flags().StringVarP(&file, "file", "f", "", "source file - will be read from stdin if not specified") 111 cmd.Flags().StringVarP(&uri, "uri", "u", "", "URI which should be called to get test content") 112 cmd.Flags().StringVarP(&gitUri, "git-uri", "", "", "Git repository uri") 113 cmd.Flags().StringVarP(&gitBranch, "git-branch", "", "", "if uri is git repository we can set additional branch parameter") 114 cmd.Flags().StringVarP(&gitCommit, "git-commit", "", "", "if uri is git repository we can use commit id (sha) parameter") 115 cmd.Flags().StringVarP(&gitPath, "git-path", "", "", "if repository is big we need to define additional path to directory/file to checkout partially") 116 cmd.Flags().StringVarP(&gitUsername, "git-username", "", "", "if git repository is private we can use username as an auth parameter") 117 cmd.Flags().StringVarP(&gitToken, "git-token", "", "", "if git repository is private we can use token as an auth parameter") 118 cmd.Flags().StringToStringVarP(&gitUsernameSecret, "git-username-secret", "", map[string]string{}, "git username secret in a form of secret_name1=secret_key1 for private repository") 119 cmd.Flags().StringToStringVarP(&gitTokenSecret, "git-token-secret", "", map[string]string{}, "git token secret in a form of secret_name1=secret_key1 for private repository") 120 cmd.Flags().StringVarP(&gitCertificateSecret, "git-certificate-secret", "", "", "if git repository is private we can use certificate as an auth parameter stored in a kubernetes secret name") 121 cmd.Flags().StringVarP(&gitWorkingDir, "git-working-dir", "", "", "if repository contains multiple directories with tests (like monorepo) and one starting directory we can set working directory parameter") 122 cmd.Flags().StringVarP(&gitAuthType, "git-auth-type", "", "basic", "auth type for git requests one of basic|header") 123 cmd.Flags().BoolVar(&update, "update", false, "update, if test source already exists") 124 125 return cmd 126 }