copyright

package
v1.20220411.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 11, 2022 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package copyright includes copyright header management utilities

Index

Constants

View Source
const (
	ExtensionUnknown = ""
	ExtensionCSS     = ".css"
	ExtensionGo      = ".go"
	ExtensionHTML    = ".html"
	ExtensionJS      = ".js"
	ExtensionJSX     = ".jsx"
	ExtensionPy      = ".py"
	ExtensionSASS    = ".sass"
	ExtensionSCSS    = ".scss"
	ExtensionTS      = ".ts"
	ExtensionTSX     = ".tsx"
	ExtensionYAML    = ".yaml"
	ExtensionYML     = ".yml"
	ExtensionSQL     = ".sql"
	ExtensionProto   = ".proto"
)

Extension

View Source
const DefaultCompany = "Blend Labs, Inc"

DefaultCompany is the default company to inject into the notice template.

View Source
const DefaultNoticeBodyTemplate = `Copyright (c) {{ .Year }} - Present. {{ .Company }}. All rights reserved
{{ .Restrictions }}`

DefaultNoticeBodyTemplate is the default notice body template.

View Source
const DefaultOpenSourceLicense = "MIT"

DefaultOpenSourceLicense is the default open source license.

View Source
const DefaultRestrictionsInternal = "Blend Confidential - Restricted"

DefaultRestrictionsInternal are the default copyright restrictions to inject into the notice template.

View Source
const DefaultRestrictionsOpenSource = `Use of this source code is governed by a {{ .License }} license that can be found in the LICENSE file.`

DefaultRestrictionsOpenSource are the default open source restrictions.

Variables

View Source
var (
	// KnownExtensions is a list of all the known extensions.
	KnownExtensions = []string{
		ExtensionCSS,
		ExtensionGo,
		ExtensionHTML,
		ExtensionJS,
		ExtensionJSX,
		ExtensionPy,
		ExtensionSCSS,
		ExtensionSASS,
		ExtensionTS,
		ExtensionTSX,
		ExtensionYAML,
		ExtensionYML,
		ExtensionSQL,
		ExtensionProto,
	}

	// DefaultExtensionNoticeTemplates is a mapping between file extension (including the prefix dot) to the notice templates.
	DefaultExtensionNoticeTemplates = map[string]string{
		ExtensionCSS:   cssNoticeTemplate,
		ExtensionGo:    goNoticeTemplate,
		ExtensionHTML:  htmlNoticeTemplate,
		ExtensionJS:    jsNoticeTemplate,
		ExtensionJSX:   jsNoticeTemplate,
		ExtensionPy:    pythonNoticeTemplate,
		ExtensionSASS:  sassNoticeTemplate,
		ExtensionSCSS:  scssNoticeTemplate,
		ExtensionTS:    tsNoticeTemplate,
		ExtensionTSX:   tsNoticeTemplate,
		ExtensionYAML:  yamlNoticeTemplate,
		ExtensionYML:   yamlNoticeTemplate,
		ExtensionSQL:   sqlNoticeTemplate,
		ExtensionProto: protoNoticeTemplate,
	}

	// DefaultExcludes is the default excluded directories.
	DefaultExcludes = []string{
		".git/*",
		".github/*",
		"*/_config",
		"*/_config/*",
		"*/dist/*",
		"*/node_modules/*",
		"*/testdata",
		"*/testdata/*",
		"*/vendor/*",
		"node_modules/*",
		"protogen/*",
		"*.pb.go",
		"vendor/*",
		"venv/*",
		"*/venv/*",
	}

	// DefaultIncludeFiles is the default included files list.
	DefaultIncludeFiles = []string{
		"*.css",
		"*.go",
		"*.html",
		"*.js",
		"*.jsx",
		"*.py",
		"*.sass",
		"*.scss",
		"*.ts",
		"*.tsx",
		"*.yaml",
		"*.yml",
		"*.sql",
		"*.proto",
	}
)
View Source
var (
	ErrWalkSkip = errors.New("walk skip; we should not process this file or path")
	ErrFailure  = errors.New("failure; one or more steps failed")
)

Error sentinels

View Source
var (
	VerifyErrorFormat = "%s: copyright header missing or invalid"
)

Error Strings

Functions

This section is empty.

Types

type Action

type Action func(path string, info os.FileInfo, file, notice []byte) error

Action is the action to run.

type Config

type Config struct {
	// NoticeBodyTemplate is the notice body template that will be processed and
	// injected to the relevant extension specific notice template.
	NoticeBodyTemplate string `yaml:"noticeBodyTemplate"`
	// Year is the year to insert into the templates as `{{ .Year }}`
	Year int `yaml:"year"`
	// Company is the company name to insert into the templates as `{{ .Company }}`
	Company string `yaml:"company"`
	// License is the open source license to insert into in templates as `{{ .License }}`
	License string `yaml:"openSourceLicense"`

	// Restrictions an optional template to clarify copyright restrictions or
	// visibility modifiers, which is available in the `NoticeBodyTemplate` as `{{ .Restrictions }}`
	Restrictions string `yaml:"restrictionTemplate"`

	// Excludes are a list of globs to exclude, they can
	// match both files and directories.
	// This can be populated with `.gitignore` and the like.
	Excludes []string `yaml:"excludes"`
	// IncludeFiles are a list of globs to match files to include.
	IncludeFiles []string `yaml:"includeFiles"`

	// ExtensionNoticeTemplates is a map between file extension (including dot prefix)
	// to the relevant full notice template for the file. It can include a template variable
	// {{ .Notice }} that will insert the compiled `NoticyBodyTemplate`.
	ExtensionNoticeTemplates map[string]string

	// FallbackNoticeTemplate is a full notice template that will be used if there is no extension
	// specific notice template.
	// It can include the template variable {{ .Notice }} that will instert the compiled `NoticeBodyTemplate`.
	FallbackNoticeTemplate string

	// ExitFirst indicates if we should return after the first failure.
	ExitFirst *bool `yaml:"exitFirst"`
	// Quiet controls whether output is suppressed.
	Quiet *bool `yaml:"quiet"`
	// Verbose controls whether verbose output is shown.
	Verbose *bool `yaml:"verbose"`
	// Debug controls whether debug output is shown.
	Debug *bool `yaml:"debug"`

	// ShowDiff shows shows the diffs on verification failues.
	ShowDiff *bool `yaml:"verifyDiff"`
}

