|
1 | 1 | /*
|
2 | 2 | Copyright © 2023 NAME HERE <EMAIL ADDRESS>
|
3 |
| -
|
4 | 3 | */
|
5 | 4 | package cmd
|
6 | 5 |
|
7 | 6 | import (
|
8 | 7 | "fmt"
|
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "strings" |
9 | 11 |
|
10 | 12 | "github.com/spf13/cobra"
|
11 | 13 | )
|
12 | 14 |
|
| 15 | +var gitUrl string |
| 16 | + |
13 | 17 | // cloneCmd represents the clone command
|
14 | 18 | var cloneCmd = &cobra.Command{
|
15 | 19 | 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. |
19 | 23 |
|
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.`, |
23 | 26 | Run: func(cmd *cobra.Command, args []string) {
|
24 |
| - fmt.Println("clone called") |
| 27 | + clone() |
25 | 28 | },
|
26 | 29 | }
|
27 | 30 |
|
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 |
30 | 42 |
|
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 | + } |
32 | 48 |
|
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) |
36 | 70 |
|
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 | + } |
40 | 76 | }
|
0 commit comments