github.com/fluxcd/go-git-providers@v0.19.3/gitprovider/options.go (about)

     1  /*
     2  Copyright 2020 The Flux CD contributors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package gitprovider
    18  
    19  import (
    20  	"github.com/fluxcd/go-git-providers/validation"
    21  )
    22  
    23  // MakeRepositoryCreateOptions returns a RepositoryCreateOptions based off the mutator functions
    24  // given to e.g. RepositoriesClient.Create(). The returned validation error may be ignored in the
    25  // case that the client allows e.g. other license templates than those that are common.
    26  // validation.ErrFieldEnumInvalid is returned if the license template doesn't match known values.
    27  func MakeRepositoryCreateOptions(opts ...RepositoryCreateOption) (RepositoryCreateOptions, error) {
    28  	o := &RepositoryCreateOptions{}
    29  	for _, opt := range opts {
    30  		opt.ApplyToRepositoryCreateOptions(o)
    31  	}
    32  	return *o, o.ValidateOptions()
    33  }
    34  
    35  // RepositoryReconcileOption is an interface for applying options to when reconciling repositories.
    36  type RepositoryReconcileOption interface {
    37  	// RepositoryCreateOption is embedded, as reconcile uses the create options.
    38  	RepositoryCreateOption
    39  }
    40  
    41  // RepositoryCreateOption is an interface for applying options to when creating repositories.
    42  type RepositoryCreateOption interface {
    43  	// ApplyToRepositoryCreateOptions should apply relevant options to the target.
    44  	ApplyToRepositoryCreateOptions(target *RepositoryCreateOptions)
    45  }
    46  
    47  // RepositoryCreateOptions specifies optional options when creating a repository.
    48  type RepositoryCreateOptions struct {
    49  	// AutoInit can be set to true in order to automatically initialize the Git repo with a
    50  	// README.md and optionally a license in the first commit.
    51  	// Default: nil (which means "false, don't create")
    52  	AutoInit *bool
    53  
    54  	// LicenseTemplate lets the user specify a license template to use when AutoInit is true.
    55  	// Default: nil.
    56  	// Available options: See the LicenseTemplate enum.
    57  	LicenseTemplate *LicenseTemplate
    58  }
    59  
    60  // ApplyToRepositoryCreateOptions applies the options defined in the options struct to the
    61  // target struct that is being completed.
    62  func (opts *RepositoryCreateOptions) ApplyToRepositoryCreateOptions(target *RepositoryCreateOptions) {
    63  	// Go through each field in opts, and apply it to target if set
    64  	if opts.AutoInit != nil {
    65  		target.AutoInit = opts.AutoInit
    66  	}
    67  	if opts.LicenseTemplate != nil {
    68  		target.LicenseTemplate = opts.LicenseTemplate
    69  	}
    70  }
    71  
    72  // ValidateOptions validates that the options are valid.
    73  func (opts *RepositoryCreateOptions) ValidateOptions() error {
    74  	errs := validation.New("RepositoryCreateOptions")
    75  	if opts.LicenseTemplate != nil {
    76  		errs.Append(ValidateLicenseTemplate(*opts.LicenseTemplate), *opts.LicenseTemplate, "LicenseTemplate")
    77  	}
    78  	return errs.Error()
    79  }
    80  
    81  // FilesGetOptions specifies optional options when fetcing files.
    82  type FilesGetOptions struct {
    83  	Recursive bool
    84  }
    85  
    86  // FilesGetOption is an interface for applying options when fetching/getting files
    87  type FilesGetOption interface {
    88  	ApplyFilesGetOptions(target *FilesGetOptions)
    89  }
    90  
    91  // ApplyFilesGetOptions applies target options onto the invoked opts
    92  func (opts *FilesGetOptions) ApplyFilesGetOptions(target *FilesGetOptions) {
    93  	// Go through each field in opts, and apply it to target if set
    94  	target.Recursive = opts.Recursive
    95  
    96  }