-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
173 lines (143 loc) · 3.95 KB
/
main.go
File metadata and controls
173 lines (143 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"slices"
"strings"
"github.com/jonjohnsonjr/apkrane/internal/version"
"github.com/spf13/cobra"
"gitlab.alpinelinux.org/alpine/go/repository"
"golang.org/x/exp/maps"
)
func main() {
if err := cli().ExecuteContext(context.Background()); err != nil {
log.Fatal(err)
}
}
func cli() *cobra.Command {
cmd := &cobra.Command{
Use: "apkrane",
}
cmd.AddCommand(ls())
return cmd
}
func fetchIndex(ctx context.Context, u string, auth string) (io.ReadCloser, error) {
if u == "-" {
return os.Stdin, nil
}
scheme, _, ok := strings.Cut(u, "://")
if !ok || !strings.HasPrefix(scheme, "http") {
return os.Open(u)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
if err != nil {
return nil, err
}
if auth != "" && !strings.Contains(u, "packages.wolfi.dev") {
// The auth string comes in the format of basic:domain:user:password
// We only need the user and password. So split it out.
a := strings.Split(auth, ":")
if len(a) < 4 || a[0] != "basic" {
return nil, fmt.Errorf("auth string needs to be in the format of basic:domain:user:password")
}
req.SetBasicAuth(a[2], a[3])
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("GET %q: %w", u, err)
}
if resp.StatusCode >= 400 {
return nil, fmt.Errorf("GET %q: status %d", u, resp.StatusCode)
}
return resp.Body, nil
}
func ls() *cobra.Command {
var full bool
var latest bool
var j bool
var packageFilter string
var auth string
cmd := &cobra.Command{
Use: "ls",
Example: `apkrane ls https://packages.wolfi.dev/os/x86_64/APKINDEX.tar.gz`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
u := args[0]
if auth == "" {
// Allow using the HTTP_AUTH environment variable as well.
auth = os.Getenv("HTTP_AUTH")
}
dir := strings.TrimSuffix(u, "/APKINDEX.tar.gz")
in, err := fetchIndex(ctx, u, auth)
if err != nil {
return err
}
defer in.Close()
index, err := repository.IndexFromArchive(io.NopCloser(in))
if err != nil {
return fmt.Errorf("parsing %q: %w", u, err)
}
w := cmd.OutOrStdout()
enc := json.NewEncoder(w)
packages := index.Packages
// TODO: origin filter as well?
if packageFilter != "" {
packages = slices.DeleteFunc(packages, func(pkg *repository.Package) bool {
return pkg.Name != packageFilter
})
}
if latest {
// by package
highest := map[string]*repository.Package{}
for _, pkg := range packages {
got, err := version.Parse(pkg.Version)
if err != nil {
// TODO: We should really fail here.
log.Printf("parsing %q: %v", pkg.Filename(), err)
continue
}
have, ok := highest[pkg.Name]
if !ok {
highest[pkg.Name] = pkg
continue
}
// TODO: We re-parse this for no reason.
parsed, err := version.Parse(have.Version)
if err != nil {
return err
}
if version.Compare(*got, *parsed) > 0 {
highest[pkg.Name] = pkg
}
}
packages = maps.Values(highest)
}
for _, pkg := range packages {
p := fmt.Sprintf("%s-%s.apk", pkg.Name, pkg.Version)
u := fmt.Sprintf("%s/%s", dir, p)
if j {
if err := enc.Encode(pkg); err != nil {
return fmt.Errorf("encoding %s: %w", pkg.Name, err)
}
} else if full {
fmt.Fprintf(w, "%s\n", u)
} else {
fmt.Fprintf(w, "%s\n", p)
}
}
return nil
},
}
cmd.Flags().StringVarP(&packageFilter, "package", "P", "", "print only packages with the given name")
cmd.Flags().BoolVar(&latest, "latest", false, "print only the latest version of each package")
cmd.Flags().BoolVar(&full, "full", false, "print the full url or path")
cmd.Flags().BoolVar(&j, "json", false, "print each package as json")
cmd.Flags().StringVarP(&auth, "auth", "A", "", "the auth string (basic:domain:user:password) to use for the request")
return cmd
}