all repos

mugit @ 20a3418

🐮 git server that your cow will love

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
cli: add a way to change HEAD of repo, 3 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.BoolFlag{
68
								Name:  "private",
69
								Usage: "make the repository private",
70
							},
71
						},
72
					},
73
					{
74
						Name:   "description",
75
						Usage:  "get or set repo description",
76
						Action: c.repoDescriptionAction,
77
						Arguments: []cli.Argument{
78
							&cli.StringArg{Name: "name"},
79
						},
80
					},
81
					{
82
						Name:   "private",
83
						Usage:  "toggle private status of a repo",
84
						Action: c.repoPrivateAction,
85
						Arguments: []cli.Argument{
86
							&cli.StringArg{Name: "name"},
87
						},
88
					},
89
					{
90
						Name:   "checkout",
91
						Usage:  "switch branches in repo",
92
						Action: c.repoCheckoutAction,
93
						Arguments: []cli.Argument{
94
							&cli.StringArg{Name: "name"},
95
						},
96
					},
97
				},
98
			},
99
		},
100
	}
101
	return cmd.Run(ctx, args)
102
}
103
104
func (c *Cli) openRepo(name string) (*git.Repo, error) {
105
	path, err := securejoin.SecureJoin(c.cfg.Repo.Dir, name)
106
	if err != nil {
107
		return nil, err
108
	}
109
110
	repo, err := git.Open(path, "")
111
	if err != nil {
112
		return nil, fmt.Errorf("failed to open repo: %w", err)
113
	}
114
115
	return repo, nil
116
}