|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "github.com/ByteSizedMarius/rewerse-engineering/pkg" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +func hdl(err error) { |
| 13 | + if err != nil { |
| 14 | + fmt.Println(err) |
| 15 | + os.Exit(1) |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +func main() { |
| 20 | + flag.Usage = func() { |
| 21 | + fmt.Println("Usage: rewerse-cli [flags] [subcommand] [subcommand-flags]") |
| 22 | + fmt.Println("\nFlags:") |
| 23 | + fmt.Println(align("-cert <path>") + "Path to the certificate file (default 'certificate.pem')") |
| 24 | + fmt.Println(align("-key <path>") + "Path to the key file (default 'private.key')") |
| 25 | + fmt.Println(align("-json") + "Output in JSON format (default false)") |
| 26 | + fmt.Println("\nSubcommands:") |
| 27 | + fmt.Println(align("marketsearch -query <query>") + "Search for markets") |
| 28 | + fmt.Println(align("marketdetails -id <market-id>") + "Get details for a market") |
| 29 | + fmt.Println(align("coupons") + "Get coupons") |
| 30 | + fmt.Println(align("recalls") + "Get recalls") |
| 31 | + fmt.Println(align("discounts -id <market-id> [-raw] [-catGroup]") + "Get discounts") |
| 32 | + fmt.Println("\nSubcommand-Flags:") |
| 33 | + fmt.Println(align("-query <query>") + "Search query. Can be a city, postal code, street, etc.") |
| 34 | + fmt.Println(align("-id <market-id>") + "ID of the market or discount. Get it from marketsearch.") |
| 35 | + fmt.Println(align("-raw") + "Raw output format (directly from the API). Otherwise, it's parsed and cleaned based on what I thought was useful.") |
| 36 | + fmt.Println(align("-catGroup") + "Group by product category instead of rewe-app-category") |
| 37 | + fmt.Println("\nExamples:") |
| 38 | + fmt.Println(" rewerse.exe marketsearch -query Köln") |
| 39 | + fmt.Println(" rewerse.exe marketdetails -id 1763153") |
| 40 | + fmt.Println(" rewerse.exe discounts -id 1763153") |
| 41 | + fmt.Println(" rewerse.exe -json discounts -id 1763153 -raw") |
| 42 | + fmt.Println(" rewerse.exe discounts -id 1763153 -catGroup") |
| 43 | + fmt.Println(" rewerse.exe -cert cert.pem -key p.key marketsearch -query Köln") |
| 44 | + } |
| 45 | + certFile := flag.String("cert", "certificate.pem", "Path to the certificate file") |
| 46 | + keyFile := flag.String("key", "private.key", "Path to the key file") |
| 47 | + jsonOutput := flag.Bool("json", false, "Output in JSON format (default false)") |
| 48 | + flag.Parse() |
| 49 | + |
| 50 | + var err error |
| 51 | + err = rewerse.SetCertificate(*certFile, *keyFile) |
| 52 | + hdl(err) |
| 53 | + |
| 54 | + if flag.NArg() == 0 { |
| 55 | + flag.Usage() |
| 56 | + os.Exit(1) |
| 57 | + } |
| 58 | + |
| 59 | + marketSearchCmd := flag.NewFlagSet("marketsearch", flag.ExitOnError) |
| 60 | + marketSearchQuery := marketSearchCmd.String("query", "", "Search query") |
| 61 | + |
| 62 | + marketDetailsCmd := flag.NewFlagSet("marketdetails", flag.ExitOnError) |
| 63 | + marketDetailsID := marketDetailsCmd.String("id", "", "ID of the market details") |
| 64 | + |
| 65 | + discountsCmd := flag.NewFlagSet("discounts", flag.ExitOnError) |
| 66 | + discountsID := discountsCmd.String("id", "", "Market-ID") |
| 67 | + rawOutput := discountsCmd.Bool("raw", false, "Whether to return raw API Data") |
| 68 | + groupProduct := discountsCmd.Bool("catGroup", false, "Group by product category instead of app-category") |
| 69 | + |
| 70 | + var data any |
| 71 | + switch flag.Arg(0) { |
| 72 | + case "marketsearch": |
| 73 | + hdl(marketSearchCmd.Parse(flag.Args()[1:])) |
| 74 | + if *marketSearchQuery == "" { |
| 75 | + fmt.Println("Expected search query") |
| 76 | + os.Exit(1) |
| 77 | + } |
| 78 | + data, err = rewerse.MarketSearch(*marketSearchQuery) |
| 79 | + case "marketdetails": |
| 80 | + hdl(marketDetailsCmd.Parse(flag.Args()[1:])) |
| 81 | + if *marketDetailsID == "" { |
| 82 | + fmt.Println("Expected market ID") |
| 83 | + os.Exit(1) |
| 84 | + } |
| 85 | + data, err = rewerse.GetMarketDetails(*marketDetailsID) |
| 86 | + case "coupons": |
| 87 | + var oc rewerse.OneScanCoupons |
| 88 | + var c rewerse.Coupons |
| 89 | + oc, c, err = rewerse.GetCoupons() |
| 90 | + data = []any{oc, c} |
| 91 | + case "recalls": |
| 92 | + data, err = rewerse.GetRecalls() |
| 93 | + case "discounts": |
| 94 | + hdl(discountsCmd.Parse(flag.Args()[1:])) |
| 95 | + if *rawOutput { |
| 96 | + data, err = rewerse.GetDiscountsRaw(*discountsID) |
| 97 | + *jsonOutput = true |
| 98 | + } else { |
| 99 | + data, err = rewerse.GetDiscounts(*discountsID) |
| 100 | + } |
| 101 | + if *groupProduct { |
| 102 | + data = data.(rewerse.Discounts).GroupByProductCategory() |
| 103 | + } |
| 104 | + |
| 105 | + default: |
| 106 | + fmt.Println("Expected 'marketsearch', 'marketdetails', 'coupons', 'recalls' or 'discounts' subcommands") |
| 107 | + os.Exit(1) |
| 108 | + } |
| 109 | + |
| 110 | + hdl(err) |
| 111 | + if *jsonOutput { |
| 112 | + var bt []byte |
| 113 | + bt, err = json.MarshalIndent(data, "", " ") |
| 114 | + hdl(err) |
| 115 | + data = string(bt) |
| 116 | + } |
| 117 | + fmt.Println(data) |
| 118 | +} |
| 119 | + |
| 120 | +func align(s string) string { |
| 121 | + return " " + s + strings.Repeat(" ", 40-len(s)) |
| 122 | +} |
0 commit comments