go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/copyright/config.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     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  	// NoticeTemplate is the notice body template that will be processed and
    15  	// injected to the relevant extension specific notice template.
    16  	//
    17  	// If omitted, a default template is used.
    18  	NoticeTemplate string `yaml:"noticeBodyTemplate"`
    19  
    20  	// Year is the year to insert into the templates as `{{ .Year }}`
    21  	Year int `yaml:"year"`
    22  	// CopyrightHolder is the company name to insert into the templates as `{{ .CopyrightHolder }}`
    23  	CopyrightHolder string `yaml:"copyrightHolder"`
    24  	// License is the open source license to insert into in templates as `{{ .License }}`
    25  	License string `yaml:"openSourceLicense"`
    26  
    27  	// Restrictions an optional template to clarify copyright restrictions or
    28  	// visibility modifiers, which is available in the `NoticeBodyTemplate` as `{{ .Restrictions }}`
    29  	RestrictionsTemplate string `yaml:"restrictionTemplate"`
    30  
    31  	// Excludes are a list of globs to exclude, they can
    32  	// match both files and directories.
    33  	//
    34  	// This can be populated with `.gitignore`, and `node_modules` and the like.
    35  	Excludes []string `yaml:"excludes"`
    36  	// Includes are a list of globs to match files to include.
    37  	Includes []string `yaml:"includeFiles"`
    38  
    39  	// ExtensionInjectionTemplates is a map between file extension (including dot prefix)
    40  	// to the relevant full injection template for the file. It should include a template
    41  	// variable reference {{ .Notice }} that will insert the compiled `NoticeTemplate`.
    42  	ExtensionInjectionTemplates map[Extension]string
    43  
    44  	// If we should print the intended actions but not perform them.
    45  	DryRun bool `yaml:"dryRun"`
    46  	// ExitFirst indicates if we should return after the first failure.
    47  	ExitFirst bool `yaml:"exitFirst"`
    48  	// Quiet controls whether output is suppressed.
    49  	Quiet bool `yaml:"quiet"`
    50  	// Verbose controls whether verbose output is shown.
    51  	Verbose bool `yaml:"verbose"`
    52  	// Debug controls whether debug output is shown.
    53  	Debug bool `yaml:"debug"`
    54  	// ShowDiff shows shows the diffs on verification failues.
    55  	ShowDiff bool `yaml:"verifyDiff"`
    56  }
    57  
    58  // NoticeTemplateOrDefault returns the notice template or a default.
    59  func (c Config) NoticeTemplateOrDefault() string {
    60  	if c.NoticeTemplate != "" {
    61  		return c.NoticeTemplate
    62  	}
    63  	return DefaultNoticeTemplate
    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  // CopyrightHolderOrDefault returns a copyright holder or a default.
    75  func (c Config) CopyrightHolderOrDefault() string {
    76  	if c.CopyrightHolder != "" {
    77  		return c.CopyrightHolder
    78  	}
    79  	return DefaultCopyrightHolder
    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.RestrictionsTemplate != "" {
    93  		return c.RestrictionsTemplate
    94  	}
    95  	return DefaultRestrictionsTemplate
    96  }
    97  
    98  // ExtensionInjectionTemplatesOrDefault 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) ExtensionInjectionTemplatesOrDefault() map[Extension]string {
   101  	if c.ExtensionInjectionTemplates != nil {
   102  		return c.ExtensionInjectionTemplates
   103  	}
   104  	return DefaultExtensionInjectionTemplates
   105  }