diff --git a/README.md b/README.md index acecb38..04b143f 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ $ brew install https://raw.githubusercontent.com/moul/ssh2docker/master/contrib/ ### master (unreleased) -* No entry +* Flexible parsing of the input URL [full commits list](https://github.com/moul/gotty-client/compare/v1.3.0...master) diff --git a/gotty-client.go b/gotty-client.go index 9b4d958..ac67834 100644 --- a/gotty-client.go +++ b/gotty-client.go @@ -372,11 +372,30 @@ func (c *Client) SetOutput(w io.Writer) { c.Output = w } +// ParseURL parses an URL which may be incomplete and tries to standardize it +func ParseURL(input string) (string, error) { + parsed, err := url.Parse(input) + if err != nil { + return "", err + } + switch parsed.Scheme { + case "http", "https": + // everything is ok + default: + return ParseURL(fmt.Sprintf("http://%s", input)) + } + return parsed.String(), nil +} + // NewClient returns a GoTTY client object -func NewClient(httpURL string) (*Client, error) { +func NewClient(inputURL string) (*Client, error) { + url, err := ParseURL(inputURL) + if err != nil { + return nil, err + } return &Client{ Dialer: &websocket.Dialer{}, - URL: httpURL, + URL: url, WriteMutex: &sync.Mutex{}, Output: os.Stdout, QuitChan: make(chan struct{}),