goheader

package module
v1.0.0-rc.3 Latest Latest
Warning

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

Go to latest
Published: May 8, 2025 License: GPL-3.0 Imports: 14 Imported by: 12

README

go-header

ci

Go source code linter providing checks for license headers.

Features

Feature Status Details
✅ Copyright Headers ✔️ Supports all standard formats
✅ Parallel Processing ✔️ Processes files concurrently
✅ Comment Support ✔️ //, /* */, /* * */
✅ Go/Analysis ✔️ Native Go tooling integration
✅ Regex Customization ✔️ User-defined pattern matching
✅ Automatic Year Checks ✔️ Validates & updates copyright years
✅ Auto-Fix Files ✔️ In-place header corrections
⏳ Go/Template Support ❌ In development
⏳ Multi-License Support ❌ Planned

Installation

For installation you can simply use go install.

go install github.com/denis-tingaikin/go-header/cmd/go-header@latest

Configuration

To configuring .go-header.yml linter you simply need to fill the next fields:

Inline template:

---
template: # expects header template string.
vars: # expects valid key value paris where key is string, value is regexp.
  key1: value1 # const value just checks equality. Note `key1` should be used in template string as {{ .key1 }} or {{ .KEY1 }}.
  key2: value2(.*) # regexp value just checks regex match. The value should be a valid regexp pattern. Note `key2` should be used in template string as {{ .key2 }} or {{ .KEY2 }}.

Filebased template:

---
template-path: # expects header template path string.
vars: # expects `const` or `regexp` node with values where values is a map string to string.
  key1: value1 # const value just checks equality. Note `key1` should be used in template string as {{ key1 }} or {{ KEY1 }}.
  key2: value2(.*) # regexp value just checks regex match. The value should be a valid regexp pattern. Note `key2` should be used in template string as {{ key2 }} or {{ KEY2 }}.

Configuration (DEPRECATED)

To configuring .go-header.yml linter you simply need to fill the next fields:

---
template: # expects header template string.
template-path: # expects path to file with license header string. 
values: # expects `const` or `regexp` node with values where values is a map string to string.
  const:
    key1: value1 # const value just checks equality. Note `key1` should be used in template string as {{ key1 }} or {{ KEY1 }}.
  regexp:
    key2: value2 # regexp value just checks regex match. The value should be a valid regexp pattern. Note `key2` should be used in template string as {{ key2 }} or {{ KEY2 }}.

Where values also can be used recursively. Example:

values:
  const:
    key1: "value" 
  regexp:
    key2: "{{key1}} value1" # Reads as regex pattern "value value1"

Bult-in values

  • MOD-YEAR - Returns the year when the file was modified.
  • MOD-YEAR-RANGE - Returns a year-range where the range starts from the year when the file was modified.
  • YEAR - Expects current year. Example header value: 2020. Example of template using: {{YEAR}} or {{year}}.
  • YEAR-RANGE - Expects any valid year interval or current year. Example header value: 2020 or 2000-2020. Example of template using: {{year-range}} or {{YEAR-RANGE}}.

Execution

go-header linter expects file paths on input. If you want to run go-header only on diff files, then you can use this command:

go-header ./...

Setup example

Step 1

Create configuration file .go-header.yml in the root of project.

---
vars:
  DOMAIN: sales|product
  MY_COMPANY: {{ .DOMAIN }}.mycompany.com
template: |
  {{ .MY_COMPANY }}
  SPDX-License-Identifier: Apache-2.0

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at:

  	  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
Step 2

Run go-header ./...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAnalyzer

func NewAnalyzer(templ string, vars map[string]Value) *analysis.Analyzer

NewAnalyzer creates new analyzer based on template and goheader values

func NewAnalyzerFromConfig

func NewAnalyzerFromConfig(config *string) *analysis.Analyzer

NewAnalyzerFromConfig creates a new analysis.Analyzer from goheader config file

Types

type Analyzer

type Analyzer struct {
	// contains filtered or unexported fields
}

