Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ linters:
- all
- '-ST1003' # waiting for package name will be fixed (underscores)
- '-QF1008' # not need to fix; we understand how to call nested structs
- '-SA1019' # remove when fix logger
revive:
rules:
- name: var-naming # waiting for package name will be fixed (incorrect naming)
Expand Down
4 changes: 4 additions & 0 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,25 @@ func InitLogger(w io.Writer, logLevel string) {
log.SetOutput(io.Discard)
}

// Deprecated: use slog directly instead of these wrapper functions
func DebugF(format string, a ...any) {
slog.Debug(
fmt.Sprintf(format, a...))
}

// Deprecated: use slog directly instead of these wrapper functions
func InfoF(format string, a ...any) {
slog.Info(
fmt.Sprintf(format, a...))
}

// Deprecated: use slog directly instead of these wrapper functions
func WarnF(format string, a ...any) {
slog.Warn(
fmt.Sprintf(format, a...))
}

// Deprecated: use slog directly instead of these wrapper functions
func ErrorF(format string, a ...any) {
slog.Error(
fmt.Sprintf(format, a...))
Expand Down
17 changes: 9 additions & 8 deletions pkg/linters/module/rules/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ limitations under the License.
package rules

import (
"errors"
"os"
"regexp"

"github.com/deckhouse/dmt/internal/fsutils"
"github.com/deckhouse/dmt/internal/logger"
"github.com/deckhouse/dmt/internal/module"
"github.com/deckhouse/dmt/pkg"
"github.com/deckhouse/dmt/pkg/errors"
pkgerrors "github.com/deckhouse/dmt/pkg/errors"
)

const (
Expand Down Expand Up @@ -59,7 +60,7 @@ type LicenseRule struct {
pkg.PathRule
}

func (r *LicenseRule) CheckFiles(mod *module.Module, errorList *errors.LintRuleErrorsList) {
func (r *LicenseRule) CheckFiles(mod *module.Module, errorList *pkgerrors.LintRuleErrorsList) {
errorList = errorList.WithRule(r.GetName())

// Use new parser if available
Expand All @@ -74,15 +75,15 @@ func (r *LicenseRule) CheckFiles(mod *module.Module, errorList *errors.LintRuleE
continue
}

licenseInfo, parseErr := parser.ParseFile(fileName)
if parseErr != nil {
errorList.WithFilePath(name).Error(parseErr.Error())
_, parseErr := parser.ParseFile(fileName)
if errors.Is(parseErr, ErrUnsupportedFileType) {
logger.DebugF("Skipping unsupported file type: %s", name)
continue
}

// Handle parsed license info
if !licenseInfo.Valid {
errorList.WithFilePath(name).Error(licenseInfo.Error)
if parseErr != nil {
errorList.WithFilePath(name).Error(parseErr.Error())
continue
}
}
}
Expand Down
32 changes: 12 additions & 20 deletions pkg/linters/module/rules/license_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package rules

import (
"bufio"
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -49,10 +50,8 @@ type FileTypeConfig struct {

// LicenseInfo contains information about parsed license
type LicenseInfo struct {
Type string // "CE", "EE", or empty
Year string // Extracted year
Valid bool // Whether license is valid
Error string // Error message if invalid
Type string // "CE", "EE", or empty
Year string // Extracted year
}

// LicenseParser handles license parsing and validation
Expand Down Expand Up @@ -198,15 +197,14 @@ func initFileConfigs() map[string]FileTypeConfig {
return configs
}

var ErrUnsupportedFileType = fmt.Errorf("unsupported file type")

// ParseFile parses a file and extracts license information
func (p *LicenseParser) ParseFile(filename string) (*LicenseInfo, error) {
// Get file type configuration
config := p.getFileConfig(filename)
if config == nil {
return &LicenseInfo{
Valid: false,
Error: fmt.Sprintf("unsupported file type: %s", filename),
}, nil
return nil, fmt.Errorf("%w: %s", ErrUnsupportedFileType, filename)
}

// Read file header
Expand All @@ -215,35 +213,29 @@ func (p *LicenseParser) ParseFile(filename string) (*LicenseInfo, error) {
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
}

// Check for generated file markers
if isHeaderMarkedAsGenerated(header) {
return &LicenseInfo{Valid: true}, nil
return &LicenseInfo{}, nil
}

// Try to extract license text from header
licenseText := p.extractLicenseText(header, config)
if licenseText == "" {
return &LicenseInfo{
Valid: false,
Error: "no license header found",
}, nil
return nil, errors.New("no license header found")
}

// Match against known licenses
for _, license := range p.licenses {
if matched, year := p.matchLicense(licenseText, license); matched {
return &LicenseInfo{
Type: license.Type,
Year: year,
Valid: true,
Type: license.Type,
Year: year,
}, nil
}
}

return &LicenseInfo{
Valid: false,
Error: "license header does not match any known license",
}, nil
return nil, errors.New("license header does not match any known license")
}

// getFileConfig returns the configuration for a given file
Expand Down
Loading