Advertisement
deim

Untitled

Aug 20th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.42 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.   "os"
  5.   "fmt"
  6.   "net/url"
  7.   "strings"
  8.   "unicode"
  9. )
  10.  
  11. func main() {
  12.   if len(os.Args) == 1 {
  13.     fmt.Printf("script download|list website|database\n")
  14.   } else if os.Args[1] == "download" && len(os.Args) == 3 {
  15.     content_type, name := defineWebsite(os.Args[2])
  16.     fmt.Println("downloading", content_type, name)
  17.   } else if os.Args[1] == "download" && len(os.Args) != 3 {
  18.     fmt.Println("Download requires one argument")
  19.   } else if os.Args[1] == "list" && len(os.Args) == 3 {
  20.     content_type, name := defineWebsite(os.Args[2])
  21.     fmt.Println("listing", content_type, name)
  22.   } else if os.Args[1] == "list" && len(os.Args) != 3 {
  23.     fmt.Println("List requires one argument")
  24.   } else {
  25.     fmt.Println("ingorrect argument", os.Args[1])
  26.   }
  27. }
  28.  
  29. func disambiguate(a string) (bool, string) { // true if website, false if db
  30.   for _, r := range a {
  31.     if unicode.IsLetter(r) {
  32.       return true, a
  33.     }
  34.   }
  35.   return false, a
  36. }
  37.  
  38. func defineWebsite(a string) (string, string) {
  39.   web, value := disambiguate(a)
  40.   switch web {
  41.   case true:
  42.     switch strings.HasPrefix(value, "http://") || strings.HasPrefix(value, "https://") {
  43.     case true:
  44.       u, err := url.Parse(value)
  45.       if err != nil {
  46.         panic(err)
  47.       }
  48.       return "website", u.Host
  49.     case false:
  50.         return "website", value
  51.     }
  52.   case false:
  53.     return "database", value
  54.   }
  55.   return "a", "b"
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement