|
| 1 | +package customLicense |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "net/http" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + "github.com/sirupsen/logrus" |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +type CustomLicenseCmd struct { |
| 14 | + logger *logrus.Logger |
| 15 | +} |
| 16 | + |
| 17 | +func NewCustomLicenseCmd(logger *logrus.Logger) *CustomLicenseCmd { |
| 18 | + return &CustomLicenseCmd{ |
| 19 | + logger: logger, |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +// Execute runs the xmlconvert command |
| 24 | +func (cl *CustomLicenseCmd) Execute() *cobra.Command { |
| 25 | + // Create a new cobra command for xmlconvert |
| 26 | + customLicenseCmd := &cobra.Command{ |
| 27 | + Use: "customLicense", |
| 28 | + Short: "customLicense takes the public url of the license file and stores it in the specified path of the project", |
| 29 | + Long: `The 'customLicense' command retrieves a license file from a specified public URL and stores it in a designated path within your project. By default, it uses 'config.xml' for the license URL and 'config.yaml' for the project path, ensuring easy integration and updates of license files. This command includes pre-run validation and customizable flags for flexible usage.`, |
| 30 | + Example: exampleCommand, |
| 31 | + PreRun: cl.preRun, // Define a pre-run function |
| 32 | + Run: cl.run, // Define the run function |
| 33 | + } |
| 34 | + |
| 35 | + // Define the flags for the xmlconvert command |
| 36 | + customLicenseCmd.Flags().StringVar(&path, "path", path, "please specify the public url of the license file. The default path is ``") |
| 37 | + customLicenseCmd.Flags().StringVar(&projectPath, "projectPath", projectPath, "Provide the project path to store the license file. The default path is ``") |
| 38 | + |
| 39 | + return customLicenseCmd |
| 40 | +} |
| 41 | + |
| 42 | +func (cl *CustomLicenseCmd) preRun(cmd *cobra.Command, args []string) { |
| 43 | + // Do any pre-run setup here |
| 44 | + yellow := "\033[33m" |
| 45 | + reset := "\033[0m" |
| 46 | + text := "WARNING: This command is in alpha version and may need some changes." |
| 47 | + cl.logger.Println(yellow + text + reset) |
| 48 | +} |
| 49 | + |
| 50 | +func (cl *CustomLicenseCmd) run(cmd *cobra.Command, args []string) { |
| 51 | + // Ensure path is set |
| 52 | + if path == "" { |
| 53 | + cl.logger.Fatal("Path to the license file URL must be specified") |
| 54 | + } |
| 55 | + |
| 56 | + // Use the current working directory if projectPath is not provided |
| 57 | + if projectPath == "" { |
| 58 | + cwd, err := os.Getwd() |
| 59 | + if err != nil { |
| 60 | + cl.logger.Fatalf("Failed to get the current working directory: %v", err) |
| 61 | + } |
| 62 | + projectPath = filepath.Join(cwd, "LICENSE") |
| 63 | + } |
| 64 | + |
| 65 | + // Log the start of the process |
| 66 | + cl.logger.Println("Starting to download the license file from:", path) |
| 67 | + |
| 68 | + // Create the HTTP request to fetch the license file |
| 69 | + response, err := http.Get(path) |
| 70 | + if err != nil { |
| 71 | + cl.logger.Fatalf("Failed to download the license file: %v", err) |
| 72 | + } |
| 73 | + defer response.Body.Close() |
| 74 | + |
| 75 | + // Check if the HTTP request was successful |
| 76 | + if response.StatusCode != http.StatusOK { |
| 77 | + cl.logger.Fatalf("Failed to download the license file, HTTP Status: %s", response.Status) |
| 78 | + } |
| 79 | + |
| 80 | + // Create the destination directory if it does not exist |
| 81 | + err = os.MkdirAll(filepath.Dir(projectPath), os.ModePerm) |
| 82 | + if err != nil { |
| 83 | + cl.logger.Fatalf("Failed to create the destination directory: %v", err) |
| 84 | + } |
| 85 | + |
| 86 | + // Create the destination file |
| 87 | + outFile, err := os.Create(projectPath) |
| 88 | + if err != nil { |
| 89 | + cl.logger.Fatalf("Failed to create the destination file: %v", err) |
| 90 | + } |
| 91 | + defer outFile.Close() |
| 92 | + |
| 93 | + // Copy the content from the response to the file |
| 94 | + _, err = io.Copy(outFile, response.Body) |
| 95 | + if err != nil { |
| 96 | + cl.logger.Fatalf("Failed to save the license file: %v", err) |
| 97 | + } |
| 98 | + |
| 99 | + // Log the success of the operation |
| 100 | + cl.logger.Println("License file successfully downloaded and stored at:", projectPath) |
| 101 | +} |
0 commit comments