all repos

mugit @ 334ab235a0d17d253e3a1d7b816527eb63c794b0

🐮 git server that your cow will love

mugit/internal/cli/cli.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
cli: repo new --description, 2 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(
39
				config.PathOrDefault(cmd.String("config")))
40
			if err != nil {
41
				return ctx, err
42
			}
43
			c.cfg = loadedCfg
44
			return ctx, nil
45
		},
46
		Commands: []*cli.Command{
47
			{
48
				Name:   "serve",
49
				Usage:  "starts the server",
50
				Action: c.serveAction,
51
			},
52
			{
53
				Name: "repo",
54
				Commands: []*cli.Command{
55
					{
56
						Name:   "new",
57
						Usage:  "create new repo",
58
						Action: c.repoNewAction,
59
						Arguments: []cli.Argument{
60
							&cli.StringArg{Name: "name"},
61
						},
62
						Flags: []cli.Flag{
63
							&cli.StringFlag{
64
								Name:  "mirror",
65
								Usage: "remote URL(only http/https) to mirror from",
66
							},
67
							&cli.StringFlag{
68
								Name:    "description",
69
								Usage:   "set repo description",
70
								Aliases: []string{"desc"},
71
							},
72
							&cli.BoolFlag{
73
								Name:  "private",
74
								Usage: "make the repository private",
75
							},
76
						},
77
					},
78
					{
79
						Name:   "description",
80
						Usage:  "get or set repo description",
81
						Action: c.repoDescriptionAction,
82
						Arguments: []cli.Argument{
83
							&cli.StringArg{Name: "name"},
84
						},
85
					},
86
					{
87
						Name:   "private",
88
						Usage:  "toggle private status of a repo",
89
						Action: c.repoPrivateAction,
90
						Arguments: []cli.Argument{
91
							&cli.StringArg{Name: "name"},
92
						},
93
					},
94
					{
95
						Name:   "set-default",
96
						Usage:  "switch default repo branch",
97
						Action: c.repoSetDefaultAction,
98
						Arguments: []cli.Argument{
99
							&cli.StringArg{Name: "name"},
100
						},
101
					},
102
				},
103
			},
104
		},
105
	}
106
	return cmd.Run(ctx, args)
107
}
108
109
func (c *Cli) openRepo(name string) (*git.Repo, error) {
110
	path, err := securejoin.SecureJoin(c.cfg.Repo.Dir, name)
111
	if err != nil {
112
		return nil, err
113
	}
114
115
	repo, err := git.Open(path, "")
116
	if err != nil {
117
		return nil, fmt.Errorf("failed to open repo: %w", err)
118
	}
119
120
	return repo, nil
121
}