|
| 1 | +// |
| 2 | +// urldecode.go is a simple command line utility to decode a string in a URL friendly way. |
| 3 | +// |
| 4 | +// @author R. S. Doiel, <rsdoiel@caltech.edu> |
| 5 | +// |
| 6 | +// Copyright (c) 2021, Caltech |
| 7 | +// All rights not granted herein are expressly reserved by Caltech. |
| 8 | +// |
| 9 | +// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: |
| 10 | +// |
| 11 | +// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. |
| 12 | +// |
| 13 | +// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. |
| 14 | +// |
| 15 | +// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. |
| 16 | +// |
| 17 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 18 | +// |
| 19 | +package main |
| 20 | + |
| 21 | +import ( |
| 22 | + "flag" |
| 23 | + "fmt" |
| 24 | + "io/ioutil" |
| 25 | + "net/url" |
| 26 | + "os" |
| 27 | + "path" |
| 28 | + "strings" |
| 29 | + |
| 30 | + // CaltechLibrary Packages |
| 31 | + "github.com/caltechlibrary/datatools" |
| 32 | +) |
| 33 | + |
| 34 | +var ( |
| 35 | + helpText = `%{app_name}(1) user manual | version {version} {release_hash} |
| 36 | +% R. S. Doiel |
| 37 | +% {release_date} |
| 38 | +
|
| 39 | +# NAME |
| 40 | +
|
| 41 | +{app_name} |
| 42 | +
|
| 43 | +# SYNOPSIS |
| 44 | +
|
| 45 | +{app_name} [OPTIONS] [URL_ENCODED_STRING] |
| 46 | +
|
| 47 | +# DESCRIPTION |
| 48 | +
|
| 49 | +{app_name} is a simple command line utility to URL decode content. By default |
| 50 | +it reads from standard input and writes to standard out. You can |
| 51 | +also specifty the string to decode as a command line parameter. |
| 52 | +
|
| 53 | +You can provide the URL encoded string as a command line parameter or if none |
| 54 | +present it will be read from standard input. |
| 55 | +
|
| 56 | +# OPTIONS |
| 57 | +
|
| 58 | +-help |
| 59 | +: display help |
| 60 | +
|
| 61 | +-license |
| 62 | +: display license |
| 63 | +
|
| 64 | +-version |
| 65 | +: display version |
| 66 | +
|
| 67 | +-query |
| 68 | +: use query escape (pluses for spaces) |
| 69 | +
|
| 70 | +-newline |
| 71 | +: Append a trailing newline |
| 72 | +
|
| 73 | +# EXAMPLE |
| 74 | +
|
| 75 | +echo 'This%20is%20the%20string%20to%20encode%20&%20nothing%20else%0A' | {app_name} |
| 76 | +
|
| 77 | +would yield (without the double quotes) |
| 78 | +
|
| 79 | + "This is the string to encode & nothing else!" |
| 80 | +
|
| 81 | +` |
| 82 | + |
| 83 | + // Standard Options |
| 84 | + showHelp bool |
| 85 | + showLicense bool |
| 86 | + showVersion bool |
| 87 | + |
| 88 | + // App Options |
| 89 | + useQueryUnescape bool |
| 90 | + newLine bool |
| 91 | +) |
| 92 | + |
| 93 | +func main() { |
| 94 | + appName := path.Base(os.Args[0]) |
| 95 | + version := datatools.Version |
| 96 | + license := datatools.LicenseText |
| 97 | + releaseDate := datatools.ReleaseDate |
| 98 | + releaseHash := datatools.ReleaseHash |
| 99 | + |
| 100 | + // Standard Options |
| 101 | + flag.BoolVar(&showHelp, "help", showHelp, "display help") |
| 102 | + flag.BoolVar(&showLicense, "license", showLicense, "display license") |
| 103 | + flag.BoolVar(&showVersion, "version", showVersion, "display version") |
| 104 | + flag.BoolVar(&newLine, "newline", false, "append a newline character") |
| 105 | + flag.BoolVar(&useQueryUnescape, "query", false, "use query escape (pluses for spaces)") |
| 106 | + |
| 107 | + // Parse environment and options |
| 108 | + flag.Parse() |
| 109 | + args := flag.Args() |
| 110 | + |
| 111 | + // Setup IO |
| 112 | + var err error |
| 113 | + |
| 114 | + in := os.Stdin |
| 115 | + out := os.Stdout |
| 116 | + eout := os.Stderr |
| 117 | + |
| 118 | + // Handle the default options |
| 119 | + if showHelp { |
| 120 | + fmt.Fprintf(out, "%s\n", datatools.FmtHelp(helpText, appName, version, releaseDate, releaseHash)) |
| 121 | + os.Exit(0) |
| 122 | + } |
| 123 | + if showLicense == true { |
| 124 | + fmt.Fprintf(out, "%s\n", license) |
| 125 | + os.Exit(0) |
| 126 | + } |
| 127 | + if showVersion == true { |
| 128 | + fmt.Fprintf(out, "datatools, %s %s %s\n", appName, version, releaseHash) |
| 129 | + os.Exit(0) |
| 130 | + } |
| 131 | + |
| 132 | + nl := "\n" |
| 133 | + if newLine == false { |
| 134 | + nl = "" |
| 135 | + } |
| 136 | + |
| 137 | + var ( |
| 138 | + src string |
| 139 | + s string |
| 140 | + ) |
| 141 | + |
| 142 | + if len(args) > 0 { |
| 143 | + src = strings.Join(args, " ") |
| 144 | + } else { |
| 145 | + buf, err := ioutil.ReadAll(in) |
| 146 | + if err != nil { |
| 147 | + fmt.Fprintf(eout, "%s\n", err) |
| 148 | + os.Exit(1) |
| 149 | + } |
| 150 | + src = fmt.Sprintf("%s", buf) |
| 151 | + } |
| 152 | + if useQueryUnescape { |
| 153 | + s, err = url.QueryUnescape(src) |
| 154 | + } else { |
| 155 | + s, err = url.PathUnescape(src) |
| 156 | + } |
| 157 | + if err != nil { |
| 158 | + fmt.Fprintf(eout, "%s\n", err) |
| 159 | + os.Exit(1) |
| 160 | + } |
| 161 | + fmt.Fprintf(out, "%s%s", s, nl) |
| 162 | +} |
0 commit comments