github.com/erikjuhani/git-gong@v0.0.0-20220213141213-6b9fa82d4e7c/cmd/clone.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "os" 6 "strings" 7 8 "github.com/erikjuhani/git-gong/gong" 9 "github.com/spf13/cobra" 10 ) 11 12 func init() { 13 rootCmd.AddCommand(cloneCmd) 14 } 15 16 var cloneCmd = &cobra.Command{ 17 Use: "clone [repository] [directory]", 18 Short: "Clone a repository", 19 Long: `Description: 20 Clone command clones a repository into a newly created directory and checks out 21 to initial branch of the cloned repository. 22 23 Clone command takes a valid GIT URL, where the repository is located as an argument.`, 24 Args: cobra.MinimumNArgs(1), 25 Run: func(cmd *cobra.Command, args []string) { 26 path, err := os.Getwd() 27 if err != nil { 28 cmd.PrintErr(err) 29 return 30 } 31 32 if len(args) > 1 { 33 path = fmt.Sprintf("%s/%s", path, args[1]) 34 } else { 35 parts := strings.Split(args[0], "/") 36 path = fmt.Sprintf("%s/%s", parts[len(parts)-3], strings.TrimSuffix(parts[len(parts)-1], ".git")) 37 } 38 39 repo, err := gong.Clone(args[0], path) 40 if err != nil { 41 cmd.PrintErr(err) 42 return 43 } 44 gong.Free(repo) 45 }, 46 }