Skip to content

Commit 57580b1

Browse files
added the clone command and tested it
1 parent 15b1582 commit 57580b1

File tree

1 file changed

+53
-17
lines changed

1 file changed

+53
-17
lines changed

cmd/clone.go

+53-17
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,76 @@
11
/*
22
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
3-
43
*/
54
package cmd
65

76
import (
87
"fmt"
8+
"os"
9+
"os/exec"
10+
"strings"
911

1012
"github.com/spf13/cobra"
1113
)
1214

15+
var gitUrl string
16+
1317
// cloneCmd represents the clone command
1418
var cloneCmd = &cobra.Command{
1519
Use: "clone",
16-
Short: "A brief description of your command",
17-
Long: `A longer description that spans multiple lines and likely contains examples
18-
and usage of using your command. For example:
20+
Short: "To Clone A Repository And Start NeoVim In It",
21+
Long: `Use this command to clone a Repository, CD into that Repository
22+
and then open that Repository in Neovim.
1923
20-
Cobra is a CLI library for Go that empowers applications.
21-
This application is a tool to generate the needed files
22-
to quickly create a Cobra application.`,
24+
**Note:
25+
Please Only Enter The SSH Link Of The Repository.`,
2326
Run: func(cmd *cobra.Command, args []string) {
24-
fmt.Println("clone called")
27+
clone()
2528
},
2629
}
2730

28-
func init() {
29-
rootCmd.AddCommand(cloneCmd)
31+
func clone() {
32+
// Getting the repository name
33+
parts := strings.Split(gitUrl, "/")
34+
repoName := strings.TrimSuffix(parts[1], ".git")
35+
36+
// Execute commands sequentially
37+
38+
// Clone Command
39+
cmd := exec.Command("git", "clone", gitUrl)
40+
cmd.Stdout = os.Stdout
41+
cmd.Stderr = os.Stderr
3042

31-
// Here you will define your flags and configuration settings.
43+
err := cmd.Run()
44+
if err != nil {
45+
fmt.Printf("Error executing clone command: %v\n", err)
46+
return
47+
}
3248

33-
// Cobra supports Persistent Flags which will work for this command
34-
// and all subcommands, e.g.:
35-
// cloneCmd.PersistentFlags().String("foo", "", "A help for foo")
49+
// CD Command
50+
err = os.Chdir(repoName)
51+
if err != nil {
52+
fmt.Printf("Error executing cd command: %v\n", err)
53+
return
54+
}
55+
56+
// Nvim Command
57+
cmd = exec.Command("nvim")
58+
cmd.Stdout = os.Stdout
59+
cmd.Stderr = os.Stderr
60+
61+
err = cmd.Run()
62+
if err != nil {
63+
fmt.Printf("Error executing nvim command: %v\n", err)
64+
return
65+
}
66+
}
67+
68+
func init() {
69+
rootCmd.AddCommand(cloneCmd)
3670

37-
// Cobra supports local flags which will only run when this command
38-
// is called directly, e.g.:
39-
// cloneCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
71+
cloneCmd.Flags().StringVarP(&gitUrl, "url", "u", "", "url of the repository")
72+
err := cloneCmd.MarkFlagRequired("url")
73+
if err != nil {
74+
fmt.Println(err)
75+
}
4076
}

0 commit comments

Comments
 (0)