Config holds the runtime configuration option for the copyright engine.

func (Config) CompanyOrDefault

func (c Config) CompanyOrDefault() string

CompanyOrDefault returns a company name or a default.

func (Config) DebugOrDefault

func (c Config) DebugOrDefault() bool

DebugOrDefault returns a value or a default.

func (Config) ExitFirstOrDefault

func (c Config) ExitFirstOrDefault() bool

ExitFirstOrDefault returns a value or a default.

func (Config) ExtensionNoticeTemplatesOrDefault added in v1.20210227.3

func (c Config) ExtensionNoticeTemplatesOrDefault() map[string]string

ExtensionNoticeTemplatesOrDefault returns mapping between file extensions (including dot) to the notice templates (i.e. how the template should be fully formatted per file type).

func (Config) LicenseOrDefault added in v1.20210205.5

func (c Config) LicenseOrDefault() string

LicenseOrDefault returns an open source license or a default.

func (Config) NoticeBodyTemplateOrDefault

func (c Config) NoticeBodyTemplateOrDefault() string

NoticeBodyTemplateOrDefault returns the notice body template or a default.

func (Config) QuietOrDefault added in v1.20210205.5

func (c Config) QuietOrDefault() bool

QuietOrDefault returns a value or a default.

func (Config) RestrictionsOrDefault added in v1.20210116.4

func (c Config) RestrictionsOrDefault() string

RestrictionsOrDefault returns restrictions or a default.

func (Config) ShowDiffOrDefault added in v1.20210603.3

func (c Config) ShowDiffOrDefault() bool

ShowDiffOrDefault returns a value or a default.

func (Config) VerboseOrDefault

func (c Config) VerboseOrDefault() bool

VerboseOrDefault returns a value or a default.

func (Config) YearOrDefault

func (c Config) YearOrDefault() int

YearOrDefault returns the current year or a default.

type Copyright struct {
	Config // Config holds the configuration opitons.

	// Stdout is the writer for Verbose and Debug output.
	// If it is unset, `os.Stdout` will be used.
	Stdout io.Writer
	// Stderr is the writer for Error output.
	// If it is unset, `os.Stderr` will be used.
	Stderr io.Writer
}

Copyright is the main type that injects, removes and verifies copyright headers.

func New

func New(options ...Option) *Copyright

New creates a new copyright engine with a given set of config options.

func (Copyright) Debugf

func (c Copyright) Debugf(format string, args ...interface{})

Debugf writes to stdout if the `Debug` flag is true.

func (Copyright) Errorf added in v1.20210722.4

func (c Copyright) Errorf(format string, args ...interface{})

Errorf writes to stderr.

func (Copyright) GetStderr

func (c Copyright) GetStderr() io.Writer

GetStderr returns standard error.

func (Copyright) GetStdout

func (c Copyright) GetStdout() io.Writer

GetStdout returns standard out.

func (Copyright) Inject

func (c Copyright) Inject(ctx context.Context, root string) error

Inject inserts the copyright header in any matching files that don't already have the copyright header.

func (Copyright) Remove

func (c Copyright) Remove(ctx context.Context, root string) error

Remove removes the copyright header in any matching files that have the copyright header.

func (Copyright) Verbosef

func (c Copyright) Verbosef(format string, args ...interface{})

Verbosef writes to stdout if the `Verbose` flag is true.

func (Copyright) Verify

func (c Copyright) Verify(ctx context.Context, root string) error

Verify asserts that the files found during walk have the copyright header.

func (Copyright) Walk

func (c Copyright) Walk(ctx context.Context, action Action, root string) error

Walk traverses the tree recursively from the root and applies the given action.

If the root is a file, it is handled singly and then walk will return.

type Option

type Option func(*Copyright)

Option is a function that modifies a config.

func OptCompany added in v1.20210116.4

func OptCompany(company string) Option

OptCompany sets the template company.

func OptConfig

func OptConfig(cfg Config) Option

OptConfig sets the config in its entirety.

func OptDebug

func OptDebug(debug bool) Option

OptDebug sets if we should show debug output.

func OptExcludes added in v1.20210708.4

func OptExcludes(excludeGlobs ...string) Option

OptExcludes sets the exclude glob filters.

func OptExitFirst

func OptExitFirst(exitFirst bool) Option

OptExitFirst sets if we should stop after the first failure.

func OptIncludeFiles

func OptIncludeFiles(includeGlobs ...string) Option

OptIncludeFiles sets the include file glob filters.

func OptLicense added in v1.20210205.5

func OptLicense(license string) Option

OptLicense sets the template license.

func OptNoticeBodyTemplate added in v1.20210116.4

func OptNoticeBodyTemplate(noticeBodyTemplate string) Option

OptNoticeBodyTemplate sets the notice body template.

func OptRestrictions added in v1.20210116.4

func OptRestrictions(restrictions string) Option

OptRestrictions sets the template restrictions.

func OptVerbose

func OptVerbose(verbose bool) Option

OptVerbose sets if we should show verbose output.

func OptYear added in v1.20210205.5

func OptYear(year int) Option

OptYear sets the template year.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL