Skip to content

Commit f8d3721

Browse files
ldmonsterCopilot
andauthored
[dmt] fix unknown license file (#346)
Signed-off-by: Pavel Okhlopkov <pavel.okhlopkov@flant.com> Signed-off-by: Pavel Okhlopkov <36456348+ldmonster@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 79302ad commit f8d3721

5 files changed

Lines changed: 84 additions & 114 deletions

File tree

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ linters:
6868
- all
6969
- '-ST1003' # waiting for package name will be fixed (underscores)
7070
- '-QF1008' # not need to fix; we understand how to call nested structs
71+
- '-SA1019' # remove when fix logger
7172
revive:
7273
rules:
7374
- name: var-naming # waiting for package name will be fixed (incorrect naming)

internal/logger/logger.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,25 @@ func InitLogger(w io.Writer, logLevel string) {
5252
log.SetOutput(io.Discard)
5353
}
5454

55+
// Deprecated: use slog directly instead of these wrapper functions
5556
func DebugF(format string, a ...any) {
5657
slog.Debug(
5758
fmt.Sprintf(format, a...))
5859
}
5960

61+
// Deprecated: use slog directly instead of these wrapper functions
6062
func InfoF(format string, a ...any) {
6163
slog.Info(
6264
fmt.Sprintf(format, a...))
6365
}
6466

67+
// Deprecated: use slog directly instead of these wrapper functions
6568
func WarnF(format string, a ...any) {
6669
slog.Warn(
6770
fmt.Sprintf(format, a...))
6871
}
6972

73+
// Deprecated: use slog directly instead of these wrapper functions
7074
func ErrorF(format string, a ...any) {
7175
slog.Error(
7276
fmt.Sprintf(format, a...))

pkg/linters/module/rules/license.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ limitations under the License.
1717
package rules
1818

1919
import (
20+
"errors"
2021
"os"
2122
"regexp"
2223

2324
"github.com/deckhouse/dmt/internal/fsutils"
2425
"github.com/deckhouse/dmt/internal/logger"
2526
"github.com/deckhouse/dmt/internal/module"
2627
"github.com/deckhouse/dmt/pkg"
27-
"github.com/deckhouse/dmt/pkg/errors"
28+
pkgerrors "github.com/deckhouse/dmt/pkg/errors"
2829
)
2930

3031
const (
@@ -59,7 +60,7 @@ type LicenseRule struct {
5960
pkg.PathRule
6061
}
6162

62-
func (r *LicenseRule) CheckFiles(mod *module.Module, errorList *errors.LintRuleErrorsList) {
63+
func (r *LicenseRule) CheckFiles(mod *module.Module, errorList *pkgerrors.LintRuleErrorsList) {
6364
errorList = errorList.WithRule(r.GetName())
6465

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

77-
licenseInfo, parseErr := parser.ParseFile(fileName)
78-
if parseErr != nil {
79-
errorList.WithFilePath(name).Error(parseErr.Error())
78+
_, parseErr := parser.ParseFile(fileName)
79+
if errors.Is(parseErr, ErrUnsupportedFileType) {
80+
logger.DebugF("Skipping unsupported file type: %s", name)
8081
continue
8182
}
8283

83-
// Handle parsed license info
84-
if !licenseInfo.Valid {
85-
errorList.WithFilePath(name).Error(licenseInfo.Error)
84+
if parseErr != nil {
85+
errorList.WithFilePath(name).Error(parseErr.Error())
86+
continue
8687
}
8788
}
8889
}

pkg/linters/module/rules/license_parser.go

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package rules
1818

1919
import (
2020
"bufio"
21+
"errors"
2122
"fmt"
2223
"os"
2324
"path/filepath"
@@ -49,10 +50,8 @@ type FileTypeConfig struct {
4950

5051
// LicenseInfo contains information about parsed license
5152
type LicenseInfo struct {
52-
Type string // "CE", "EE", or empty
53-
Year string // Extracted year
54-
Valid bool // Whether license is valid
55-
Error string // Error message if invalid
53+
Type string // "CE", "EE", or empty
54+
Year string // Extracted year
5655
}
5756

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

200+
var ErrUnsupportedFileType = fmt.Errorf("unsupported file type")
201+
201202
// ParseFile parses a file and extracts license information
202203
func (p *LicenseParser) ParseFile(filename string) (*LicenseInfo, error) {
203204
// Get file type configuration
204205
config := p.getFileConfig(filename)
205206
if config == nil {
206-
return &LicenseInfo{
207-
Valid: false,
208-
Error: fmt.Sprintf("unsupported file type: %s", filename),
209-
}, nil
207+
return nil, fmt.Errorf("%w: %s", ErrUnsupportedFileType, filename)
210208
}
211209

212210
// Read file header
@@ -215,35 +213,29 @@ func (p *LicenseParser) ParseFile(filename string) (*LicenseInfo, error) {
215213
if err != nil {
216214
return nil, fmt.Errorf("failed to read file: %w", err)
217215
}
216+
218217
// Check for generated file markers
219218
if isHeaderMarkedAsGenerated(header) {
220-
return &LicenseInfo{Valid: true}, nil
219+
return &LicenseInfo{}, nil
221220
}
222221

223222
// Try to extract license text from header
224223
licenseText := p.extractLicenseText(header, config)
225224
if licenseText == "" {
226-
return &LicenseInfo{
227-
Valid: false,
228-
Error: "no license header found",
229-
}, nil
225+
return nil, errors.New("no license header found")
230226
}
231227

232228
// Match against known licenses
233229
for _, license := range p.licenses {
234230
if matched, year := p.matchLicense(licenseText, license); matched {
235231
return &LicenseInfo{
236-
Type: license.Type,
237-
Year: year,
238-
Valid: true,
232+
Type: license.Type,
233+
Year: year,
239234
}, nil
240235
}
241236
}
242237

243-
return &LicenseInfo{
244-
Valid: false,
245-
Error: "license header does not match any known license",
246-
}, nil
238+
return nil, errors.New("license header does not match any known license")
247239
}
248240

249241
// getFileConfig returns the configuration for a given file

0 commit comments

Comments
 (0)