func New

func New(options ...Option) *Analyzer

func (*Analyzer) Analyze

func (a *Analyzer) Analyze(target *Target) (i Issue)

type Calculable

type Calculable interface {
	Calculate(map[string]Value) error
	Get() string
	Raw() string
}

type Config added in v1.0.0

type Config struct {
	// Values is map of values. Supports two types 'const` and `regexp`. Values can be used recursively.
	// DEPRECATED: Use Vars instead.
	Values map[string]map[string]string `yaml:"values"`
	// Template is template for checking. Uses values.
	Template string `yaml:"template"`
	// TemplatePath path to the template file. Useful if need to load the template from a specific file.
	TemplatePath string `yaml:"template-path"`
	// Vars is map of values. Values can be used recursively.
	Vars map[string]string `yaml:"vars"`
}

Config represents go-header linter setup parameters

func (*Config) GetTemplate added in v1.0.0

func (c *Config) GetTemplate() (string, error)

func (*Config) GetValues added in v1.0.0

func (c *Config) GetValues() (map[string]Value, error)

func (*Config) Parse

func (c *Config) Parse(p string) error

type ConstValue

type ConstValue struct {
	RawValue, Value string
}

func (*ConstValue) Calculate

func (c *ConstValue) Calculate(values map[string]Value) error

func (*ConstValue) Get

func (c *ConstValue) Get() string

func (*ConstValue) Raw added in v0.5.0

func (c *ConstValue) Raw() string

func (*ConstValue) Read

func (c *ConstValue) Read(s *Reader) Issue

func (*ConstValue) String added in v0.5.0

func (c *ConstValue) String() string

type Fix added in v0.5.0

type Fix struct {
	Actual   []string
	Expected []string
}

type Issue

type Issue interface {
	Location() Location
	Message() string
	Fix() *Fix
}

func NewIssue

func NewIssue(msg string) Issue

func NewIssueWithFix added in v0.5.0

func NewIssueWithFix(msg string, location Location, fix Fix) Issue

func NewIssueWithLocation

func NewIssueWithLocation(msg string, location Location) Issue

type Location

type Location struct {
	Line     int
	Position int
}

func (Location) Add

func (l Location) Add(other Location) Location

func (Location) String

func (l Location) String() string

type Option

type Option interface {
	// contains filtered or unexported methods
}

func WithTemplate

func WithTemplate(template string) Option

func WithValues

func WithValues(values map[string]Value) Option

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

func NewReader

func NewReader(text string) *Reader

func (*Reader) Done

func (r *Reader) Done() bool

func (*Reader) Finish

func (r *Reader) Finish() string

func (*Reader) Location

func (r *Reader) Location() Location

func (*Reader) Next

func (r *Reader) Next() rune

func (*Reader) Peek

func (r *Reader) Peek() rune

func (*Reader) Position

func (r *Reader) Position() int

func (*Reader) ReadWhile

func (r *Reader) ReadWhile(match func(rune) bool) string

func (*Reader) SetOffset

func (r *Reader) SetOffset(offset Location)

func (*Reader) SetPosition

func (r *Reader) SetPosition(pos int)

type RegexpValue

type RegexpValue struct {
	RawValue, Value string
}

func (*RegexpValue) Calculate

func (r *RegexpValue) Calculate(values map[string]Value) error

func (*RegexpValue) Get

func (r *RegexpValue) Get() string

func (*RegexpValue) Raw added in v0.5.0

func (r *RegexpValue) Raw() string

func (*RegexpValue) Read

func (r *RegexpValue) Read(s *Reader) Issue

func (*RegexpValue) String added in v0.5.0

func (r *RegexpValue) String() string

type Target

type Target struct {
	Path string
	File *ast.File
}

func (*Target) ModTime

func (t *Target) ModTime() (time.Time, error)

type Value

type Value interface {
	Calculable
	Read(*Reader) Issue
}

Directories

Path Synopsis
cmd
go-header command

Jump to

Keyboard shortcuts

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