mugit/internal/cli/cli.go (view raw)
Oleksandr Smirnov
Oleksandr Smirnov
olexsmir@gmail.com cli: create private repo; toggle private status, 4 months ago
olexsmir@gmail.com cli: create private repo; toggle private status, 4 months ago
| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | |
| 7 | securejoin "github.com/cyphar/filepath-securejoin" |
| 8 | "github.com/urfave/cli/v3" |
| 9 | "olexsmir.xyz/mugit/internal/config" |
| 10 | "olexsmir.xyz/mugit/internal/git" |
| 11 | ) |
| 12 | |
| 13 | type Cli struct { |
| 14 | cfg *config.Config |
| 15 | version string |
| 16 | } |
| 17 | |
| 18 | func New(version string) *Cli { |
| 19 | return &Cli{ |
| 20 | version: version, |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | func (c *Cli) Run(ctx context.Context, args []string) error { |
| 25 | cmd := &cli.Command{ |
| 26 | Name: "mugit", |
| 27 | Usage: "a frontend for git repos", |
| 28 | Version: c.version, |
| 29 | EnableShellCompletion: true, |
| 30 | Flags: []cli.Flag{ |
| 31 | &cli.StringFlag{ |
| 32 | Name: "config", |
| 33 | Aliases: []string{"c"}, |
| 34 | Usage: "path to config file", |
| 35 | }, |
| 36 | }, |
| 37 | Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) { |
| 38 | loadedCfg, err := config.Load(cmd.String("config")) |
| 39 | if err != nil { |
| 40 | return ctx, err |
| 41 | } |
| 42 | c.cfg = loadedCfg |
| 43 | return ctx, nil |
| 44 | }, |
| 45 | Commands: []*cli.Command{ |
| 46 | { |
| 47 | Name: "serve", |
| 48 | Usage: "starts the server", |
| 49 | Action: c.serveAction, |
| 50 | }, |
| 51 | { |
| 52 | Name: "repo", |
| 53 | Commands: []*cli.Command{ |
| 54 | { |
| 55 | Name: "new", |
| 56 | Usage: "create new repo", |
| 57 | Action: c.repoNewAction, |
| 58 | Arguments: []cli.Argument{ |
| 59 | &cli.StringArg{Name: "name"}, |
| 60 | }, |
| 61 | Flags: []cli.Flag{ |
| 62 | &cli.StringFlag{ |
| 63 | Name: "mirror", |
| 64 | Usage: "remote URL(only http/https) to mirror from", |
| 65 | }, |
| 66 | &cli.BoolFlag{ |
| 67 | Name: "private", |
| 68 | Usage: "make the repository private", |
| 69 | }, |
| 70 | }, |
| 71 | }, |
| 72 | { |
| 73 | Name: "description", |
| 74 | Usage: "get or set repo description", |
| 75 | Action: c.repoDescriptionAction, |
| 76 | Arguments: []cli.Argument{ |
| 77 | &cli.StringArg{Name: "name"}, |
| 78 | }, |
| 79 | }, |
| 80 | { |
| 81 | Name: "private", |
| 82 | Usage: "toggle private status of a repo", |
| 83 | Action: c.repoPrivateAction, |
| 84 | Arguments: []cli.Argument{ |
| 85 | &cli.StringArg{Name: "name"}, |
| 86 | }, |
| 87 | }, |
| 88 | }, |
| 89 | }, |
| 90 | }, |
| 91 | } |
| 92 | return cmd.Run(ctx, args) |
| 93 | } |
| 94 | |
| 95 | func (c *Cli) openRepo(name string) (*git.Repo, error) { |
| 96 | path, err := securejoin.SecureJoin(c.cfg.Repo.Dir, name) |
| 97 | if err != nil { |
| 98 | return nil, err |
| 99 | } |
| 100 | |
| 101 | repo, err := git.Open(path, "") |
| 102 | if err != nil { |
| 103 | return nil, fmt.Errorf("failed to open repo: %w", err) |
| 104 | } |
| 105 | |
| 106 | return repo, nil |
| 107 | } |