-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathvalidation.go
More file actions
95 lines (82 loc) · 2.93 KB
/
validation.go
File metadata and controls
95 lines (82 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package cmd
import (
"fmt"
"os"
"path/filepath"
"strings"
"codacy/cli-v2/config"
"codacy/cli-v2/utils/logger"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
// validateCodacyYAML validates that codacy.yaml exists and is not empty
// Returns an error if validation fails, nil if validation passes
func validateCodacyYAML() error {
codacyYAMLPath := config.Config.ProjectConfigFile()
// Check if file exists
if _, err := os.Stat(codacyYAMLPath); os.IsNotExist(err) {
logger.Error("codacy.yaml file not found", logrus.Fields{
"file": codacyYAMLPath,
})
return fmt.Errorf("❌ Fatal: codacy.yaml file not found. Please run 'codacy-cli init' to initialize your configuration first")
} else if err != nil {
logger.Error("Error accessing codacy.yaml file", logrus.Fields{
"file": codacyYAMLPath,
"error": err,
})
return fmt.Errorf("error accessing %s: %w", codacyYAMLPath, err)
}
// Read file content
content, err := os.ReadFile(codacyYAMLPath)
if err != nil {
logger.Error("Failed to read codacy.yaml file", logrus.Fields{
"file": codacyYAMLPath,
"error": err,
})
return fmt.Errorf("error reading %s: %w", codacyYAMLPath, err)
}
// Check if file is empty or contains only whitespace
if len(strings.TrimSpace(string(content))) == 0 {
logger.Error("codacy.yaml file is empty", logrus.Fields{
"file": codacyYAMLPath,
})
return fmt.Errorf("❌ Fatal: %s is empty. Please run 'codacy-cli init' to initialize your configuration first", filepath.Base(codacyYAMLPath))
}
// Try to parse YAML to ensure it's valid
var configData map[string]interface{}
if err := yaml.Unmarshal(content, &configData); err != nil {
logger.Error("Failed to parse codacy.yaml file", logrus.Fields{
"file": codacyYAMLPath,
"error": err,
})
if strings.Contains(err.Error(), "cannot unmarshal") {
return fmt.Errorf("❌ Fatal: %s contains invalid configuration - run 'codacy-cli config reset' to fix: %v", filepath.Base(codacyYAMLPath), err)
}
return fmt.Errorf("❌ Fatal: %s is broken or has invalid YAML format - run 'codacy-cli config reset' to reinitialize your configuration", filepath.Base(codacyYAMLPath))
}
// Ensure configData is not nil after unmarshaling
if configData == nil {
logger.Error("codacy.yaml unmarshaled to nil data", logrus.Fields{
"file": codacyYAMLPath,
})
return fmt.Errorf("❌ Fatal: %s contains no valid configuration. Please run 'codacy-cli init' to initialize your configuration first", filepath.Base(codacyYAMLPath))
}
return nil
}
// shouldSkipValidation checks if the current command should skip codacy.yaml validation
func shouldSkipValidation(cmdName string) bool {
skipCommands := []string{
"init",
"help",
"version",
"reset", // config reset should work even with empty/invalid codacy.yaml
"codacy-cli", // root command when called without subcommands
"update",
}
for _, skipCmd := range skipCommands {
if cmdName == skipCmd {
return true
}
}
return false
}