all repos

mugit @ 474cf87

🐮 git server that your cow will love

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
cli: set-default -> set-head, 2 months ago
1
package cli
2
3
import (
4
	"context"
5
	"fmt"
6
7
	"github.com/urfave/cli/v3"
8
	"olexsmir.xyz/mugit/internal/config"
9
	"olexsmir.xyz/mugit/internal/git"
10
)
11
12
type Cli struct {
13
	cfg     *config.Config
14
	version string
15
}
16
17
func New(version string) *Cli {
18
	return &Cli{
19
		version: version,
20
	}
21
}
22
23
func (c *Cli) Run(ctx context.Context, args []string) error {
24
	cmd := &cli.Command{
25
		Name:                  "mugit",
26
		Usage:                 "a frontend for git repos",
27
		Version:               c.version,
28
		EnableShellCompletion: true,
29
		Flags: []cli.Flag{
30
			&cli.StringFlag{
31
				Name:    "config",
32
				Aliases: []string{"c"},
33
				Usage:   "path to config file",
34
			},
35
		},
36
		Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
37
			loadedCfg, err := config.Load(
38
				config.PathOrDefault(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.StringFlag{
67
								Name:    "description",
68
								Usage:   "set repo description",
69
								Aliases: []string{"desc"},
70
							},
71
							&cli.BoolFlag{
72
								Name:  "private",
73
								Usage: "make the repository private",
74
							},
75
						},
76
					},
77
					{
78
						Name:   "description",
79
						Usage:  "get or set repo description",
80
						Action: c.repoDescriptionAction,
81
						Arguments: []cli.Argument{
82
							&cli.StringArg{Name: "name"},
83
						},
84
					},
85
					{
86
						Name:   "private",
87
						Usage:  "toggle private status of a repo",
88
						Action: c.repoPrivateAction,
89
						Arguments: []cli.Argument{
90
							&cli.StringArg{Name: "name"},
91
						},
92
					},
93
					{
94
						Name:   "set-head",
95
						Usage:  "switches repo's head to specified branch",
96
						Action: c.repoSetHeadAction,
97
						Arguments: []cli.Argument{
98
							&cli.StringArg{Name: "name"},
99
						},
100
					},
101
				},
102
			},
103
		},
104
	}
105
	return cmd.Run(ctx, args)
106
}
107
108
func (c *Cli) openRepo(name string) (*git.Repo, error) {
109
	path, err := git.ResolvePath(c.cfg.Repo.Dir, name)
110
	if err != nil {
111
		return nil, err
112
	}
113
114
	repo, err := git.Open(path, "")
115
	if err != nil {
116
		return nil, fmt.Errorf("failed to open repo: %w", err)
117
	}
118
119
	return repo, nil
120
}