all repos

mugit @ d985efd13fa58bb461048b8652266621129505b1

🐮 git server that your cow will love

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
fix: read private flag when creating repo, 3 months ago
1
package cli
2
3
import (
4
	"context"
5
	"fmt"
6
	"log/slog"
7
	"os"
8
	"strings"
9
10
	securejoin "github.com/cyphar/filepath-securejoin"
11
	"github.com/urfave/cli/v3"
12
	"olexsmir.xyz/mugit/internal/git"
13
)
14
15
func (c *Cli) repoNewAction(ctx context.Context, cmd *cli.Command) error {
16
	name := cmd.StringArg("name")
17
	if name == "" {
18
		return fmt.Errorf("no name provided")
19
	}
20
21
	name = strings.TrimRight(name, ".git") + ".git"
22
23
	path, err := securejoin.SecureJoin(c.cfg.Repo.Dir, name)
24
	if err != nil {
25
		return err
26
	}
27
28
	if _, err = os.Stat(path); err == nil {
29
		return fmt.Errorf("repository already exists: %s", name)
30
	}
31
32
	if err = git.Init(path); err != nil {
33
		return err
34
	}
35
36
	repo, err := git.Open(path, "")
37
	if err != nil {
38
		return fmt.Errorf("failed to open repo: %w", err)
39
	}
40
41
	if err := repo.SetPrivate(cmd.Bool("private")); err != nil {
42
		return fmt.Errorf("failed to set private status: %w", err)
43
	}
44
45
	mirrorURL := cmd.String("mirror")
46
	if mirrorURL != "" {
47
		if !strings.HasPrefix(mirrorURL, "http") {
48
			return fmt.Errorf("only http and https remotes are supported")
49
		}
50
		if err := repo.SetMirrorRemote(mirrorURL); err != nil {
51
			return fmt.Errorf("failed to set mirror remote: %w", err)
52
		}
53
	}
54
55
	return nil
56
}
57
58
func (c *Cli) repoDescriptionAction(ctx context.Context, cmd *cli.Command) error {
59
	name := cmd.StringArg("name")
60
	if name == "" {
61
		return fmt.Errorf("no name provided")
62
	}
63
64
	name = strings.TrimRight(name, ".git") + ".git"
65
66
	repo, err := c.openRepo(name)
67
	if err != nil {
68
		return fmt.Errorf("failed to open repo: %w", err)
69
	}
70
71
	newDesc := cmd.Args().Get(0)
72
	if newDesc != "" {
73
		if err = repo.SetDescription(newDesc); err != nil {
74
			return fmt.Errorf("failed to set description: %w", err)
75
		}
76
	}
77
78
	desc, err := repo.Description()
79
	if err != nil {
80
		return fmt.Errorf("failed to get description: %w", err)
81
	}
82
83
	slog.Info("changed repo description", "repo", name, "new_description", desc)
84
	return nil
85
}
86
87
func (c *Cli) repoPrivateAction(ctx context.Context, cmd *cli.Command) error {
88
	name := cmd.StringArg("name")
89
	if name == "" {
90
		return fmt.Errorf("no name provided")
91
	}
92
93
	name = strings.TrimRight(name, ".git") + ".git"
94
95
	repo, err := c.openRepo(name)
96
	if err != nil {
97
		return fmt.Errorf("failed to open repo: %w", err)
98
	}
99
100
	isPrivate, err := repo.IsPrivate()
101
	if err != nil {
102
		return fmt.Errorf("failed to get private status: %w", err)
103
	}
104
105
	newStatus := !isPrivate
106
	if err := repo.SetPrivate(newStatus); err != nil {
107
		return fmt.Errorf("failed to set private status: %w", err)
108
	}
109
110
	slog.Info("new repo private status", "repo", name, "is_private", newStatus)
111
	return nil
112
}