bb/tool/urlprs/urlprs.go

54 lines
809 B
Go
Raw Normal View History

2020-10-19 03:07:41 +03:00
package urlprs
import (
"os"
2020-10-19 03:07:41 +03:00
"fmt"
"net"
"net/url"
2024-01-04 17:13:03 +03:00
"github.com/vultras/cli/mtool"
2020-10-19 03:07:41 +03:00
)
var nilStr = "_"
func printNil(s string) {
if len(s) != 0 {
fmt.Println(s)
} else {
fmt.Println(nilStr)
}
}
2023-03-24 16:54:51 +03:00
func Run(flags *mtool.Flags) {
2020-10-19 03:07:41 +03:00
nilStr = "_"
2023-03-24 16:54:51 +03:00
flags.Parse()
args := flags.Args()
if len(args) < 1 { flags.Usage() }
s := args[0]
2020-10-19 03:07:41 +03:00
u, err := url.Parse(s)
if err != nil {
os.Exit(1)
2020-10-19 03:07:41 +03:00
}
printNil(u.Scheme)
printNil(u.User.Username())
p, _ := u.User.Password()
printNil(p)
if host, port, err := net.SplitHostPort(u.Host) ; err!=nil {
printNil(u.Host)
fmt.Println(nilStr)
} else {
printNil(host)
printNil(port)
}
printNil(u.Path)
printNil(u.Fragment)
printNil(u.RawQuery)
/*fmt.Println(u.RawQuery)
m, _ := url.ParseQuery(u.RawQuery)
fmt.Println(m)
fmt.Println(m["k"][0])*/
}