|
1 | 1 | package publish
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "errors" |
4 | 5 | "fmt"
|
| 6 | + "log/slog" |
5 | 7 | "os"
|
6 | 8 | "path/filepath"
|
| 9 | + |
| 10 | + "pault.ag/go/debian/deb" |
7 | 11 | )
|
8 | 12 |
|
9 | 13 | type CLI struct {
|
10 |
| - Dists string `arg:"" required:"" help:"Path to dists directory" type:"existingdir" env:"EZAPT_DISTS"` |
11 |
| - KeepVersions int `help:"Number of versions to keep" default:"2" env:"EZAPT_KEEP_VERSIONS"` |
12 |
| - Keyring string `required:"" help:"Path to GPG keyring" type:"existingfile" env:"EZAPT_KEYRING"` |
13 |
| - SignUser []string `help:"GPG user to sign with" env:"EZAPT_SIGN_USER" default:"37C84554E7E0A261E4F76E1ED26E6ED000654A3E,FBA2E162F2F44657B38F0309E5665F9BD5970C47"` |
| 14 | + Dists string `required:"" help:"Path to dists directory" type:"existingdir" env:"EZAPT_DISTS"` |
| 15 | + KeepVersions int `help:"Number of versions to keep" default:"2" env:"EZAPT_KEEP_VERSIONS"` |
| 16 | + Keyring string `required:"" help:"Path to GPG keyring" type:"existingfile" env:"EZAPT_KEYRING"` |
| 17 | + Add string `help:"Path to packages to add" type:"existingdir" env:"EZAPT_ADD"` |
14 | 18 | }
|
15 | 19 |
|
16 | 20 | func (c *CLI) Run() error {
|
| 21 | + if c.Add != "" { |
| 22 | + if err := c.add(); err != nil { |
| 23 | + return fmt.Errorf("add: %w", err) |
| 24 | + } |
| 25 | + } |
| 26 | + |
17 | 27 | pkgs, err := scanPackages(c.Dists)
|
18 | 28 | if err != nil {
|
19 | 29 | return fmt.Errorf("publish: %w", err)
|
@@ -50,3 +60,43 @@ func (c *CLI) Run() error {
|
50 | 60 |
|
51 | 61 | return nil
|
52 | 62 | }
|
| 63 | + |
| 64 | +func (c *CLI) add() error { |
| 65 | + return filepath.Walk(c.Add, func(path string, info os.FileInfo, err error) error { |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + if info.IsDir() { |
| 70 | + return nil |
| 71 | + } |
| 72 | + if filepath.Ext(path) != ".deb" { |
| 73 | + return nil |
| 74 | + } |
| 75 | + |
| 76 | + deb, cl, err := deb.LoadFile(path) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + defer cl() |
| 81 | + |
| 82 | + // If we have foo/syncthing/candidate/whatever.deb, grab the |
| 83 | + // syncthing/candidate part |
| 84 | + repoPath, err := filepath.Rel(c.Add, path) |
| 85 | + if err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + repoPath = filepath.Dir(repoPath) |
| 89 | + |
| 90 | + newPath := filepath.Join(c.Dists, repoPath, "binary-"+deb.Control.Architecture.String(), filepath.Base(path)) |
| 91 | + slog.Info("Adding package", "package", deb.Control.Package, "version", deb.Control.Version, "architecture", deb.Control.Architecture, "to", newPath) |
| 92 | + if err := os.MkdirAll(filepath.Dir(newPath), 0o700); err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + if err := os.Link(path, newPath); errors.Is(err, os.ErrExist) { |
| 96 | + // never mind |
| 97 | + } else if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + return nil |
| 101 | + }) |
| 102 | +} |
0 commit comments