github.com/kubeshop/testkube@v1.17.23/cmd/kubectl-testkube/commands/common/repository.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	"github.com/spf13/cobra"
     8  
     9  	"github.com/kubeshop/testkube/pkg/api/v1/testkube"
    10  	"github.com/kubeshop/testkube/pkg/ui"
    11  )
    12  
    13  func hasGitParamsInCmd(cmd *cobra.Command, crdOnly bool) bool {
    14  	var fields = []string{"git-uri", "git-branch", "git-commit", "git-path",
    15  		"git-username-secret", "git-token-secret", "git-working-dir", "git-certificate-secret", "git-auth-type"}
    16  	if !crdOnly {
    17  		fields = append(fields, "git-username", "git-token")
    18  	}
    19  
    20  	for _, field := range fields {
    21  		if cmd.Flag(field).Changed {
    22  			return true
    23  		}
    24  	}
    25  
    26  	return false
    27  }
    28  
    29  // NewRepositoryFromFlags creates repository from command flags
    30  func NewRepositoryFromFlags(cmd *cobra.Command) (repository *testkube.Repository, err error) {
    31  	crdOnly, err := strconv.ParseBool(cmd.Flag("crd-only").Value.String())
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	gitUri := cmd.Flag("git-uri").Value.String()
    37  	gitBranch := cmd.Flag("git-branch").Value.String()
    38  	gitCommit := cmd.Flag("git-commit").Value.String()
    39  	gitPath := cmd.Flag("git-path").Value.String()
    40  
    41  	var gitUsername, gitToken string
    42  	if !crdOnly {
    43  		client, _, err := GetClient(cmd)
    44  		ui.ExitOnError("getting client", err)
    45  
    46  		info, err := client.GetServerInfo()
    47  		ui.ExitOnError("getting server info", err)
    48  
    49  		if !info.DisableSecretCreation {
    50  			gitUsername = cmd.Flag("git-username").Value.String()
    51  			gitToken = cmd.Flag("git-token").Value.String()
    52  		}
    53  	}
    54  
    55  	gitUsernameSecret, err := cmd.Flags().GetStringToString("git-username-secret")
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	gitTokenSecret, err := cmd.Flags().GetStringToString("git-token-secret")
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  
    65  	gitWorkingDir := cmd.Flag("git-working-dir").Value.String()
    66  	gitCertificateSecret := cmd.Flag("git-certificate-secret").Value.String()
    67  	gitAuthType := cmd.Flag("git-auth-type").Value.String()
    68  
    69  	hasGitParams := hasGitParamsInCmd(cmd, crdOnly)
    70  	if !hasGitParams {
    71  		return nil, nil
    72  	}
    73  
    74  	repository = &testkube.Repository{
    75  		Type_:             "git",
    76  		Uri:               gitUri,
    77  		Branch:            gitBranch,
    78  		Commit:            gitCommit,
    79  		Path:              gitPath,
    80  		Username:          gitUsername,
    81  		Token:             gitToken,
    82  		CertificateSecret: gitCertificateSecret,
    83  		WorkingDir:        gitWorkingDir,
    84  		AuthType:          gitAuthType,
    85  	}
    86  
    87  	for key, val := range gitUsernameSecret {
    88  		repository.UsernameSecret = &testkube.SecretRef{
    89  			Name: key,
    90  			Key:  val,
    91  		}
    92  	}
    93  
    94  	for key, val := range gitTokenSecret {
    95  		repository.TokenSecret = &testkube.SecretRef{
    96  			Name: key,
    97  			Key:  val,
    98  		}
    99  	}
   100  
   101  	return repository, nil
   102  }
   103  
   104  // NewRepositoryUpdateFromFlags creates repository update from command flags
   105  func NewRepositoryUpdateFromFlags(cmd *cobra.Command) (repository *testkube.RepositoryUpdate, err error) {
   106  	repository = &testkube.RepositoryUpdate{}
   107  
   108  	var fields = []struct {
   109  		name        string
   110  		destination **string
   111  	}{
   112  		{
   113  			"git-uri",
   114  			&repository.Uri,
   115  		},
   116  		{
   117  			"git-branch",
   118  			&repository.Branch,
   119  		},
   120  		{
   121  			"git-commit",
   122  			&repository.Commit,
   123  		},
   124  		{
   125  			"git-path",
   126  			&repository.Path,
   127  		},
   128  		{
   129  			"git-working-dir",
   130  			&repository.WorkingDir,
   131  		},
   132  		{
   133  			"git-certificate-secret",
   134  			&repository.CertificateSecret,
   135  		},
   136  		{
   137  			"git-auth-type",
   138  			&repository.AuthType,
   139  		},
   140  	}
   141  
   142  	client, _, err := GetClient(cmd)
   143  	ui.ExitOnError("getting client", err)
   144  
   145  	info, err := client.GetServerInfo()
   146  	ui.ExitOnError("getting server info", err)
   147  
   148  	if !info.DisableSecretCreation {
   149  		fields = append(fields, []struct {
   150  			name        string
   151  			destination **string
   152  		}{
   153  			{
   154  				"git-username",
   155  				&repository.Username,
   156  			},
   157  			{
   158  				"git-token",
   159  				&repository.Token,
   160  			}}...)
   161  	}
   162  
   163  	var nonEmpty bool
   164  	for _, field := range fields {
   165  		if cmd.Flag(field.name).Changed {
   166  			value := cmd.Flag(field.name).Value.String()
   167  			*field.destination = &value
   168  			nonEmpty = true
   169  		}
   170  	}
   171  
   172  	var refs = []struct {
   173  		name        string
   174  		destination ***testkube.SecretRef
   175  	}{
   176  		{
   177  			"git-username-secret",
   178  			&repository.UsernameSecret,
   179  		},
   180  		{
   181  			"git-token-secret",
   182  			&repository.TokenSecret,
   183  		},
   184  	}
   185  
   186  	for _, ref := range refs {
   187  		if cmd.Flag(ref.name).Changed {
   188  			value, err := cmd.Flags().GetStringToString(ref.name)
   189  			if err != nil {
   190  				return nil, err
   191  			}
   192  
   193  			for key, val := range value {
   194  				secret := &testkube.SecretRef{
   195  					Name: key,
   196  					Key:  val,
   197  				}
   198  
   199  				*ref.destination = &secret
   200  				nonEmpty = true
   201  			}
   202  		}
   203  	}
   204  
   205  	if nonEmpty {
   206  		return repository, nil
   207  	}
   208  
   209  	return nil, nil
   210  }
   211  
   212  // ValidateUpsertOptions validates upsert options
   213  func ValidateUpsertOptions(cmd *cobra.Command, sourceName string) error {
   214  	crdOnly, err := strconv.ParseBool(cmd.Flag("crd-only").Value.String())
   215  	if err != nil {
   216  		return err
   217  	}
   218  
   219  	gitUri := cmd.Flag("git-uri").Value.String()
   220  	gitBranch := cmd.Flag("git-branch").Value.String()
   221  	gitCommit := cmd.Flag("git-commit").Value.String()
   222  
   223  	var gitUsername, gitToken string
   224  	if !crdOnly {
   225  		client, _, err := GetClient(cmd)
   226  		ui.ExitOnError("getting client", err)
   227  
   228  		info, err := client.GetServerInfo()
   229  		ui.ExitOnError("getting server info", err)
   230  
   231  		if !info.DisableSecretCreation {
   232  			gitUsername = cmd.Flag("git-username").Value.String()
   233  			gitToken = cmd.Flag("git-token").Value.String()
   234  		}
   235  	}
   236  
   237  	gitUsernameSecret, err := cmd.Flags().GetStringToString("git-username-secret")
   238  	if err != nil {
   239  		return err
   240  	}
   241  
   242  	gitTokenSecret, err := cmd.Flags().GetStringToString("git-token-secret")
   243  	if err != nil {
   244  		return err
   245  	}
   246  
   247  	gitAuthType := cmd.Flag("git-auth-type").Value.String()
   248  	file := cmd.Flag("file").Value.String()
   249  	uri := cmd.Flag("uri").Value.String()
   250  
   251  	hasGitParams := hasGitParamsInCmd(cmd, crdOnly)
   252  	if hasGitParams && uri != "" {
   253  		return fmt.Errorf("found git params and `--uri` flag, please use `--git-uri` for git based repo or `--uri` without git based params")
   254  	}
   255  
   256  	if hasGitParams && file != "" {
   257  		return fmt.Errorf("found git params and `--file` flag, please use `--git-uri` for git based repo or `--file` without git based params")
   258  	}
   259  
   260  	if file != "" && uri != "" {
   261  		return fmt.Errorf("please pass only one of `--file` and `--uri`")
   262  	}
   263  
   264  	if hasGitParams {
   265  		if gitUri == "" && sourceName == "" {
   266  			return fmt.Errorf("please pass valid `--git-uri` flag")
   267  		}
   268  		if gitBranch != "" && gitCommit != "" {
   269  			return fmt.Errorf("please pass only one of `--git-branch` or `--git-commit`")
   270  		}
   271  	}
   272  
   273  	if len(gitUsernameSecret) > 1 {
   274  		return fmt.Errorf("please pass only one secret reference for git username")
   275  	}
   276  
   277  	if len(gitTokenSecret) > 1 {
   278  		return fmt.Errorf("please pass only one secret reference for git token")
   279  	}
   280  
   281  	if gitAuthType != string(testkube.GitAuthTypeBasic) && gitAuthType != string(testkube.GitAuthTypeHeader) {
   282  		return fmt.Errorf("please pass one of `basic` or `header` for git auth type")
   283  	}
   284  
   285  	if (gitUsername != "" || gitToken != "") && (len(gitUsernameSecret) > 0 || len(gitTokenSecret) > 0) {
   286  		return fmt.Errorf("please pass only one auth method for git repository")
   287  	}
   288  
   289  	return nil
   290  }