chore: apply some linter recommendations.

This commit is contained in:
Alexandre Pujol 2025-04-04 23:45:24 +02:00
parent 3b6b50cf63
commit 984cf28e61
No known key found for this signature in database
GPG key ID: C5469996F0DF68EC
37 changed files with 125 additions and 126 deletions

View file

@ -23,7 +23,7 @@ var (
regDirective = regexp.MustCompile(`(?m).*` + Keyword + `([a-z]*)( .*)?`)
)
// Main directive interface
// Directive main interface
type Directive interface {
prebuild.BaseInterface
Apply(opt *Option, profile string) (string, error)
@ -39,7 +39,7 @@ func Usage() string {
return res
}
// Directive options
// Option for the directive
type Option struct {
Name string
ArgMap map[string]string
@ -83,7 +83,7 @@ func (o *Option) cleanKeyword(input string) string {
return reg.ReplaceAllString(input, "")
}
// Check if the directive is inline or if it is a paragraph
// IsInline checks if either the directive is in one line or if it is a paragraph
func (o *Option) IsInline() bool {
inline := true
tmp := strings.Split(o.Raw, Keyword)
@ -106,7 +106,7 @@ func Run(file *paths.Path, profile string) (string, error) {
opt := NewOption(file, match)
drtv, ok := Directives[opt.Name]
if !ok {
return "", fmt.Errorf("Unknown directive '%s' in %s", opt.Name, opt.File)
return "", fmt.Errorf("unknown directive '%s' in %s", opt.Name, opt.File)
}
profile, err = drtv.Apply(opt, profile)
if err != nil {

View file

@ -61,32 +61,32 @@ func (d Dbus) Apply(opt *Option, profile string) (string, error) {
generatedDbus := r.String()
lenDbus := len(generatedDbus)
generatedDbus = generatedDbus[:lenDbus-1]
profile = strings.Replace(profile, opt.Raw, generatedDbus, -1)
profile = strings.ReplaceAll(profile, opt.Raw, generatedDbus)
return profile, nil
}
func (d Dbus) sanityCheck(opt *Option) (string, error) {
if len(opt.ArgList) < 1 {
return "", fmt.Errorf("Unknown dbus action: %s in %s", opt.Name, opt.File)
return "", fmt.Errorf("unknown dbus action: %s in %s", opt.Name, opt.File)
}
action := opt.ArgList[0]
if action != "own" && action != "talk" && action != "common" {
return "", fmt.Errorf("Unknown dbus action: %s in %s", opt.Name, opt.File)
return "", fmt.Errorf("unknown dbus action: %s in %s", opt.Name, opt.File)
}
if _, present := opt.ArgMap["name"]; !present {
return "", fmt.Errorf("Missing name for 'dbus: %s' in %s", action, opt.File)
return "", fmt.Errorf("missing name for 'dbus: %s' in %s", action, opt.File)
}
if _, present := opt.ArgMap["bus"]; !present {
return "", fmt.Errorf("Missing bus for '%s' in %s", opt.ArgMap["name"], opt.File)
return "", fmt.Errorf("missing bus for '%s' in %s", opt.ArgMap["name"], opt.File)
}
if _, present := opt.ArgMap["label"]; !present && action == "talk" {
return "", fmt.Errorf("Missing label for '%s' in %s", opt.ArgMap["name"], opt.File)
return "", fmt.Errorf("missing label for '%s' in %s", opt.ArgMap["name"], opt.File)
}
// Set default values
if _, present := opt.ArgMap["path"]; !present {
opt.ArgMap["path"] = "/" + strings.Replace(opt.ArgMap["name"], ".", "/", -1) + "{,/**}"
opt.ArgMap["path"] = "/" + strings.ReplaceAll(opt.ArgMap["name"], ".", "/") + "{,/**}"
}
opt.ArgMap["name"] += "{,.*}"
return action, nil

View file

@ -31,7 +31,7 @@ func init() {
func (d Exec) Apply(opt *Option, profileRaw string) (string, error) {
if len(opt.ArgList) == 0 {
return "", fmt.Errorf("No profile to exec")
return "", fmt.Errorf("no profile to exec")
}
transition := "Px"
transitions := []string{"P", "U", "p", "u", "PU", "pu"}
@ -70,5 +70,5 @@ func (d Exec) Apply(opt *Option, profileRaw string) (string, error) {
rules = rules.Sort()
new := rules.String()
new = new[:len(new)-1]
return strings.Replace(profileRaw, opt.Raw, new, -1), nil
return strings.ReplaceAll(profileRaw, opt.Raw, new), nil
}

View file

@ -59,7 +59,7 @@ func filter(only bool, opt *Option, profile string) (string, error) {
}
if opt.IsInline() {
profile = strings.Replace(profile, opt.Raw, "", -1)
profile = strings.ReplaceAll(profile, opt.Raw, "")
} else {
regRemoveParagraph := regexp.MustCompile(`(?s)` + opt.Raw + `\n.*?\n\n`)
profile = regRemoveParagraph.ReplaceAllString(profile, "")

View file

@ -40,7 +40,7 @@ func init() {
func (s Stack) Apply(opt *Option, profile string) (string, error) {
if len(opt.ArgList) == 0 {
return "", fmt.Errorf("No profile to stack")
return "", fmt.Errorf("no profile to stack")
}
t := opt.ArgList[0]
if t != "X" {
@ -58,7 +58,7 @@ func (s Stack) Apply(opt *Option, profile string) (string, error) {
stackedProfile := prebuild.RootApparmord.Join(name).MustReadFileAsString()
m := regRules.FindStringSubmatch(stackedProfile)
if len(m) < 2 {
return "", fmt.Errorf("No profile found in %s", name)
return "", fmt.Errorf("no profile found in %s", name)
}
stackedRules := m[1]
stackedRules = regCleanStakedRules.Replace(stackedRules)
@ -68,9 +68,9 @@ func (s Stack) Apply(opt *Option, profile string) (string, error) {
// Insert the stacked profile at the end of the current profile, remove the stack directive
m := regEndOfRules.FindStringSubmatch(profile)
if len(m) <= 1 {
return "", fmt.Errorf("No end of rules found in %s", opt.File)
return "", fmt.Errorf("no end of rules found in %s", opt.File)
}
profile = strings.Replace(profile, m[0], res+m[0], -1)
profile = strings.Replace(profile, opt.Raw, "", -1)
profile = strings.ReplaceAll(profile, m[0], res+m[0])
profile = strings.ReplaceAll(profile, opt.Raw, "")
return profile, nil
}