mugit/internal/cli/repo.go (view raw)
| 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 | "olexsmir.xyz/mugit/internal/mirror" |
| 14 | ) |
| 15 | |
| 16 | func (c *Cli) repoNewAction(ctx context.Context, cmd *cli.Command) error { |
| 17 | name, err := c.getRepoNameArg(cmd) |
| 18 | if name == "" { |
| 19 | return err |
| 20 | } |
| 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 | repo, err := git.Open(path, "") |
| 36 | if err != nil { |
| 37 | return fmt.Errorf("failed to open repo: %w", err) |
| 38 | } |
| 39 | |
| 40 | if err := repo.SetPrivate(cmd.Bool("private")); err != nil { |
| 41 | return fmt.Errorf("failed to set private status: %w", err) |
| 42 | } |
| 43 | |
| 44 | mirrorURL := cmd.String("mirror") |
| 45 | if mirrorURL != "" { |
| 46 | if err := mirror.IsRemoteSupported(mirrorURL); err != nil { |
| 47 | return err |
| 48 | } |
| 49 | if err := repo.SetMirrorRemote(mirrorURL); err != nil { |
| 50 | return fmt.Errorf("failed to set mirror remote: %w", err) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | return nil |
| 55 | } |
| 56 | |
| 57 | func (c *Cli) repoDescriptionAction(ctx context.Context, cmd *cli.Command) error { |
| 58 | name, err := c.getRepoNameArg(cmd) |
| 59 | if name == "" { |
| 60 | return err |
| 61 | } |
| 62 | |
| 63 | repo, err := c.openRepo(name) |
| 64 | if err != nil { |
| 65 | return fmt.Errorf("failed to open repo: %w", err) |
| 66 | } |
| 67 | |
| 68 | newDesc := cmd.Args().Get(0) |
| 69 | if newDesc != "" { |
| 70 | if err = repo.SetDescription(newDesc); err != nil { |
| 71 | return fmt.Errorf("failed to set description: %w", err) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | desc, err := repo.Description() |
| 76 | if err != nil { |
| 77 | return fmt.Errorf("failed to get description: %w", err) |
| 78 | } |
| 79 | |
| 80 | slog.Info("changed repo description", "repo", name, "new_description", desc) |
| 81 | return nil |
| 82 | } |
| 83 | |
| 84 | func (c *Cli) repoPrivateAction(ctx context.Context, cmd *cli.Command) error { |
| 85 | name, err := c.getRepoNameArg(cmd) |
| 86 | if name == "" { |
| 87 | return err |
| 88 | } |
| 89 | |
| 90 | repo, err := c.openRepo(name) |
| 91 | if err != nil { |
| 92 | return fmt.Errorf("failed to open repo: %w", err) |
| 93 | } |
| 94 | |
| 95 | isPrivate, err := repo.IsPrivate() |
| 96 | if err != nil { |
| 97 | return fmt.Errorf("failed to get private status: %w", err) |
| 98 | } |
| 99 | |
| 100 | newStatus := !isPrivate |
| 101 | if err := repo.SetPrivate(newStatus); err != nil { |
| 102 | return fmt.Errorf("failed to set private status: %w", err) |
| 103 | } |
| 104 | |
| 105 | slog.Info("new repo private status", "repo", name, "is_private", newStatus) |
| 106 | return nil |
| 107 | } |
| 108 | |
| 109 | func (c *Cli) repoSetDefaultAction(ctx context.Context, cmd *cli.Command) error { |
| 110 | name, err := c.getRepoNameArg(cmd) |
| 111 | if name == "" { |
| 112 | return err |
| 113 | } |
| 114 | |
| 115 | repo, err := c.openRepo(name) |
| 116 | if err != nil { |
| 117 | return fmt.Errorf("failed to open repo: %w", err) |
| 118 | } |
| 119 | |
| 120 | branch := cmd.Args().Get(0) |
| 121 | slog.Info("chaining repo head", "repo", name, "branch", branch) |
| 122 | err = repo.Checkout(branch) |
| 123 | return err |
| 124 | } |
| 125 | |
| 126 | func (c *Cli) getRepoNameArg(cmd *cli.Command) (string, error) { |
| 127 | name := cmd.StringArg("name") |
| 128 | if name == "" { |
| 129 | return "", fmt.Errorf("no name provided") |
| 130 | } |
| 131 | name = strings.TrimRight(name, ".git") + ".git" |
| 132 | return name, nil |
| 133 | } |