Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "os"
- "fmt"
- "net/url"
- "strings"
- "unicode"
- )
- func main() {
- if len(os.Args) == 1 {
- fmt.Printf("script download|list website|database\n")
- } else if os.Args[1] == "download" && len(os.Args) == 3 {
- content_type, name := defineWebsite(os.Args[2])
- fmt.Println("downloading", content_type, name)
- } else if os.Args[1] == "download" && len(os.Args) != 3 {
- fmt.Println("Download requires one argument")
- } else if os.Args[1] == "list" && len(os.Args) == 3 {
- content_type, name := defineWebsite(os.Args[2])
- fmt.Println("listing", content_type, name)
- } else if os.Args[1] == "list" && len(os.Args) != 3 {
- fmt.Println("List requires one argument")
- } else {
- fmt.Println("ingorrect argument", os.Args[1])
- }
- }
- func disambiguate(a string) (bool, string) { // true if website, false if db
- for _, r := range a {
- if unicode.IsLetter(r) {
- return true, a
- }
- }
- return false, a
- }
- func defineWebsite(a string) (string, string) {
- web, value := disambiguate(a)
- switch web {
- case true:
- switch strings.HasPrefix(value, "http://") || strings.HasPrefix(value, "https://") {
- case true:
- u, err := url.Parse(value)
- if err != nil {
- panic(err)
- }
- return "website", u.Host
- case false:
- return "website", value
- }
- case false:
- return "database", value
- }
- return "a", "b"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement