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 1fdd438
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pkg/packer/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func GetPackageManager(pkgBuild *pkgbuild.PKGBUILD, distro string) Packer {
PKGBUILD: pkgBuild,
}
default:
utils.Logger.Fatal("unknown unsupported linux target",
utils.Logger.Fatal("unsupported linux target",
utils.Logger.Args("distro", distro))
}

Expand Down
53 changes: 44 additions & 9 deletions pkg/pkgbuild/pkgbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,39 +173,44 @@ 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)
}
}

// ValidateArchitecture checks if the specified architecture is supported.
// ComputeArchitecture checks if the specified architecture is supported.
// If "any", sets to "any". Otherwise, checks if current architecture is supported.
// Logs error if not supported, then sets to current architecture if supported.
func (pkgBuild *PKGBUILD) ValidateArchitecture() {
func (pkgBuild *PKGBUILD) ComputeArchitecture() {
isSupported := utils.Contains(pkgBuild.Arch, "any")
if isSupported {
pkgBuild.ArchComputed = "any"
Expand All @@ -218,7 +223,9 @@ func (pkgBuild *PKGBUILD) ValidateArchitecture() {
isSupported = utils.Contains(pkgBuild.Arch, currentArch)
if !isSupported {
utils.Logger.Fatal("unsupported architecture",
utils.Logger.Args("pkgname", pkgBuild.PkgName))
utils.Logger.Args(
"pkgname", pkgBuild.PkgName,
"arch", strings.Join(pkgBuild.Arch, " ")))
}

pkgBuild.ArchComputed = currentArch
Expand Down Expand Up @@ -428,3 +435,31 @@ 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
mandatoryChecks := map[string]string{
"pkgname": pkgBuild.PkgName,
"pkgrel": pkgBuild.PkgRel,
"pkgver": pkgBuild.PkgVer,
}

for variable, value := range mandatoryChecks {
if value == "" {
validationErrors = append(validationErrors, variable)
}
}

// Exit if there are validation errors
if len(validationErrors) > 0 {
utils.Logger.Fatal(
"failed to set variables",
utils.Logger.Args(
"variables",
strings.Join(validationErrors, " ")))
}
}
3 changes: 2 additions & 1 deletion pkg/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,9 @@ func (mpc *MultipleProject) populateProjects(distro, release, path string) error
return err
}

pkgbuildFile.ComputeArchitecture()
pkgbuildFile.ValidateMandatoryItems()
pkgbuildFile.Validate()
pkgbuildFile.ValidateArchitecture()

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 1fdd438

Please sign in to comment.