Skip to content

Commit

Permalink
Switch package name back so it builds.
Browse files Browse the repository at this point in the history
  • Loading branch information
awalsh128 committed Nov 27, 2023
1 parent cc6a957 commit 6154ec6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
37 changes: 20 additions & 17 deletions apt_query.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package apt_query
package main

import (
"fmt"
Expand Down Expand Up @@ -29,6 +29,16 @@ type AptPackage struct {
Version string
}

type AptPackages []AptPackage

func (ps *AptPackages) serialize() string {
tokens := []string{}
for _, p := range ps {
tokens = append(tokens, p.Name+"="+p.Version)
}
return strings.Join(tokens, " ")
}

// Executes a command and either returns the output or exits the programs and writes the output (including error) to STDERR.
func execCommand(name string, arg ...string) string {
cmd := exec.Command(name, arg...)
Expand All @@ -41,11 +51,11 @@ func execCommand(name string, arg ...string) string {
}

// Gets the APT based packages as a sorted by name list (normalized).
func getPackages(names []string) []AptPackage {
func getPackages(names []string) AptPackages {
prefixArgs := []string{"--quiet=0", "--no-all-versions", "show"}
out := execCommand("apt-cache", append(prefixArgs, names...)...)

packages := []AptPackage{}
pkgs := []AptPackage{}
errorMessages := []string{}

for _, paragraph := range strings.Split(string(out), "\n\n") {
Expand All @@ -62,27 +72,19 @@ func getPackages(names []string) []AptPackage {
}
}
if pkg.Name != "" {
packages = append(packages, pkg)
pkgs = append(pkgs, pkg)
}
}

if len(errorMessages) > 0 {
exitOnError("Errors encountered in apt-cache output (see below):\n%s", strings.Join(errorMessages, "\n"))
}

sort.Slice(packages, func(i, j int) bool {
return packages[i].Name < packages[j].Name
sort.Slice(pkgs, func(i, j int) bool {
return pkgs[i].Name < pkgs[j].Name
})

return packages
}

func serialize(packages []AptPackage) string {
tokens := []string{}
for _, pkg := range packages {
tokens = append(tokens, pkg.Name+"="+pkg.Version)
}
return strings.Join(tokens, " ")
return pkgs
}

func main() {
Expand All @@ -92,11 +94,12 @@ func main() {
}

command := os.Args[1]
packageNames := os.Args[2:]
pkgNames := os.Args[2:]

switch command {
case "normalized-list":
fmt.Println(serialize(getPackages(packageNames)))
pkgs := getPackages(pkgNames)
fmt.Println(pkgs.serialize())
break
default:
exitOnError("Command '%s' not recognized.", command)
Expand Down
2 changes: 1 addition & 1 deletion apt_query_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package apt_query
package main

import (
"bytes"
Expand Down

0 comments on commit 6154ec6

Please sign in to comment.