parse flags from the cli, do some checks and return a config struct
()
| 23 | |
| 24 | // parse flags from the cli, do some checks and return a config struct |
| 25 | func parseFlags() config { |
| 26 | cert := flag.String("cert", "cert.pem", "path to certificate file") |
| 27 | key := flag.String("key", "key.pem", "path to key file") |
| 28 | ip := flag.String("ip", "", "android tv ip") |
| 29 | version := flag.Int("version", 1, "remote protocol version [1 or 2]") |
| 30 | pair := flag.Bool("pair", false, "pairing mode") |
| 31 | command := flag.String("command", "", "command to execute") |
| 32 | open := flag.String("open", "", "app activity to run (only for v1)") |
| 33 | link := flag.String("link", "", "link to open (only for v2)") |
| 34 | info := flag.Bool("info", false, "print device info (only for v2)") |
| 35 | |
| 36 | flag.Parse() |
| 37 | |
| 38 | if *ip == "" { |
| 39 | flag.Usage() |
| 40 | log.Fatalf("please provide the ip of your android tv device") |
| 41 | } |
| 42 | |
| 43 | err := checkPath(*cert, *key) |
| 44 | if *pair { |
| 45 | if err != nil { |
| 46 | err := common.CreateCertificate("androidtv-remote", []string{*ip}, *cert, *key) |
| 47 | if err != nil { |
| 48 | log.Fatalf("unable to create certificate: %s", err) |
| 49 | } |
| 50 | } |
| 51 | } else { |
| 52 | if err != nil { |
| 53 | flag.Usage() |
| 54 | log.Fatal(err) |
| 55 | } |
| 56 | // check flags |
| 57 | if *version == 1 { |
| 58 | if *link != "" { |
| 59 | flag.Usage() |
| 60 | log.Fatalf("flag -link is invalid for protocol v1") |
| 61 | } |
| 62 | if *info { |
| 63 | flag.Usage() |
| 64 | log.Fatalf("flag -info is invalid for protocol v1") |
| 65 | } |
| 66 | } else { |
| 67 | if *open != "" { |
| 68 | flag.Usage() |
| 69 | log.Fatalf("flag -open is invalid for protocol v2") |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | certs, err := tls.LoadX509KeyPair(*cert, *key) |
| 75 | if err != nil { |
| 76 | log.Fatalf("unable to load certificates: %s", err) |
| 77 | } |
| 78 | |
| 79 | return config{Version: *version, Certificates: certs, IP: *ip, Pair: *pair, Command: common.ParseKeys(*command), Open: *open, Link: *link, Info: *info} |
| 80 | } |
| 81 | |
| 82 | // check if the paths to the certs exists |
no test coverage detected