|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "fmt" |
5 | 6 |
|
6 | 7 | "github.com/github/git-bundle-server/internal/argparse" |
7 | 8 | "github.com/github/git-bundle-server/internal/bundles" |
8 | 9 | "github.com/github/git-bundle-server/internal/core" |
9 | 10 | "github.com/github/git-bundle-server/internal/git" |
| 11 | + "github.com/github/git-bundle-server/internal/log" |
10 | 12 | ) |
11 | 13 |
|
12 | | -type Init struct{} |
| 14 | +type initCmd struct { |
| 15 | + logger log.TraceLogger |
| 16 | +} |
| 17 | + |
| 18 | +func NewInitCommand(logger log.TraceLogger) argparse.Subcommand { |
| 19 | + return &initCmd{ |
| 20 | + logger: logger, |
| 21 | + } |
| 22 | +} |
13 | 23 |
|
14 | | -func (Init) Name() string { |
| 24 | +func (initCmd) Name() string { |
15 | 25 | return "init" |
16 | 26 | } |
17 | 27 |
|
18 | | -func (Init) Description() string { |
| 28 | +func (initCmd) Description() string { |
19 | 29 | return ` |
20 | 30 | Initialize a repository by cloning a bare repo from '<url>', whose bundles |
21 | 31 | should be hosted at '<route>'.` |
22 | 32 | } |
23 | 33 |
|
24 | | -func (Init) Run(args []string) error { |
25 | | - parser := argparse.NewArgParser("git-bundle-server init <url> <route>") |
| 34 | +func (i *initCmd) Run(ctx context.Context, args []string) error { |
| 35 | + parser := argparse.NewArgParser(i.logger, "git-bundle-server init <url> <route>") |
26 | 36 | url := parser.PositionalString("url", "the URL of a repository to clone") |
27 | 37 | // TODO: allow parsing <route> out of <url> |
28 | 38 | route := parser.PositionalString("route", "the route to host the specified repo") |
29 | | - parser.Parse(args) |
| 39 | + parser.Parse(ctx, args) |
30 | 40 |
|
31 | 41 | repo, err := core.CreateRepository(*route) |
32 | 42 | if err != nil { |
33 | | - return err |
| 43 | + return i.logger.Error(ctx, err) |
34 | 44 | } |
35 | 45 |
|
36 | 46 | fmt.Printf("Cloning repository from %s\n", *url) |
37 | 47 | gitErr := git.GitCommand("clone", "--bare", *url, repo.RepoDir) |
38 | 48 |
|
39 | 49 | if gitErr != nil { |
40 | | - return fmt.Errorf("failed to clone repository: %w", gitErr) |
| 50 | + return i.logger.Errorf(ctx, "failed to clone repository: %w", gitErr) |
41 | 51 | } |
42 | 52 |
|
43 | 53 | gitErr = git.GitCommand("-C", repo.RepoDir, "config", "remote.origin.fetch", "+refs/heads/*:refs/heads/*") |
44 | 54 | if gitErr != nil { |
45 | | - return fmt.Errorf("failed to configure refspec: %w", gitErr) |
| 55 | + return i.logger.Errorf(ctx, "failed to configure refspec: %w", gitErr) |
46 | 56 | } |
47 | 57 |
|
48 | 58 | gitErr = git.GitCommand("-C", repo.RepoDir, "fetch", "origin") |
49 | 59 | if gitErr != nil { |
50 | | - return fmt.Errorf("failed to fetch latest refs: %w", gitErr) |
| 60 | + return i.logger.Errorf(ctx, "failed to fetch latest refs: %w", gitErr) |
51 | 61 | } |
52 | 62 |
|
53 | 63 | bundle := bundles.CreateInitialBundle(repo) |
54 | 64 | fmt.Printf("Constructing base bundle file at %s\n", bundle.Filename) |
55 | 65 |
|
56 | 66 | written, gitErr := git.CreateBundle(repo.RepoDir, bundle.Filename) |
57 | 67 | if gitErr != nil { |
58 | | - return fmt.Errorf("failed to create bundle: %w", gitErr) |
| 68 | + return i.logger.Errorf(ctx, "failed to create bundle: %w", gitErr) |
59 | 69 | } |
60 | 70 | if !written { |
61 | | - return fmt.Errorf("refused to write empty bundle. Is the repo empty?") |
| 71 | + return i.logger.Errorf(ctx, "refused to write empty bundle. Is the repo empty?") |
62 | 72 | } |
63 | 73 |
|
64 | 74 | list := bundles.CreateSingletonList(bundle) |
65 | 75 | listErr := bundles.WriteBundleList(list, repo) |
66 | 76 | if listErr != nil { |
67 | | - return fmt.Errorf("failed to write bundle list: %w", listErr) |
| 77 | + return i.logger.Errorf(ctx, "failed to write bundle list: %w", listErr) |
68 | 78 | } |
69 | 79 |
|
70 | 80 | SetCronSchedule() |
|
0 commit comments