Skip to content

Commit bc69872

Browse files
committed
urly tweaks for empty command line
1 parent ab41245 commit bc69872

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

cmd/urly/urly.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import (
44
_ "embed"
55
"encoding/json"
66
"fmt"
7+
"io"
78
"net/url"
89
"os"
910
"strings"
1011

12+
"github.com/mattn/go-isatty"
1113
"github.com/spf13/pflag"
1214
)
1315

@@ -177,11 +179,25 @@ func main() {
177179
if len(args) > 1 {
178180
fmt.Fprintf(os.Stderr, "WARNING: Ignoring extra arguments (count=%d)\n", len(args)-1)
179181
}
182+
} else if !isatty.IsTerminal(os.Stdin.Fd()) {
183+
bytesUrl, readErr := io.ReadAll(os.Stdin)
184+
if readErr != nil {
185+
fmt.Fprintf(os.Stderr, "ERROR: Unable to read URL from stdin: %v\n", readErr)
186+
os.Exit(1)
187+
}
188+
strUrl := strings.TrimSpace(string(bytesUrl))
189+
theUrl, parseErr = url.Parse(strUrl)
190+
if parseErr != nil {
191+
fmt.Fprintf(os.Stderr, "ERROR: Unable to parse URL from stdin: %v\n", parseErr)
192+
os.Exit(1)
193+
}
180194
}
181195
}
182196

183197
if theUrl == nil {
184-
theUrl = &url.URL{}
198+
fmt.Fprintf(os.Stderr, "usage: urly [options] <url>\n")
199+
pflag.PrintDefaults()
200+
os.Exit(1)
185201
}
186202

187203
if *noScheme {
@@ -318,7 +334,7 @@ func main() {
318334
fmt.Print(password)
319335
}
320336
}
321-
case "host":
337+
case "host", "hostname":
322338
fmt.Print(theUrl.Hostname())
323339
case "port":
324340
fmt.Print(theUrl.Port())

0 commit comments

Comments
 (0)