mugit/internal/cli/repo.go (view raw)
Oleksandr Smirnov
Oleksandr Smirnov
olexsmir@gmail.com test: add missing tests (#5)..., 2 months ago
olexsmir@gmail.com test: add missing tests (#5)..., 2 months ago
| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "log/slog" |
| 7 | "os" |
| 8 | |
| 9 | "github.com/urfave/cli/v3" |
| 10 | "olexsmir.xyz/mugit/internal/git" |
| 11 | "olexsmir.xyz/mugit/internal/mirror" |
| 12 | ) |
| 13 | |
| 14 | func (c *Cli) repoNewAction(ctx context.Context, cmd *cli.Command) error { |
| 15 | name, err := c.getRepoNameArg(cmd) |
| 16 | if name == "" { |
| 17 | return err |
| 18 | } |
| 19 | |
| 20 | path, err := git.ResolvePath(c.cfg.Repo.Dir, name) |
| 21 | if err != nil { |
| 22 | return err |
| 23 | } |
| 24 | |
| 25 | if _, err = os.Stat(path); err == nil { |
| 26 | return fmt.Errorf("repository already exists: %s", name) |
| 27 | } |
| 28 | |
| 29 | mirrorURL := cmd.String("mirror") |
| 30 | if mirrorURL != "" { |
| 31 | if merr := mirror.IsRemoteSupported(mirrorURL); merr != nil { |
| 32 | return merr |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | if err = git.Init(path); err != nil { |
| 37 | return err |
| 38 | } |
| 39 | |
| 40 | repo, err := git.Open(path, "") |
| 41 | if err != nil { |
| 42 | return fmt.Errorf("failed to open repo: %w", err) |
| 43 | } |
| 44 | |
| 45 | if err := repo.SetPrivate(cmd.Bool("private")); err != nil { |
| 46 | return fmt.Errorf("failed to set private status: %w", err) |
| 47 | } |
| 48 | |
| 49 | if mirrorURL != "" { |
| 50 | if err := repo.SetMirrorRemote(mirrorURL); err != nil { |
| 51 | return fmt.Errorf("failed to set mirror remote: %w", err) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | desc := cmd.String("description") |
| 56 | if desc != "" { |
| 57 | if err := repo.SetDescription(desc); err != nil { |
| 58 | return fmt.Errorf("failed to set description: %w", err) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | return nil |
| 63 | } |
| 64 | |
| 65 | func (c *Cli) repoDescriptionAction(ctx context.Context, cmd *cli.Command) error { |
| 66 | name, err := c.getRepoNameArg(cmd) |
| 67 | if name == "" { |
| 68 | return err |
| 69 | } |
| 70 | |
| 71 | repo, err := c.openRepo(name) |
| 72 | if err != nil { |
| 73 | return fmt.Errorf("failed to open repo: %w", err) |
| 74 | } |
| 75 | |
| 76 | newDesc := cmd.Args().Get(0) |
| 77 | if newDesc != "" { |
| 78 | if err = repo.SetDescription(newDesc); err != nil { |
| 79 | return fmt.Errorf("failed to set description: %w", err) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | desc, err := repo.Description() |
| 84 | if err != nil { |
| 85 | return fmt.Errorf("failed to get description: %w", err) |
| 86 | } |
| 87 | |
| 88 | slog.Info("changed repo description", "repo", name, "new_description", desc) |
| 89 | return nil |
| 90 | } |
| 91 | |
| 92 | func (c *Cli) repoPrivateAction(ctx context.Context, cmd *cli.Command) error { |
| 93 | name, err := c.getRepoNameArg(cmd) |
| 94 | if name == "" { |
| 95 | return err |
| 96 | } |
| 97 | |
| 98 | repo, err := c.openRepo(name) |
| 99 | if err != nil { |
| 100 | return fmt.Errorf("failed to open repo: %w", err) |
| 101 | } |
| 102 | |
| 103 | isPrivate, err := repo.IsPrivate() |
| 104 | if err != nil { |
| 105 | return fmt.Errorf("failed to get private status: %w", err) |
| 106 | } |
| 107 | |
| 108 | newStatus := !isPrivate |
| 109 | if err := repo.SetPrivate(newStatus); err != nil { |
| 110 | return fmt.Errorf("failed to set private status: %w", err) |
| 111 | } |
| 112 | |
| 113 | slog.Info("new repo private status", "repo", name, "is_private", newStatus) |
| 114 | return nil |
| 115 | } |
| 116 | |
| 117 | func (c *Cli) repoSetHeadAction(ctx context.Context, cmd *cli.Command) error { |
| 118 | name, err := c.getRepoNameArg(cmd) |
| 119 | if name == "" { |
| 120 | return err |
| 121 | } |
| 122 | |
| 123 | repo, err := c.openRepo(name) |
| 124 | if err != nil { |
| 125 | return fmt.Errorf("failed to open repo: %w", err) |
| 126 | } |
| 127 | |
| 128 | branch := cmd.Args().Get(0) |
| 129 | if err = repo.Checkout(branch); err != nil { |
| 130 | return err |
| 131 | } |
| 132 | |
| 133 | slog.Info("changed repo head", "repo", name, "branch", branch) |
| 134 | return err |
| 135 | } |
| 136 | |
| 137 | func (c *Cli) getRepoNameArg(cmd *cli.Command) (string, error) { |
| 138 | name := cmd.StringArg("name") |
| 139 | if name == "" { |
| 140 | return "", fmt.Errorf("no name provided") |
| 141 | } |
| 142 | return git.ResolveName(name), nil |
| 143 | } |