diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index 004b05256f09..0ed7e9d01188 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -478,9 +478,10 @@ type GofumptSettings struct { } type GoHeaderSettings struct { - Values map[string]map[string]string `mapstructure:"values"` - Template string `mapstructure:"template"` - TemplatePath string `mapstructure:"template-path"` + Values map[string]map[string]string `mapstructure:"values"` + Template string `mapstructure:"template"` + TemplatePath string `mapstructure:"template-path"` + LintConfigDir string `mapstructure:"lint-config-dir"` } type GoImportsSettings struct { diff --git a/pkg/golinters/goheader.go b/pkg/golinters/goheader.go index d3cfefa90b28..648a001b6ec8 100644 --- a/pkg/golinters/goheader.go +++ b/pkg/golinters/goheader.go @@ -2,6 +2,7 @@ package golinters import ( "go/token" + "path/filepath" "sync" goheader "github.com/denis-tingaikin/go-header" @@ -21,10 +22,14 @@ func NewGoHeader(settings *config.GoHeaderSettings) *goanalysis.Linter { conf := &goheader.Configuration{} if settings != nil { + path := settings.TemplatePath + if path != "" && !filepath.IsAbs(path) { + path = filepath.Join(settings.LintConfigDir, path) + } conf = &goheader.Configuration{ Values: settings.Values, Template: settings.Template, - TemplatePath: settings.TemplatePath, + TemplatePath: path, } } diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 1247f07934f2..571a44e39b14 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -247,6 +247,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { if stylecheckCfg != nil && stylecheckCfg.GoVersion != "" { stylecheckCfg.GoVersion = trimGoVersion(m.cfg.Run.Go) } + + if goheaderCfg != nil { + goheaderCfg.LintConfigDir = m.cfg.GetConfigDir() + } } const megacheckName = "megacheck" diff --git a/test/testdata/configs/go-header-template b/test/testdata/configs/go-header-template new file mode 100644 index 000000000000..7a08ae949871 --- /dev/null +++ b/test/testdata/configs/go-header-template @@ -0,0 +1 @@ +MY {{title}} \ No newline at end of file diff --git a/test/testdata/configs/go-header-template.yml b/test/testdata/configs/go-header-template.yml new file mode 100644 index 000000000000..857fb96402aa --- /dev/null +++ b/test/testdata/configs/go-header-template.yml @@ -0,0 +1,6 @@ +linters-settings: + goheader: + template-path: go-header-template + values: + const: + title: TITLE. diff --git a/test/testdata/configs/go-header.yml b/test/testdata/configs/go-header.yml index c20ed76c196f..51d51b337411 100644 --- a/test/testdata/configs/go-header.yml +++ b/test/testdata/configs/go-header.yml @@ -1,6 +1,6 @@ linters-settings: goheader: - template: MY {{title}} + template: "MY {{title}}" values: const: title: TITLE. diff --git a/test/testdata/go-header-template_good.go b/test/testdata/go-header-template_good.go new file mode 100644 index 000000000000..33bfed45380b --- /dev/null +++ b/test/testdata/go-header-template_good.go @@ -0,0 +1,6 @@ +/*MY TITLE.*/ + +//golangcitest:args -Egoheader +//golangcitest:config_path testdata/configs/go-header-template.yml +//golangcitest:expected_exitcode 0 +package testdata diff --git a/test/testdata/go-header_bad.go b/test/testdata/go-header_bad.go index fbcf828e360e..cdb7a19a6db5 100644 --- a/test/testdata/go-header_bad.go +++ b/test/testdata/go-header_bad.go @@ -1,4 +1,4 @@ -/*MY TITLE!*/ // want `Expected:TITLE\., Actual: TITLE!` +/*MY TITLE?*/ // want `Expected:TITLE\., Actual: TITLE?` //golangcitest:args -Egoheader //golangcitest:config_path testdata/configs/go-header.yml