all repos

mugit @ ffeccd3029b1a465adf45caec127fac33ceb8123

🐮 git server that your cow will love

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
cli: repo description, 4 months ago
1
package cli
2
3
import (
4
	"context"
5
	"fmt"
6
	"os"
7
	"strings"
8
9
	securejoin "github.com/cyphar/filepath-securejoin"
10
	"github.com/urfave/cli/v3"
11
	"olexsmir.xyz/mugit/internal/git"
12
)
13
14
func (c *Cli) repoNewAction(ctx context.Context, cmd *cli.Command) error {
15
	name := cmd.StringArg("name")
16
	if name == "" {
17
		return fmt.Errorf("no name provided")
18
	}
19
20
	name = strings.TrimRight(name, ".git") + ".git"
21
22
	path, err := securejoin.SecureJoin(c.cfg.Repo.Dir, name)
23
	if err != nil {
24
		return err
25
	}
26
27
	if _, err := os.Stat(path); err == nil {
28
		return fmt.Errorf("repository already exists: %s", name)
29
	}
30
31
	if err := git.Init(path); err != nil {
32
		return err
33
	}
34
35
	mirrorURL := cmd.String("mirror")
36
	if mirrorURL != "" {
37
		if !strings.HasPrefix(mirrorURL, "http") {
38
			return fmt.Errorf("only http and https remotes are supported")
39
		}
40
		repo, err := git.Open(path, "")
41
		if err != nil {
42
			return fmt.Errorf("failed to open repo for mirror setup: %w", err)
43
		}
44
		if err := repo.SetMirrorRemote(mirrorURL); err != nil {
45
			return fmt.Errorf("failed to set mirror remote: %w", err)
46
		}
47
	}
48
49
	return nil
50
}
51
52
func (c *Cli) repoDescriptionAction(ctx context.Context, cmd *cli.Command) error {
53
	name := cmd.StringArg("name")
54
	if name == "" {
55
		return fmt.Errorf("no name provided")
56
	}
57
58
	name = strings.TrimRight(name, ".git") + ".git"
59
60
	repo, err := c.openRepo(name)
61
	if err != nil {
62
		return fmt.Errorf("failed to open repo: %w", err)
63
	}
64
65
	newDesc := cmd.Args().Get(0)
66
	if newDesc != "" {
67
		if err := repo.SetDescription(newDesc); err != nil {
68
			return fmt.Errorf("failed to set description: %w", err)
69
		}
70
	}
71
72
	desc, err := repo.Description()
73
	if err != nil {
74
		return fmt.Errorf("failed to get description: %w", err)
75
	}
76
77
	if desc == "" {
78
		fmt.Println("No description set")
79
	} else {
80
		fmt.Println(desc)
81
	}
82
83
	return nil
84
}