Skip to content

Commit 87ea5d0

Browse files
committed
urly: alias user to username
1 parent f4ba0be commit 87ea5d0

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

cmd/urly/urly.go

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@ import (
77
"os"
88
"strings"
99

10-
"github.com/FileFormatInfo/fftools/internal"
1110
"github.com/spf13/pflag"
1211
)
1312

13+
var (
14+
BUILDER = "unknown"
15+
COMMIT = "(local)"
16+
LASTMOD = "(local)"
17+
VERSION = "internal"
18+
)
19+
1420
func setUserName(userInfo *url.Userinfo, username string) *url.Userinfo {
1521
if userInfo != nil {
1622
password, hasPassword := userInfo.Password()
@@ -86,6 +92,15 @@ func toJson(theUrl *url.URL, pretty bool) string {
8692
return string(jsonStr)
8793
}
8894

95+
func aliasFunc(f *pflag.FlagSet, name string) pflag.NormalizedName {
96+
switch name {
97+
case "user":
98+
name = "username"
99+
break
100+
}
101+
return pflag.NormalizedName(name)
102+
}
103+
89104
// Detailed help text
90105
var helpText = `urly: A URL parsing and processing tool.`
91106

@@ -119,16 +134,17 @@ func main() {
119134

120135
var envUrl = pflag.String("url-env", "", "Environment variable containing the URL to process")
121136

122-
var output = pflag.String("output", "url", "Output type: url, scheme, host, port, path, query, fragment, userinfo, username, password")
137+
var output = pflag.String("output", "url", "Output type: url, scheme, host, port, path, query, fragment, userinfo, username, password, segments, segment[N]")
123138
var newline = pflag.Bool("newline", false, "Append newline to output")
124139

125140
var help = pflag.Bool("help", false, "Detailed help")
126141
var version = pflag.Bool("version", false, "Version info")
127142

143+
pflag.CommandLine.SetNormalizeFunc(aliasFunc)
128144
pflag.Parse()
129145

130146
if *version {
131-
internal.PrintVersion("urly")
147+
fmt.Printf("urly version %s (built on %s from %s by %s)\n", VERSION, LASTMOD, COMMIT, BUILDER)
132148
return
133149
}
134150

@@ -291,7 +307,7 @@ func main() {
291307
fmt.Print(theUrl.String())
292308
case "scheme":
293309
fmt.Print(theUrl.Scheme)
294-
case "username":
310+
case "username", "user":
295311
if theUrl.User != nil {
296312
fmt.Print(theUrl.User.Username())
297313
}
@@ -325,9 +341,27 @@ func main() {
325341
fmt.Print(toJson(theUrl, true))
326342
case "jsonl":
327343
fmt.Print(toJson(theUrl, false))
344+
case "segments":
345+
fmt.Print(theUrl.Path[1:]) // remove leading slash
328346
default:
329-
fmt.Fprintf(os.Stderr, "ERROR: Unknown output type: %s\n", *output)
330-
os.Exit(1)
347+
if strings.HasPrefix(*output, "segment[") && strings.HasSuffix(*output, "]") {
348+
indexStr := (*output)[len("segment[") : len(*output)-1]
349+
var index int
350+
_, err := fmt.Sscanf(indexStr, "%d", &index)
351+
if err != nil {
352+
fmt.Fprintf(os.Stderr, "ERROR: Invalid segment index: %s\n", indexStr)
353+
os.Exit(1)
354+
}
355+
segments := strings.Split(theUrl.Path, "/")
356+
if index < 0 || index >= len(segments) {
357+
fmt.Fprintf(os.Stderr, "ERROR: Segment index out of range: %d\n", index)
358+
os.Exit(1)
359+
}
360+
fmt.Print(segments[index])
361+
} else {
362+
fmt.Fprintf(os.Stderr, "ERROR: Unknown output type: %s\n", *output)
363+
os.Exit(1)
364+
}
331365
}
332366
if *newline {
333367
fmt.Println()

testdata/urly.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ stdout 'https'
3636
exec urly --output=username https://username:pswd@host.example.com/path/to/file?querykey=value#fragment
3737
stdout 'username'
3838

39+
# get the username (via alias)
40+
exec urly --output=user https://username:pswd@host.example.com/path/to/file?querykey=value#fragment
41+
stdout 'username'
42+
3943
# get the password
4044
exec urly --output=password https://user:pswd@host.example.com/path/to/file?querykey=value#fragment
4145
stdout 'pswd'
@@ -56,6 +60,18 @@ stdout 'querykey=value&key2=val2'
5660
exec urly --output=fragment 'https://user:pswd@host.example.com/path/to/file?querykey=value&key2=val2#fraggers'
5761
stdout 'fraggers'
5862

63+
# set the username
64+
exec urly --output=url --username=newuser 'https://olduser:pswd@host.example.com/path/to/file'
65+
stdout 'https://newuser:pswd@host.example.com/path/to/file'
66+
67+
# set the username (no password)
68+
exec urly --output=url --username=newuser 'https://olduser@host.example.com/path/to/file'
69+
stdout 'https://newuser@host.example.com/path/to/file'
70+
71+
# set the username (via alias)
72+
exec urly --output=url --user=newuser 'https://olduser@host.example.com/path/to/file'
73+
stdout 'https://newuser@host.example.com/path/to/file'
74+
5975
# set a parameter
6076
exec urly --output=query --setparam p=q 'https://user:pswd@host.example.com/path/to/file?querykey=value&key2=val2'
6177
stdout 'key2=val2&p=q&querykey=value'

0 commit comments

Comments
 (0)