@@ -3,6 +3,7 @@ package lfs
33import (
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}
0 commit comments