|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | +) |
| 10 | + |
| 11 | +// Command Line flags |
| 12 | +var ( |
| 13 | + username string // Username for Audisto API authentication |
| 14 | + password string // Password for audisto API authentication |
| 15 | + crawlID uint64 // ID of the crawl to download |
| 16 | + chunkNumber uint64 // Number of Chunk |
| 17 | + chunkSize uint64 // Elements in each chunk |
| 18 | + output string // Output format |
| 19 | + filter string // Possible filter |
| 20 | + noResume bool // Resume or not any previously downloaded file |
| 21 | + noDetails bool // Request or not details from Audisto API |
| 22 | + order string // Possible order of results |
| 23 | + mode string // pages or links |
| 24 | + targets string // "self" or a path to a file containing link target pages (IDs) |
| 25 | +) |
| 26 | + |
| 27 | +// register global flags that apply to the root command |
| 28 | +func registerPersistentFlags(rootCmd *cobra.Command) { |
| 29 | + pf := rootCmd.PersistentFlags() |
| 30 | + pf.StringVarP(&username, "username", "u", "", "Audisto API Username (required)") |
| 31 | + pf.StringVarP(&password, "password", "p", "", "Audisto API Password (required)") |
| 32 | + pf.Uint64VarP(&crawlID, "crawl", "c", 0, "ID of the crawl to download (required)") |
| 33 | + pf.StringVarP(&mode, "mode", "m", "pages", "Download mode, set it to 'links' or 'pages' (default)") |
| 34 | + pf.BoolVarP(&noDetails, "no-details", "d", false, "If passed, details in API request is set to 0") |
| 35 | + pf.StringVarP(&output, "output", "o", "", "Path for the output file") |
| 36 | + pf.BoolVarP(&noResume, "no-resume", "r", false, "If passed, download starts again, else the download is resumed") |
| 37 | + pf.StringVarP(&filter, "filter", "f", "", "Filter all pages by some attributes") |
| 38 | + pf.StringVarP(&order, "order", "", "", "Order by some attributes") |
| 39 | + pf.StringVarP(&targets, "targets", "t", "", `"self" or a path to a file containing link target pages (IDs)`) |
| 40 | +} |
| 41 | + |
| 42 | +// check if --username --password and --crawl are being passed with non-empty values |
| 43 | +func requiredFlagsPassed() bool { |
| 44 | + return username != "" && password != "" && crawlID != 0 |
| 45 | +} |
| 46 | + |
| 47 | +// Beside parsing flags and auto-type inferring offered by Cobra package |
| 48 | +// we check for our own flag validations/logic as well |
| 49 | +func customFlagsValidation(cmd *cobra.Command) error { |
| 50 | + |
| 51 | + // make sure required flags are passed |
| 52 | + if !requiredFlagsPassed() { |
| 53 | + return CError("--username, --password and --crawl are required") |
| 54 | + } |
| 55 | + |
| 56 | + // normalize flags before proceeding with the validation |
| 57 | + normalizeFlags() |
| 58 | + |
| 59 | + // validate mode |
| 60 | + if mode != "" && mode != "pages" && mode != "links" { |
| 61 | + msg := "mode has to be 'links' or 'pages', if this flag is dropped, it will default to 'pages'" |
| 62 | + return CError(msg) |
| 63 | + } |
| 64 | + |
| 65 | + // validate targets / mode / filter combinations |
| 66 | + if targets != "" { |
| 67 | + |
| 68 | + // do not allow --filter when --targets is being used |
| 69 | + if filter != "" { |
| 70 | + return CError("Set either --filter or --targets, but not both") |
| 71 | + } |
| 72 | + |
| 73 | + // --mode=pages is only allowed when targets=self |
| 74 | + if targets == "self" && mode != "pages" { |
| 75 | + return CError("Set --mode=pages to use --targets=self") |
| 76 | + } |
| 77 | + |
| 78 | + // --targets=FILEPATH is only allowed when mode is set to links |
| 79 | + // we'd also make sure the file exists. |
| 80 | + if targets != "self" { |
| 81 | + |
| 82 | + if mode != "links" { |
| 83 | + return CError("Set --mode=links to use --targets=FILEPATH") |
| 84 | + } |
| 85 | + |
| 86 | + if _, err := os.Stat(targets); os.IsNotExist(err) { |
| 87 | + return CError(fmt.Sprintf("%s file does not exist", targets)) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + } |
| 92 | + // returning no error means the validation passed |
| 93 | + return nil |
| 94 | +} |
| 95 | + |
| 96 | +// trim spaces and lowercase [some] string-based flags |
| 97 | +func normalizeFlags() { |
| 98 | + |
| 99 | + // trim spaces for 'mode', 'targets', 'output', 'filter' and 'order' |
| 100 | + mode = strings.TrimSpace(mode) |
| 101 | + targets = strings.TrimSpace(targets) |
| 102 | + output = strings.TrimSpace(output) |
| 103 | + filter = strings.TrimSpace(filter) |
| 104 | + order = strings.TrimSpace(order) |
| 105 | + |
| 106 | + // lowercase 'mode' |
| 107 | + mode = strings.ToLower(mode) |
| 108 | + |
| 109 | + // lowercase 'targets' when it's being set to 'self' |
| 110 | + if strings.EqualFold(targets, "self") { |
| 111 | + targets = "self" |
| 112 | + } |
| 113 | +} |
0 commit comments