github.com/erikjuhani/git-gong@v0.0.0-20220213141213-6b9fa82d4e7c/cmd/init.go (about) 1 package cmd 2 3 import ( 4 "os" 5 6 "github.com/erikjuhani/git-gong/gong" 7 "github.com/spf13/cobra" 8 ) 9 10 func init() { 11 rootCmd.AddCommand(initCmd) 12 initFlags() 13 } 14 15 var ( 16 defaultBranch string 17 bare bool 18 ) 19 20 var initCmd = &cobra.Command{ 21 Use: "init [directory]", 22 Short: "Create an empty Git repository", 23 Long: `Description: 24 Init command creates an empty Git repository to the current working directory. 25 26 The init command also creates a .git directory with subdirectories for objects, 27 refs/heads, refs/tags and template files. 28 29 By default Gong initializes the repository's default branch as main instead of master.`, 30 Args: cobra.MaximumNArgs(1), 31 Run: func(cmd *cobra.Command, args []string) { 32 path, err := os.Getwd() 33 if err != nil { 34 cmd.PrintErr(err) 35 return 36 } 37 38 if len(args) == 1 { 39 path = args[0] 40 } 41 42 err = initRepository(path) 43 if err != nil { 44 cmd.PrintErr(err) 45 return 46 } 47 }, 48 } 49 50 func initFlags() { 51 initCmd.Flags().StringVarP( 52 &defaultBranch, "default-branch", "d", gong.DefaultReference, 53 "Use specified name for the default branch, when creating a new repository.", 54 ) 55 } 56 57 func initRepository(path string) error { 58 repo, err := gong.Init(path, bare, defaultBranch) 59 defer gong.Free(repo) 60 61 return err 62 }