github.com/blend/go-sdk@v1.20220411.3/copyright/config.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package copyright
     9  
    10  import "time"
    11  
    12  // Config holds the runtime configuration option for the copyright engine.
    13  type Config struct {
    14  	// NoticeBodyTemplate is the notice body template that will be processed and
    15  	// injected to the relevant extension specific notice template.
    16  	NoticeBodyTemplate string `yaml:"noticeBodyTemplate"`
    17  	// Year is the year to insert into the templates as `{{ .Year }}`
    18  	Year int `yaml:"year"`
    19  	// Company is the company name to insert into the templates as `{{ .Company }}`
    20  	Company string `yaml:"company"`
    21  	// License is the open source license to insert into in templates as `{{ .License }}`
    22  	License string `yaml:"openSourceLicense"`
    23  
    24  	// Restrictions an optional template to clarify copyright restrictions or
    25  	// visibility modifiers, which is available in the `NoticeBodyTemplate` as `{{ .Restrictions }}`
    26  	Restrictions string `yaml:"restrictionTemplate"`
    27  
    28  	// Excludes are a list of globs to exclude, they can
    29  	// match both files and directories.
    30  	// This can be populated with `.gitignore` and the like.
    31  	Excludes []string `yaml:"excludes"`
    32  	// IncludeFiles are a list of globs to match files to include.
    33  	IncludeFiles []string `yaml:"includeFiles"`
    34  
    35  	// ExtensionNoticeTemplates is a map between file extension (including dot prefix)
    36  	// to the relevant full notice template for the file. It can include a template variable
    37  	// {{ .Notice }} that will insert the compiled `NoticyBodyTemplate`.
    38  	ExtensionNoticeTemplates map[string]string
    39  
    40  	// FallbackNoticeTemplate is a full notice template that will be used if there is no extension
    41  	// specific notice template.
    42  	// It can include the template variable {{ .Notice }} that will instert the compiled `NoticeBodyTemplate`.
    43  	FallbackNoticeTemplate string
    44  
    45  	// ExitFirst indicates if we should return after the first failure.
    46  	ExitFirst *bool `yaml:"exitFirst"`
    47  	// Quiet controls whether output is suppressed.
    48  	Quiet *bool `yaml:"quiet"`
    49  	// Verbose controls whether verbose output is shown.
    50  	Verbose *bool `yaml:"verbose"`
    51  	// Debug controls whether debug output is shown.
    52  	Debug *bool `yaml:"debug"`
    53  
    54  	// ShowDiff shows shows the diffs on verification failues.
    55  	ShowDiff *bool `yaml:"verifyDiff"`
    56  }
    57  
    58  // NoticeBodyTemplateOrDefault returns the notice body template or a default.
    59  func (c Config) NoticeBodyTemplateOrDefault() string {
    60  	if c.NoticeBodyTemplate != "" {
    61  		return c.NoticeBodyTemplate
    62  	}
    63  	return DefaultNoticeBodyTemplate
    64  }
    65  
    66  // YearOrDefault returns the current year or a default.
    67  func (c Config) YearOrDefault() int {
    68  	if c.Year > 0 {
    69  		return c.Year
    70  	}
    71  	return time.Now().UTC().Year()
    72  }
    73  
    74  // CompanyOrDefault returns a company name or a default.
    75  func (c Config) CompanyOrDefault() string {
    76  	if c.Company != "" {
    77  		return c.Company
    78  	}
    79  	return DefaultCompany
    80  }
    81  
    82  // LicenseOrDefault returns an open source license or a default.
    83  func (c Config) LicenseOrDefault() string {
    84  	if c.License != "" {
    85  		return c.License
    86  	}
    87  	return DefaultOpenSourceLicense
    88  }
    89  
    90  // RestrictionsOrDefault returns restrictions or a default.
    91  func (c Config) RestrictionsOrDefault() string {
    92  	if c.Restrictions != "" {
    93  		return c.Restrictions
    94  	}
    95  	return DefaultRestrictionsInternal
    96  }
    97  
    98  // ExtensionNoticeTemplatesOrDefault returns mapping between file extensions (including dot) to
    99  // the notice templates (i.e. how the template should be fully formatted per file type).
   100  func (c Config) ExtensionNoticeTemplatesOrDefault() map[string]string {
   101  	if c.ExtensionNoticeTemplates != nil {
   102  		return c.ExtensionNoticeTemplates
   103  	}
   104  	return DefaultExtensionNoticeTemplates
   105  }
   106  
   107  // ExitFirstOrDefault returns a value or a default.
   108  func (c Config) ExitFirstOrDefault() bool {
   109  	if c.ExitFirst != nil {
   110  		return *c.ExitFirst
   111  	}
   112  	return false
   113  }
   114  
   115  // QuietOrDefault returns a value or a default.
   116  func (c Config) QuietOrDefault() bool {
   117  	if c.Quiet != nil {
   118  		return *c.Quiet
   119  	}
   120  	return false
   121  }
   122  
   123  // VerboseOrDefault returns a value or a default.
   124  func (c Config) VerboseOrDefault() bool {
   125  	if c.Verbose != nil {
   126  		return *c.Verbose
   127  	}
   128  	return false
   129  }
   130  
   131  // DebugOrDefault returns a value or a default.
   132  func (c Config) DebugOrDefault() bool {
   133  	if c.Debug != nil {
   134  		return *c.Debug
   135  	}
   136  	return false
   137  }
   138  
   139  // ShowDiffOrDefault returns a value or a default.
   140  func (c Config) ShowDiffOrDefault() bool {
   141  	if c.ShowDiff != nil {
   142  		return *c.ShowDiff
   143  	}
   144  	return true
   145  }