|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/url" |
| 7 | + "os" |
| 8 | + |
| 9 | + "github.com/FileFormatInfo/fftools/internal" |
| 10 | + "github.com/spf13/pflag" |
| 11 | +) |
| 12 | + |
| 13 | +func setUserName(userInfo *url.Userinfo, username string) *url.Userinfo { |
| 14 | + if userInfo != nil { |
| 15 | + password, hasPassword := userInfo.Password() |
| 16 | + if hasPassword { |
| 17 | + return url.UserPassword(username, password) |
| 18 | + } else { |
| 19 | + return url.User(username) |
| 20 | + } |
| 21 | + } else { |
| 22 | + return url.User(username) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func setPassword(userInfo *url.Userinfo, password string) *url.Userinfo { |
| 27 | + if userInfo != nil { |
| 28 | + username := userInfo.Username() |
| 29 | + return url.UserPassword(username, password) |
| 30 | + } else { |
| 31 | + return url.UserPassword("", password) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +type UrlJson struct { |
| 36 | + Scheme string `json:"scheme"` |
| 37 | + Host string `json:"host"` |
| 38 | + Port string `json:"port"` |
| 39 | + Path string `json:"path"` |
| 40 | + Query string `json:"query"` |
| 41 | + Fragment string `json:"fragment"` |
| 42 | + Username string `json:"username"` |
| 43 | + Password string `json:"password"` |
| 44 | + Url string `json:"url"` |
| 45 | + Params map[string][]string `json:"params,omitempty"` |
| 46 | +} |
| 47 | + |
| 48 | +func toJson(theUrl *url.URL) string { |
| 49 | + urlJson := UrlJson{ |
| 50 | + Scheme: theUrl.Scheme, |
| 51 | + Host: theUrl.Hostname(), |
| 52 | + Port: theUrl.Port(), |
| 53 | + Path: theUrl.Path, |
| 54 | + Query: theUrl.RawQuery, |
| 55 | + Fragment: theUrl.Fragment, |
| 56 | + Url: theUrl.String(), |
| 57 | + Params: theUrl.Query(), |
| 58 | + } |
| 59 | + if theUrl.User != nil { |
| 60 | + urlJson.Username = theUrl.User.Username() |
| 61 | + password, hasPassword := theUrl.User.Password() |
| 62 | + if hasPassword { |
| 63 | + urlJson.Password = password |
| 64 | + } |
| 65 | + } |
| 66 | + jsonStr, err := json.Marshal(urlJson) |
| 67 | + if err != nil { |
| 68 | + fmt.Fprintf(os.Stderr, "ERROR: unable to marshal URL to JSON: %v\n", err) |
| 69 | + os.Exit(1) |
| 70 | + } |
| 71 | + return string(jsonStr) |
| 72 | +} |
| 73 | + |
| 74 | +// Detailed help text |
| 75 | +var helpText = `urly: A URL parsing and processing tool.` |
| 76 | + |
| 77 | +func main() { |
| 78 | + |
| 79 | + var scheme = pflag.String("scheme", "", "Set the URL scheme") |
| 80 | + var envPassword = pflag.String("password-env", "", "Environment variable containing the password for URL processing") |
| 81 | + var stdinPassword = pflag.Bool("password-stdin", false, "Read password from standard input") |
| 82 | + var envUrl = pflag.String("url-env", "", "Environment variable containing the URL to process") |
| 83 | + var envUsername = pflag.String("username-env", "", "Environment variable containing the username for URL processing") |
| 84 | + var textUsername = pflag.String("username", "", "Username for URL processing") |
| 85 | + //LATER: var format = pflag.String("format", "text", "Output format: text or json") |
| 86 | + var output = pflag.String("output", "url", "Output type: url, scheme, host, port, path, query, fragment, userinfo, username, password") |
| 87 | + var newline = pflag.Bool("newline", false, "Append newline to output") |
| 88 | + var help = pflag.Bool("help", false, "Detailed help") |
| 89 | + var version = pflag.Bool("version", false, "Version info") |
| 90 | + |
| 91 | + pflag.Parse() |
| 92 | + |
| 93 | + if *version { |
| 94 | + internal.PrintVersion("urly") |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + if *help { |
| 99 | + fmt.Printf("%s\n", helpText) |
| 100 | + return |
| 101 | + } |
| 102 | + |
| 103 | + var theUrl *url.URL |
| 104 | + var parseErr error |
| 105 | + |
| 106 | + if *envUrl != "" { |
| 107 | + textUrl := os.Getenv(*envUrl) |
| 108 | + theUrl, parseErr = url.Parse(textUrl) |
| 109 | + if parseErr != nil { |
| 110 | + fmt.Fprintf(os.Stderr, "ERROR: unable to parse URL from environment variable %s: %v\n", *envUrl, parseErr) |
| 111 | + os.Exit(1) |
| 112 | + } |
| 113 | + } else { |
| 114 | + args := pflag.Args() |
| 115 | + if len(args) > 0 { |
| 116 | + theUrl, parseErr = url.Parse(args[0]) |
| 117 | + if parseErr != nil { |
| 118 | + fmt.Fprintf(os.Stderr, "ERROR: Unable to parse URL from argument: %v\n", parseErr) |
| 119 | + os.Exit(1) |
| 120 | + } |
| 121 | + if len(args) > 1 { |
| 122 | + fmt.Fprintf(os.Stderr, "WARNING: Ignoring extra arguments (count=%d)\n", len(args)-1) |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + if theUrl == nil { |
| 128 | + theUrl = &url.URL{} |
| 129 | + } |
| 130 | + |
| 131 | + if *envUsername != "" { |
| 132 | + theUrl.User = setUserName(theUrl.User, os.Getenv(*envUsername)) |
| 133 | + } else if *textUsername != "" { |
| 134 | + theUrl.User = setUserName(theUrl.User, *textUsername) |
| 135 | + } |
| 136 | + |
| 137 | + if *envPassword != "" { |
| 138 | + theUrl.User = setPassword(theUrl.User, os.Getenv(*envPassword)) |
| 139 | + } else if *stdinPassword { |
| 140 | + var password string |
| 141 | + _, err := fmt.Fscanln(os.Stdin, &password) |
| 142 | + if err != nil { |
| 143 | + fmt.Fprintf(os.Stderr, "ERROR: Unable to read password from stdin: %v\n", err) |
| 144 | + os.Exit(1) |
| 145 | + } |
| 146 | + theUrl.User = setPassword(theUrl.User, password) |
| 147 | + } |
| 148 | + |
| 149 | + if *scheme != "" { |
| 150 | + theUrl.Scheme = *scheme |
| 151 | + } |
| 152 | + |
| 153 | + switch *output { |
| 154 | + case "url": |
| 155 | + fmt.Println(theUrl.String()) |
| 156 | + case "scheme": |
| 157 | + fmt.Print(theUrl.Scheme) |
| 158 | + case "host": |
| 159 | + fmt.Print(theUrl.Hostname()) |
| 160 | + case "port": |
| 161 | + fmt.Print(theUrl.Port()) |
| 162 | + case "path": |
| 163 | + fmt.Print(theUrl.Path) |
| 164 | + case "query": |
| 165 | + fmt.Print(theUrl.RawQuery) |
| 166 | + case "fragment": |
| 167 | + fmt.Print(theUrl.Fragment) |
| 168 | + case "userinfo": |
| 169 | + if theUrl.User != nil { |
| 170 | + fmt.Print(theUrl.User.Username()) |
| 171 | + password, isSet := theUrl.User.Password() |
| 172 | + if isSet { |
| 173 | + fmt.Print(":") |
| 174 | + fmt.Print(password) |
| 175 | + } |
| 176 | + } |
| 177 | + case "username": |
| 178 | + if theUrl.User != nil { |
| 179 | + fmt.Print(theUrl.User.Username()) |
| 180 | + } |
| 181 | + case "password": |
| 182 | + if theUrl.User != nil { |
| 183 | + password, hasPassword := theUrl.User.Password() |
| 184 | + if hasPassword { |
| 185 | + fmt.Print(password) |
| 186 | + } |
| 187 | + } |
| 188 | + case "json": |
| 189 | + fmt.Print(toJson(theUrl)) |
| 190 | + default: |
| 191 | + fmt.Fprintf(os.Stderr, "ERROR: Unknown output type: %s\n", *output) |
| 192 | + os.Exit(1) |
| 193 | + } |
| 194 | + if *newline { |
| 195 | + fmt.Println() |
| 196 | + } |
| 197 | +} |
0 commit comments