Skip to content

Commit f439c30

Browse files
committed
address review comments
1 parent b6191ce commit f439c30

File tree

4 files changed

+59
-75
lines changed

4 files changed

+59
-75
lines changed

cmd/git-gitopia/lfs/init.go

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package lfs
33
import (
44
"context"
55
"fmt"
6+
"io/fs"
67
"os"
78
"os/exec"
89
"path"
@@ -19,18 +20,23 @@ import (
1920
"google.golang.org/grpc/credentials/insecure"
2021
)
2122

22-
var initCmd = &cobra.Command{
23-
Use: "init <remote_name>",
24-
Short: "Initialize the lfsconfig for the gitopia remote",
25-
Args: cobra.ExactArgs(1),
26-
RunE: func(cmd *cobra.Command, args []string) error {
27-
dir := path.Dir(os.Getenv("GIT_DIR"))
28-
if dir == "" {
29-
return errors.New("not a git repository")
30-
}
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+
}
3139

32-
lfsConfigPath := path.Join(dir, ".lfsconfig")
33-
if _, err := os.Stat(lfsConfigPath); os.IsNotExist(err) {
3440
c := exec.Command("git", "remote", "get-url", args[0])
3541
output, err := c.Output()
3642
if err != nil {
@@ -68,21 +74,16 @@ var initCmd = &cobra.Command{
6874
remoteRepository := *res.Repository
6975
lfsURL := fmt.Sprintf("%v/%v.git", config.GitServerHost, remoteRepository.Id)
7076

71-
cmd := core.GitCommand("git", "config",
77+
c = core.GitCommand("git", "config",
7278
fmt.Sprintf("--file=%s", lfsConfigPath),
7379
"lfs.url",
7480
lfsURL)
75-
if err := cmd.Run(); err != nil {
81+
if err := c.Run(); err != nil {
7682
return errors.Wrap(err, "error creating .lfsconfig")
7783
}
7884

7985
return nil
80-
}
81-
82-
return errors.New(".lfsconfig file already exists")
83-
},
84-
}
85-
86-
func init() {
87-
Commands.AddCommand(initCmd)
86+
},
87+
}
88+
return cmd
8889
}

cmd/git-gitopia/lfs/root.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ import (
44
"github.com/spf13/cobra"
55
)
66

7-
var Commands = &cobra.Command{
8-
Use: "lfs",
9-
Short: "Configure the lfsconfig for the gitopia remote",
10-
CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true},
11-
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
12-
},
7+
func Commands() *cobra.Command {
8+
cmd := &cobra.Command{
9+
Use: "lfs",
10+
Short: "Configure the lfsconfig for the gitopia remote",
11+
CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true},
12+
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
13+
},
14+
}
15+
cmd.AddCommand(InitCommand())
16+
17+
return cmd
1318
}

cmd/git-gitopia/main.go

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,7 @@ import (
66
"os"
77

88
"github.com/cosmos/cosmos-sdk/client"
9-
"github.com/cosmos/cosmos-sdk/client/keys"
10-
"github.com/cosmos/cosmos-sdk/codec"
11-
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
12-
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
13-
sdk "github.com/cosmos/cosmos-sdk/types"
149
"github.com/cosmos/cosmos-sdk/version"
15-
"github.com/gitopia/git-remote-gitopia/cmd/git-gitopia/lfs"
16-
"github.com/spf13/cobra"
1710
)
1811

1912
const (
@@ -26,31 +19,8 @@ func main() {
2619
ctx := context.Background()
2720
ctx = context.WithValue(ctx, client.ClientContextKey, new(client.Context))
2821
version.Name = AppName // os keyring service name is same as version name
29-
cmd := &cobra.Command{
30-
Use: "gitopia",
31-
CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true},
32-
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
33-
conf := sdk.GetConfig()
34-
conf.SetBech32PrefixForAccount(AccountAddressPrefix, AccountAddressPrefix+sdk.PrefixPublic)
35-
conf.Seal()
3622

37-
version.Name = AppName
38-
registry := codectypes.NewInterfaceRegistry()
39-
cryptocodec.RegisterInterfaces(registry)
40-
marshaler := codec.NewProtoCodec(registry)
41-
42-
initClientCtx := client.GetClientContextFromCmd(cmd).
43-
WithCodec(marshaler).
44-
WithInterfaceRegistry(registry).
45-
WithInput(os.Stdin)
46-
47-
// sets global flags for keys subcommand
48-
return client.SetCmdClientContextHandler(initClientCtx, cmd)
49-
},
50-
}
51-
cmd.AddCommand(keys.Commands("."))
52-
cmd.AddCommand(lfs.Commands)
53-
err := cmd.ExecuteContext(ctx)
23+
err := RootCommand().ExecuteContext(ctx)
5424
if err != nil {
5525
fmt.Fprint(os.Stderr, err.Error())
5626
return

cmd/git-gitopia/root.go

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,39 @@ import (
44
"os"
55

66
"github.com/cosmos/cosmos-sdk/client"
7+
"github.com/cosmos/cosmos-sdk/client/keys"
78
"github.com/cosmos/cosmos-sdk/codec"
89
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
910
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
1011
sdk "github.com/cosmos/cosmos-sdk/types"
12+
"github.com/gitopia/git-remote-gitopia/cmd/git-gitopia/lfs"
1113
"github.com/spf13/cobra"
1214
)
1315

14-
var rootCmd = &cobra.Command{
15-
Use: "gitopia",
16-
CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true},
17-
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
18-
conf := sdk.GetConfig()
19-
conf.SetBech32PrefixForAccount(AccountAddressPrefix, AccountAddressPrefix+sdk.PrefixPublic)
20-
conf.Seal()
16+
func RootCommand() *cobra.Command {
17+
cmd := &cobra.Command{
18+
Use: "gitopia",
19+
CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true},
20+
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
21+
conf := sdk.GetConfig()
22+
conf.SetBech32PrefixForAccount(AccountAddressPrefix, AccountAddressPrefix+sdk.PrefixPublic)
23+
conf.Seal()
2124

22-
registry := codectypes.NewInterfaceRegistry()
23-
cryptocodec.RegisterInterfaces(registry)
24-
marshaler := codec.NewProtoCodec(registry)
25+
registry := codectypes.NewInterfaceRegistry()
26+
cryptocodec.RegisterInterfaces(registry)
27+
marshaler := codec.NewProtoCodec(registry)
2528

26-
initClientCtx := client.GetClientContextFromCmd(cmd).
27-
WithCodec(marshaler).
28-
WithInterfaceRegistry(registry).
29-
WithInput(os.Stdin)
29+
initClientCtx := client.GetClientContextFromCmd(cmd).
30+
WithCodec(marshaler).
31+
WithInterfaceRegistry(registry).
32+
WithInput(os.Stdin)
3033

31-
// sets global flags for keys subcommand
32-
return client.SetCmdClientContextHandler(initClientCtx, cmd)
33-
},
34+
// sets global flags for keys subcommand
35+
return client.SetCmdClientContextHandler(initClientCtx, cmd)
36+
},
37+
}
38+
cmd.AddCommand(keys.Commands("."))
39+
cmd.AddCommand(lfs.Commands())
40+
41+
return cmd
3442
}

0 commit comments

Comments
 (0)