diff --git a/cmd/logcli/client.go b/cmd/logcli/client.go index c46953a7f2cf..7d5543a1dad0 100644 --- a/cmd/logcli/client.go +++ b/cmd/logcli/client.go @@ -54,28 +54,29 @@ func listLabelValues(name string) (*logproto.LabelResponse, error) { } func doRequest(path string, out interface{}) error { - fullURL, err := url.Parse(addrURL.String() + path) - if err != nil { - return err - } - url := fullURL.String() + us := *addr + path if !*quiet { - log.Print(url) + log.Print(us) } - req, err := http.NewRequest("GET", url, nil) + req, err := http.NewRequest("GET", us, nil) if err != nil { return err } req.SetBasicAuth(*username, *password) + // Parse the URL to extract the host + u, err := url.Parse(us) + if err != nil { + return err + } clientConfig := config.HTTPClientConfig{ TLSConfig: config.TLSConfig{ CAFile: *tlsCACertPath, CertFile: *tlsClientCertPath, KeyFile: *tlsClientCertKeyPath, - ServerName: fullURL.Host, + ServerName: u.Host, InsecureSkipVerify: *tlsSkipVerify, }, } @@ -109,27 +110,31 @@ func liveTailQueryConn() (*websocket.Conn, error) { } func wsConnect(path string) (*websocket.Conn, error) { + us := *addr + path + // Parse the URL to extract the host + u, err := url.Parse(us) + if err != nil { + return nil, err + } tlsConfig, err := config.NewTLSConfig(&config.TLSConfig{ CAFile: *tlsCACertPath, CertFile: *tlsClientCertPath, KeyFile: *tlsClientCertKeyPath, - ServerName: addrURL.Host, + ServerName: u.Host, InsecureSkipVerify: *tlsSkipVerify, }) if err != nil { return nil, err } - addrURL.Path = path - url := addrURL.String() - if strings.HasPrefix(url, "https") { - url = strings.Replace(url, "https", "wss", 1) - } else if strings.HasPrefix(url, "http") { - url = strings.Replace(url, "http", "ws", 1) + if strings.HasPrefix(us, "https") { + us = strings.Replace(us, "https", "wss", 1) + } else if strings.HasPrefix(us, "http") { + us = strings.Replace(us, "http", "ws", 1) } if !*quiet { - log.Println(url) + log.Println(us) } h := http.Header{"Authorization": {"Basic " + base64.StdEncoding.EncodeToString([]byte(*username+":"+*password))}} @@ -138,7 +143,7 @@ func wsConnect(path string) (*websocket.Conn, error) { TLSClientConfig: tlsConfig, } - c, resp, err := ws.Dial(url, h) + c, resp, err := ws.Dial(us, h) if err != nil { if resp == nil { diff --git a/cmd/logcli/main.go b/cmd/logcli/main.go index 86cb8bcf81a2..1e3ca247bebd 100644 --- a/cmd/logcli/main.go +++ b/cmd/logcli/main.go @@ -2,7 +2,6 @@ package main import ( "log" - "net/url" "os" "gopkg.in/alecthomas/kingpin.v2" @@ -13,8 +12,7 @@ var ( quiet = app.Flag("quiet", "suppress everything but log lines").Default("false").Short('q').Bool() outputMode = app.Flag("output", "specify output mode [default, raw, jsonl]").Default("default").Short('o').Enum("default", "raw", "jsonl") - addr = app.Flag("addr", "Server address.").Default("https://logs-us-west1.grafana.net").Envar("GRAFANA_ADDR").String() - addrURL url.URL + addr = app.Flag("addr", "Server address.").Default("https://logs-us-west1.grafana.net").Envar("GRAFANA_ADDR").String() username = app.Flag("username", "Username for HTTP basic auth.").Default("").Envar("GRAFANA_USERNAME").String() password = app.Flag("password", "Password for HTTP basic auth.").Default("").Envar("GRAFANA_PASSWORD").String() @@ -51,12 +49,6 @@ func main() { log.Fatalln("Server address cannot be empty") } - u, err := url.Parse(*addr) - if err != nil { - log.Fatalf("Failed to parse addr into URL: %v", err) - } - addrURL = *u - switch cmd { case queryCmd.FullCommand(): doQuery()