|
| 1 | +/* |
| 2 | +Copyright © 2024 ErdemOzgen m.erdemozgen@gmail.com |
| 3 | +*/ |
| 4 | +package cmd |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "os" |
| 10 | + "os/exec" |
| 11 | + "path/filepath" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "github.com/ErdemOzgen/blackdagger/internal/config" |
| 15 | + "github.com/spf13/cobra" |
| 16 | + "github.com/spf13/viper" |
| 17 | +) |
| 18 | + |
| 19 | +// pullCmd represents the pull command |
| 20 | +var pullCmd = &cobra.Command{ |
| 21 | + Use: "pull", |
| 22 | + Short: "Get YAML files from remote repository for collection of jobs (e.g. default,devops,cart,mlops,etc.)", |
| 23 | + Long: `Get YAML files from remote repository for collection of jobs (e.g. default,devops,cart,mlops,etc.) |
| 24 | + Example: blackdagger pull default |
| 25 | + Example: blackdagger pull devops |
| 26 | + Example: blackdagger pull cart |
| 27 | + Example: blackdagger pull mlops |
| 28 | + Example: blackdagger pull all |
| 29 | + |
| 30 | + see |
| 31 | + `, |
| 32 | + PreRun: func(cmd *cobra.Command, args []string) { |
| 33 | + _ = viper.BindPFlag("dags", cmd.Flags().Lookup("dags")) // /home/erdem/.blackdagger/dags |
| 34 | + _ = viper.BindPFlag("logDir", cmd.Flags().Lookup("logDir")) // /home/erdem/.blackdagger/logs |
| 35 | + _ = viper.BindPFlag("dataDir", cmd.Flags().Lookup("dataDir")) // /home/erdem/.blackdagger/data |
| 36 | + cobra.CheckErr(config.LoadConfig(homeDir)) |
| 37 | + }, |
| 38 | + Run: func(cmd *cobra.Command, args []string) { |
| 39 | + fmt.Println("Starting to pull the repository...") |
| 40 | + pulldags(args) |
| 41 | + }, |
| 42 | +} |
| 43 | + |
| 44 | +func init() { |
| 45 | + rootCmd.AddCommand(pullCmd) |
| 46 | + |
| 47 | + // Here you will define your flags and configuration settings. |
| 48 | + |
| 49 | + // Cobra supports Persistent Flags which will work for this command |
| 50 | + // and all subcommands, e.g.: |
| 51 | + // pullCmd.PersistentFlags().String("foo", "", "A help for foo") |
| 52 | + |
| 53 | + // Cobra supports local flags which will only run when this command |
| 54 | + // is called directly, e.g.: |
| 55 | + // pullCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") |
| 56 | +} |
| 57 | + |
| 58 | +func pulldags(args []string) { |
| 59 | + jobCategories := []string{"mlops", "default", "devsecops", "devops"} |
| 60 | + if len(args) > 0 { |
| 61 | + if checkCategory(jobCategories, args[0]) { |
| 62 | + repoName := args[0] // Use the first argument as the repository name, e.g., "default" |
| 63 | + viper.AutomaticEnv() // Ensure Viper is looking for env vars |
| 64 | + dagValue := viper.GetString("dags") // Get the DAG config value |
| 65 | + fmt.Printf("DAG configuration value: %s\n", dagValue) |
| 66 | + |
| 67 | + // Define the path for the folder based on the argument |
| 68 | + folderPath := filepath.Join(dagValue, repoName) |
| 69 | + |
| 70 | + // Check if the directory exists |
| 71 | + if _, err := os.Stat(folderPath); os.IsNotExist(err) { |
| 72 | + // Directory does not exist, clone the repository |
| 73 | + gitCmd := exec.Command("git", "clone", fmt.Sprintf("https://github.com/ErdemOzgen/blackdagger-%s.git", repoName), folderPath) |
| 74 | + if output, err := gitCmd.CombinedOutput(); err != nil { |
| 75 | + fmt.Printf("Failed to clone the repository: %v, output: %s\n", err, string(output)) |
| 76 | + fmt.Println("This category may not been public yet. Stay tuned for updates!") |
| 77 | + } else { |
| 78 | + fmt.Printf("Successfully cloned the repository into %s\n", folderPath) |
| 79 | + // Copy the YAML files to the DAGs folder |
| 80 | + CopyYAMLFiles(folderPath, dagValue) |
| 81 | + } |
| 82 | + } else { |
| 83 | + // Directory exists, pull the repository |
| 84 | + fmt.Printf("The directory %s already exists. Pulling updates...\n", folderPath) |
| 85 | + |
| 86 | + // Change the working directory to the repository's folder |
| 87 | + os.Chdir(folderPath) |
| 88 | + |
| 89 | + // Execute git pull to update the repository |
| 90 | + gitCmd := exec.Command("git", "pull") |
| 91 | + if output, err := gitCmd.CombinedOutput(); err != nil { |
| 92 | + fmt.Printf("Failed to pull the repository: %v, output: %s\n", err, string(output)) |
| 93 | + } else { |
| 94 | + fmt.Printf("Successfully updated the repository in %s\n", folderPath) |
| 95 | + // Copy the YAML files to the DAGs folder |
| 96 | + CopyYAMLFiles(folderPath, dagValue) |
| 97 | + } |
| 98 | + } |
| 99 | + } else { |
| 100 | + fmt.Println("Please specify correct category name.") |
| 101 | + fmt.Println("Available categories are: ", jobCategories) |
| 102 | + } |
| 103 | + } else { |
| 104 | + fmt.Println("Please specify a category name.") |
| 105 | + fmt.Println("Available categories are: ", jobCategories) |
| 106 | + } |
| 107 | + |
| 108 | +} |
| 109 | + |
| 110 | +// checkCategory checks if a string is present in a category. |
| 111 | +// It returns true if the string is found, otherwise false. |
| 112 | +func checkCategory(slice []string, str string) bool { |
| 113 | + for _, item := range slice { |
| 114 | + if item == str { |
| 115 | + return true |
| 116 | + } |
| 117 | + } |
| 118 | + return false |
| 119 | +} |
| 120 | + |
| 121 | +// CopyYAMLFiles copies all YAML files from srcDir to destDir. |
| 122 | +func CopyYAMLFiles(srcDir, destDir string) { |
| 123 | + files, err := os.ReadDir(srcDir) |
| 124 | + if err != nil { |
| 125 | + panic(err) |
| 126 | + } |
| 127 | + |
| 128 | + for _, file := range files { |
| 129 | + if file.IsDir() { |
| 130 | + continue // Skip directories |
| 131 | + } |
| 132 | + |
| 133 | + if strings.HasSuffix(file.Name(), ".yaml") || strings.HasSuffix(file.Name(), ".yml") { |
| 134 | + srcPath := filepath.Join(srcDir, file.Name()) |
| 135 | + destPath := filepath.Join(destDir, file.Name()) |
| 136 | + |
| 137 | + // Open the source file |
| 138 | + srcFile, err := os.Open(srcPath) |
| 139 | + if err != nil { |
| 140 | + panic(err) |
| 141 | + } |
| 142 | + defer srcFile.Close() |
| 143 | + |
| 144 | + // Create the destination file |
| 145 | + destFile, err := os.Create(destPath) |
| 146 | + if err != nil { |
| 147 | + panic(err) |
| 148 | + } |
| 149 | + defer destFile.Close() |
| 150 | + |
| 151 | + // Copy the contents of the source file to the destination file |
| 152 | + _, err = io.Copy(destFile, srcFile) |
| 153 | + if err != nil { |
| 154 | + panic(err) |
| 155 | + } |
| 156 | + fmt.Printf("Copied %s to %s\n", srcPath, destPath) |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments