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