-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.go
More file actions
98 lines (83 loc) · 2.63 KB
/
init.go
File metadata and controls
98 lines (83 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package lfs
import (
"context"
"fmt"
"io/fs"
"os"
"os/exec"
"path"
"strings"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/gitopia/git-remote-gitopia/config"
"github.com/gitopia/git-remote-gitopia/core"
"github.com/gitopia/git-remote-gitopia/core/api"
gitopiatypes "github.com/gitopia/gitopia/v5/x/gitopia/types"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func InitCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "init <remote_name>",
Short: "Initialize the lfsconfig for the gitopia remote",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
dir := path.Dir(os.Getenv("GIT_DIR"))
if dir == "" {
return errors.New("not a git repository")
}
lfsConfigPath := path.Join(dir, ".lfsconfig")
if _, err := os.Stat(lfsConfigPath); !errors.Is(err, fs.ErrNotExist) {
return errors.New(".lfsconfig file already exists")
}
c := exec.Command("git", "remote", "get-url", args[0])
output, err := c.Output()
if err != nil {
return errors.Wrap(err, "error reading the remote url")
}
remoteURL := strings.TrimSpace(string(output))
remoteUserId, remoteRepositoryName, err := core.ValidateGitopiaRemoteURL(string(remoteURL))
if err != nil {
return err
}
interfaceRegistry := codectypes.NewInterfaceRegistry()
grpcHost, _ := config.GitConfigGet(config.GitopiaConfigGRPCHostOption)
if grpcHost == "" || !api.CheckGRPCHostLiveness(grpcHost) {
provider := api.GetBestApiProvider()
if err := api.SetConfiguredGRPCHost(provider.GRPCHost); err != nil {
return err
}
}
grpcConn, err := grpc.Dial(grpcHost,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(interfaceRegistry).GRPCCodec())),
)
if err != nil {
return err
}
defer grpcConn.Close()
queryClient := gitopiatypes.NewQueryClient(grpcConn)
// Get RepositoryId
res, err := queryClient.AnyRepository(context.Background(), &gitopiatypes.QueryGetAnyRepositoryRequest{
Id: remoteUserId,
RepositoryName: remoteRepositoryName,
})
if err != nil {
return err
}
remoteRepository := *res.Repository
lfsURL := fmt.Sprintf("%v/%v.git", config.GitServerHost, remoteRepository.Id)
c = core.GitCommand("git", "config",
fmt.Sprintf("--file=%s", lfsConfigPath),
"lfs.url",
lfsURL)
if err := c.Run(); err != nil {
return errors.Wrap(err, "error creating .lfsconfig")
}
return nil
},
}
return cmd
}