Skip to content

Commit

Permalink
fix: check for empty variables
Browse files Browse the repository at this point in the history
  • Loading branch information
M0Rf30 committed Oct 20, 2024
1 parent e19ef8d commit ff418a6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
57 changes: 51 additions & 6 deletions pkg/pkgbuild/pkgbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,31 +173,36 @@ func (pkgBuild *PKGBUILD) RenderSpec(script string) *template.Template {
// Validate checks that mandatory items are correctly provided by the PKGBUILD
// file.
func (pkgBuild *PKGBUILD) Validate() {
var isValid = true
var validationErrors []string

// Validate license
if !pkgBuild.validateLicense() {
isValid = false
validationErrors = append(validationErrors, "license")

utils.Logger.Error("invalid SPDX license identifier", utils.Logger.Args("pkgname", pkgBuild.PkgName))
utils.Logger.Error("invalid SPDX license identifier",
utils.Logger.Args("pkgname", pkgBuild.PkgName))
utils.Logger.Info("you can find valid SPDX license identifiers here",
utils.Logger.Args("🌐", "https://spdx.org/licenses/"))
}

// Check source and hash sums
if len(pkgBuild.SourceURI) != len(pkgBuild.HashSums) {
isValid = false
validationErrors = append(validationErrors, "source-hash mismatch")

utils.Logger.Error("number of sources and hashsums differs",
utils.Logger.Args("pkgname", pkgBuild.PkgName))
}

// Check for package() function
if pkgBuild.Package == "" {
isValid = false
validationErrors = append(validationErrors, "package function")

utils.Logger.Error("missing package() function",
utils.Logger.Args("pkgname", pkgBuild.PkgName))
}

if !isValid {
// Exit if there are validation errors
if len(validationErrors) > 0 {
os.Exit(1)
}
}
Expand Down Expand Up @@ -428,3 +433,43 @@ func (pkgBuild *PKGBUILD) processOptions() {
}
}
}

// ValidateMandatoryItems checks that mandatory items are correctly provided by the PKGBUILD
// file.
func (pkgBuild *PKGBUILD) ValidateMandatoryItems() {
var validationErrors []string

// Check mandatory variables
if pkgBuild.Package == "" {
validationErrors = append(validationErrors, "pkgname")

utils.Logger.Error("missing mandatory variable",
utils.Logger.Args("variable", "pkgname"))
}

if pkgBuild.PkgRel == "" {
validationErrors = append(validationErrors, "pkgrel")

utils.Logger.Error("missing mandatory assignment",
utils.Logger.Args("variable", "pkgrel"))
}

if pkgBuild.PkgVer == "" {
validationErrors = append(validationErrors, "pkgver")

utils.Logger.Error("missing mandatory variable",
utils.Logger.Args("variable", "pkgver"))
}

if pkgBuild.ArchComputed == "" {
validationErrors = append(validationErrors, "arch")

utils.Logger.Error("missing mandatory assignment",
utils.Logger.Args("array", "arch"))
}

// Exit if there are validation errors
if len(validationErrors) > 0 {
os.Exit(1)
}
}
1 change: 1 addition & 0 deletions pkg/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ func (mpc *MultipleProject) populateProjects(distro, release, path string) error

pkgbuildFile.Validate()
pkgbuildFile.ValidateArchitecture()
// pkgbuildFile.ValidateMandatoryItems()

Check failure on line 337 in pkg/project/project.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] pkg/project/project.go#L337

commentedOutCode: may want to remove commented-out code (gocritic)
Raw output
pkg/project/project.go:337:3: commentedOutCode: may want to remove commented-out code (gocritic)
		// pkgbuildFile.ValidateMandatoryItems()
		^

packageManager = packer.GetPackageManager(pkgbuildFile, distro)

Expand Down
18 changes: 16 additions & 2 deletions pkg/utils/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ func StringifyArray(node *syntax.Assign) []string {
printer := syntax.NewPrinter(syntax.Indent(2))
out := &strings.Builder{}

if len(node.Array.Elems) == 0 {
Logger.Fatal("empty array, please give it a value",
Logger.Args("array", node.Name.Value))
}

for index := range node.Array.Elems {
if err := printer.Print(out, node.Array.Elems[index].Value); err != nil {
Logger.Error("unable to parse array element",
Expand All @@ -36,9 +41,13 @@ func StringifyAssign(node *syntax.Assign) string {
out := &strings.Builder{}
printer := syntax.NewPrinter(syntax.Indent(2))

if node.Value == nil {
Logger.Fatal("empty variable, please give it a value",
Logger.Args("variable", node.Name.Value))
}

if err := printer.Print(out, node.Value); err != nil {
Logger.Error("unable to parse variable",
Logger.Args("name", out.String()))
return ""
}

return strings.Trim(out.String(), "\"")
Expand All @@ -60,6 +69,11 @@ func StringifyFuncDecl(node *syntax.FuncDecl) string {

funcDecl := strings.Trim(out.String(), "{\n}")

if funcDecl == "" {
Logger.Fatal("empty function, please give it a value",
Logger.Args("function", node.Name.Value))
}

return funcDecl
}

Expand Down

0 comments on commit ff418a6

Please sign in to comment.