|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "path" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/boxboxjason/gitlab-sync/utils" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +type ParserArgs struct { |
| 15 | + SourceGitlabURL string |
| 16 | + SourceGitlabToken string |
| 17 | + DestinationGitlabURL string |
| 18 | + DestinationGitlabToken string |
| 19 | + MirrorMapping utils.MirrorMapping |
| 20 | + Verbose bool |
| 21 | + NoPrompt bool |
| 22 | +} |
| 23 | + |
| 24 | +func main() { |
| 25 | + var args ParserArgs |
| 26 | + var mirrorMappingPath string |
| 27 | + |
| 28 | + var rootCmd = &cobra.Command{ |
| 29 | + Use: "gitlab-sync", |
| 30 | + Short: "Copy and enable mirroring of gitlab projects and groups", |
| 31 | + Long: "Fully customizable gitlab repositories and groups mirroring between two (or one) gitlab instances.", |
| 32 | + Run: func(cmd *cobra.Command, cmdArgs []string) { |
| 33 | + |
| 34 | + utils.SetVerbose(args.Verbose) |
| 35 | + utils.LogVerbose("Verbose mode enabled") |
| 36 | + utils.LogVerbose("Parsing command line arguments") |
| 37 | + |
| 38 | + // Check if the source GitLab URL is provided |
| 39 | + args.SourceGitlabURL = promptForMandatoryInput(args.SourceGitlabURL, "Input Source GitLab URL (MANDATORY)", "Source GitLab URL is mandatory", "Source GitLab URL", args.NoPrompt, false) |
| 40 | + |
| 41 | + // Check if the destination GitLab URL is provided |
| 42 | + args.DestinationGitlabURL = promptForMandatoryInput(args.DestinationGitlabURL, "Input Destination GitLab URL (MANDATORY)", "Destination GitLab URL is mandatory", "Destination GitLab URL", args.NoPrompt, false) |
| 43 | + |
| 44 | + // Check if the Destination GitLab Token is provided |
| 45 | + args.DestinationGitlabToken = promptForMandatoryInput(args.DestinationGitlabToken, "Input Destination GitLab Token with api permissions (MANDATORY)", "Destination GitLab Token is mandatory", "Destination GitLab Token set", args.NoPrompt, true) |
| 46 | + |
| 47 | + // Check if the Mirror Mapping file path is provided |
| 48 | + mirrorMappingPath = promptForMandatoryInput(mirrorMappingPath, "Input Mirror Mapping file path (MANDATORY)", "Mirror Mapping file path is mandatory", "Mirror Mapping file path set", args.NoPrompt, false) |
| 49 | + utils.LogVerbose("Mirror Mapping file resolved path: " + path.Clean(mirrorMappingPath)) |
| 50 | + |
| 51 | + utils.LogVerbose("Parsing mirror mapping file") |
| 52 | + mapping, err := utils.OpenMirrorMapping(mirrorMappingPath) |
| 53 | + if err != nil { |
| 54 | + log.Fatalf("Error opening mirror mapping file: %v", err) |
| 55 | + } |
| 56 | + args.MirrorMapping = *mapping |
| 57 | + utils.LogVerbose("Mirror mapping file parsed successfully") |
| 58 | + }, |
| 59 | + } |
| 60 | + |
| 61 | + rootCmd.Flags().StringVar(&args.SourceGitlabURL, "source-url", os.Getenv("SOURCE_GITLAB_URL"), "Source GitLab URL") |
| 62 | + rootCmd.Flags().StringVar(&args.SourceGitlabToken, "source-token", os.Getenv("SOURCE_GITLAB_TOKEN"), "Source GitLab Token") |
| 63 | + rootCmd.Flags().StringVar(&args.DestinationGitlabURL, "destination-url", os.Getenv("DESTINATION_GITLAB_URL"), "Destination GitLab URL") |
| 64 | + rootCmd.Flags().StringVar(&args.DestinationGitlabToken, "destination-token", os.Getenv("DESTINATION_GITLAB_TOKEN"), "Destination GitLab Token") |
| 65 | + rootCmd.Flags().BoolVar(&args.Verbose, "verbose", false, "Enable verbose output") |
| 66 | + rootCmd.Flags().BoolVar(&args.NoPrompt, "no-prompt", false, "Disable prompting for missing values") |
| 67 | + rootCmd.Flags().StringVar(&mirrorMappingPath, "mirror-mapping", os.Getenv("MIRROR_MAPPING"), "Path to the mirror mapping file") |
| 68 | + |
| 69 | + if err := rootCmd.Execute(); err != nil { |
| 70 | + fmt.Println(err) |
| 71 | + os.Exit(1) |
| 72 | + } |
| 73 | + |
| 74 | +} |
| 75 | + |
| 76 | +func promptForInput(prompt string) string { |
| 77 | + var input string |
| 78 | + fmt.Printf("%s: ", prompt) |
| 79 | + fmt.Scanln(&input) |
| 80 | + return input |
| 81 | +} |
| 82 | + |
| 83 | +func promptForMandatoryInput(defaultValue string, prompt string, errorMsg string, loggerMsg string, promptsEnabled bool, hideOutput bool) string { |
| 84 | + input := strings.TrimSpace(defaultValue) |
| 85 | + if input == "" { |
| 86 | + if promptsEnabled { |
| 87 | + input = strings.TrimSpace(promptForInput(prompt)) |
| 88 | + if input == "" { |
| 89 | + log.Fatal(errorMsg) |
| 90 | + } |
| 91 | + if !hideOutput { |
| 92 | + utils.LogVerbose(loggerMsg + ": " + input) |
| 93 | + } else { |
| 94 | + utils.LogVerbose(loggerMsg) |
| 95 | + } |
| 96 | + } else { |
| 97 | + log.Fatalf("Prompting is disabled, %s", errorMsg) |
| 98 | + } |
| 99 | + } |
| 100 | + return input |
| 101 | +} |
0 commit comments