1package cmd
2
3import (
4 "io"
5 "os/exec"
6
7 "github.com/charmbracelet/soft-serve/config"
8 gm "github.com/charmbracelet/soft-serve/server/git"
9 "github.com/spf13/cobra"
10)
11
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 ac, s := fromContext(cmd)
19 auth := ac.AuthRepo("config", s.PublicKey())
20 if auth < gm.AdminAccess {
21 return ErrUnauthorized
22 }
23 if len(args) < 1 {
24 return runGit(nil, s, s, "")
25 }
26 var repo *config.Repo
27 rn := args[0]
28 repoExists := false
29 for _, rp := range ac.Source.AllRepos() {
30 if rp.Repo() == rn {
31 repoExists = true
32 repo = rp
33 break
34 }
35 }
36 if !repoExists {
37 return ErrRepoNotFound
38 }
39 return runGit(nil, s, s, repo.Path(), args[1:]...)
40 },
41 }
42 gitCmd.Flags().SetInterspersed(false)
43
44 return gitCmd
45}
46
47func runGit(in io.Reader, out, err io.Writer, dir string, args ...string) error {
48 cmd := exec.Command("git", args...)
49 cmd.Stdin = in
50 cmd.Stdout = out
51 cmd.Stderr = err
52 cmd.Dir = dir
53 return cmd.Run()
54}