1package cmd
 2
 3import (
 4	"io"
 5	"os/exec"
 6
 7	"github.com/charmbracelet/soft-serve/proto"
 8	"github.com/spf13/cobra"
 9)
10
11// TODO: remove this command.
12// GitCommand returns a command that handles Git operations.
13func GitCommand() *cobra.Command {
14	gitCmd := &cobra.Command{
15		Use:   "git REPO COMMAND",
16		Short: "Perform Git operations on a repository.",
17		RunE: func(cmd *cobra.Command, args []string) error {
18			cfg, s := fromContext(cmd)
19			auth := cfg.AuthRepo("config", s.PublicKey())
20			if auth < proto.AdminAccess {
21				return ErrUnauthorized
22			}
23			if len(args) < 1 {
24				return runGit(nil, s, s, "")
25			}
26			var repo proto.Repository
27			rn := args[0]
28			repoExists := false
29			repos, err := cfg.ListRepos()
30			if err != nil {
31				return err
32			}
33			for _, rp := range repos {
34				if rp.Name() == rn {
35					re, err := rp.Open()
36					if err != nil {
37						continue
38					}
39					repoExists = true
40					repo = re
41					break
42				}
43			}
44			if !repoExists {
45				return ErrRepoNotFound
46			}
47			return runGit(nil, s, s, repo.Repository().Path, args[1:]...)
48		},
49	}
50	gitCmd.Flags().SetInterspersed(false)
51
52	return gitCmd
53}
54
55func runGit(in io.Reader, out, err io.Writer, dir string, args ...string) error {
56	cmd := exec.Command("git", args...)
57	cmd.Stdin = in
58	cmd.Stdout = out
59	cmd.Stderr = err
60	cmd.Dir = dir
61	return cmd.Run()
62}