-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
) * Pull dev upstream to staging. (#112) * Use awk to enclose filename in single quotes tar #99 * Add null field separator so filenames don't get broken up. * Move upload logs up in the action sequence so it captures data before it gets deleted. * Fix awk (#109) --------- Co-authored-by: sn-o-w <[email protected]> * Fix awk delimiter. Pull in fix by @sn-o-w in https://github.com/sn-o-w/cache-apt-pkgs-action/commit/d0ee83b497ac30023e51cd526c62e57b07501912 mentioned in issue #99 * Swap out Bash based APT query logic for Golang version. (#117) * First version of a Golang version of command handling in general. (#118) --------- Co-authored-by: sn-o-w <[email protected]>
- Loading branch information
Showing
23 changed files
with
727 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
name: Pull Request | ||
on: | ||
pull_request: | ||
types: [opened, synchronize] | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
integrate: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Setup Go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version-file: "go.mod" | ||
|
||
- name: Build and test | ||
run: | | ||
go build -v ./... | ||
go test -v ./... | ||
- name: Lint | ||
uses: golangci/golangci-lint-action@v3 | ||
with: | ||
version: v1.52.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
src/cmd/apt_query/apt_query* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Launch Package", | ||
"type": "go", | ||
"request": "launch", | ||
"mode": "auto", | ||
"program": "${fileDirname}", | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module awalsh128.com/cache-apt-pkgs-action | ||
|
||
go 1.20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"sort" | ||
"strings" | ||
|
||
"awalsh128.com/cache-apt-pkgs-action/src/internal/common" | ||
"awalsh128.com/cache-apt-pkgs-action/src/internal/exec" | ||
"awalsh128.com/cache-apt-pkgs-action/src/internal/logging" | ||
) | ||
|
||
type AptPackage struct { | ||
Name string | ||
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, " ") | ||
} | ||
|
||
// Gets the APT based packages as a sorted by name list (normalized). | ||
func getPackages(executor exec.Executor, names []string) AptPackages { | ||
prefixArgs := []string{"--quiet=0", "--no-all-versions", "show"} | ||
execution := executor.Exec("apt-cache", append(prefixArgs, names...)...) | ||
|
||
err := execution.Error() | ||
if err != nil { | ||
logging.Fatal(err) | ||
} | ||
|
||
pkgs := []AptPackage{} | ||
errorMessages := []string{} | ||
|
||
for _, paragraph := range strings.Split(execution.Stdout, "\n\n") { | ||
pkg := AptPackage{} | ||
for _, line := range strings.Split(paragraph, "\n") { | ||
if strings.HasPrefix(line, "Package: ") { | ||
pkg.Name = strings.TrimSpace(strings.SplitN(line, ":", 2)[1]) | ||
} else if strings.HasPrefix(line, "Version: ") { | ||
pkg.Version = strings.TrimSpace(strings.SplitN(line, ":", 2)[1]) | ||
} else if strings.HasPrefix(line, "N: Unable to locate package ") || strings.HasPrefix(line, "E: ") { | ||
if !common.ContainsString(errorMessages, line) { | ||
errorMessages = append(errorMessages, line) | ||
} | ||
} | ||
} | ||
if pkg.Name != "" { | ||
pkgs = append(pkgs, pkg) | ||
} | ||
} | ||
|
||
if len(errorMessages) > 0 { | ||
logging.Fatalf("Errors encountered in apt-cache output (see below):\n%s", strings.Join(errorMessages, "\n")) | ||
} | ||
|
||
sort.Slice(pkgs, func(i, j int) bool { | ||
return pkgs[i].Name < pkgs[j].Name | ||
}) | ||
|
||
return pkgs | ||
} | ||
|
||
func getExecutor(replayFilename string) exec.Executor { | ||
if len(replayFilename) == 0 { | ||
return &exec.BinExecutor{} | ||
} | ||
return exec.NewReplayExecutor(replayFilename) | ||
} | ||
|
||
func main() { | ||
debug := flag.Bool("debug", false, "Log diagnostic information to a file alongside the binary.") | ||
|
||
replayFilename := flag.String("replayfile", "", | ||
"Replay command output from a specified file rather than executing a binary."+ | ||
"The file should be in the same format as the log generated by the debug flag.") | ||
|
||
flag.Parse() | ||
unparsedFlags := flag.Args() | ||
|
||
logging.Init(os.Args[0]+".log", *debug) | ||
|
||
executor := getExecutor(*replayFilename) | ||
|
||
if len(unparsedFlags) < 2 { | ||
logging.Fatalf("Expected at least 2 non-flag arguments but found %d.", len(unparsedFlags)) | ||
return | ||
} | ||
command := unparsedFlags[0] | ||
pkgNames := unparsedFlags[1:] | ||
|
||
switch command { | ||
|
||
case "normalized-list": | ||
pkgs := getPackages(executor, pkgNames) | ||
fmt.Println(pkgs.serialize()) | ||
|
||
default: | ||
logging.Fatalf("Command '%s' not recognized.", command) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"testing" | ||
|
||
"awalsh128.com/cache-apt-pkgs-action/src/internal/cmdtesting" | ||
) | ||
|
||
var createReplayLogs bool = false | ||
|
||
func init() { | ||
flag.BoolVar(&createReplayLogs, "createreplaylogs", false, "Execute the test commands, save the command output for future replay and skip the tests themselves.") | ||
} | ||
|
||
func TestMain(m *testing.M) { | ||
cmdtesting.TestMain(m) | ||
} | ||
|
||
func TestNormalizedList_MultiplePackagesExists_StdoutsAlphaSortedPackageNameVersionPairs(t *testing.T) { | ||
result := cmdtesting.New(t, createReplayLogs).Run("normalized-list", "xdot", "rolldice") | ||
result.ExpectSuccessfulOut("rolldice=1.16-1build1 xdot=1.2-3") | ||
} | ||
|
||
func TestNormalizedList_SamePackagesDifferentOrder_StdoutsMatch(t *testing.T) { | ||
expected := "rolldice=1.16-1build1 xdot=1.2-3" | ||
|
||
ct := cmdtesting.New(t, createReplayLogs) | ||
|
||
result := ct.Run("normalized-list", "rolldice", "xdot") | ||
result.ExpectSuccessfulOut(expected) | ||
|
||
result = ct.Run("normalized-list", "xdot", "rolldice") | ||
result.ExpectSuccessfulOut(expected) | ||
} | ||
|
||
func TestNormalizedList_MultiVersionWarning_StdoutSingleVersion(t *testing.T) { | ||
var result = cmdtesting.New(t, createReplayLogs).Run("normalized-list", "libosmesa6-dev", "libgl1-mesa-dev") | ||
result.ExpectSuccessfulOut("libgl1-mesa-dev=23.0.4-0ubuntu1~23.04.1 libosmesa6-dev=23.0.4-0ubuntu1~23.04.1") | ||
} | ||
|
||
func TestNormalizedList_SinglePackageExists_StdoutsSinglePackageNameVersionPair(t *testing.T) { | ||
var result = cmdtesting.New(t, createReplayLogs).Run("normalized-list", "xdot") | ||
result.ExpectSuccessfulOut("xdot=1.2-3") | ||
} | ||
|
||
func TestNormalizedList_VersionContainsColon_StdoutsEntireVersion(t *testing.T) { | ||
var result = cmdtesting.New(t, createReplayLogs).Run("normalized-list", "default-jre") | ||
result.ExpectSuccessfulOut("default-jre=2:1.17-74") | ||
} | ||
|
||
func TestNormalizedList_NonExistentPackageName_StderrsAptCacheErrors(t *testing.T) { | ||
var result = cmdtesting.New(t, createReplayLogs).Run("normalized-list", "nonexistentpackagename") | ||
result.ExpectError( | ||
`Error encountered running apt-cache --quiet=0 --no-all-versions show nonexistentpackagename | ||
Exited with status code 100; see combined output below: | ||
N: Unable to locate package nonexistentpackagename | ||
N: Unable to locate package nonexistentpackagename | ||
E: No packages found`) | ||
} | ||
|
||
func TestNormalizedList_NoPackagesGiven_StderrsArgMismatch(t *testing.T) { | ||
var result = cmdtesting.New(t, createReplayLogs).Run("normalized-list") | ||
result.ExpectError("Expected at least 2 non-flag arguments but found 1.") | ||
} |
Oops, something went wrong.