-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
118 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,15 @@ package main | |
import ( | ||
"errors" | ||
"fmt" | ||
"net" | ||
"net/url" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/bsiegert/ranges" | ||
) | ||
|
||
var ErrParsePorts = fmt.Errorf("cannot parse ports argument") | ||
|
||
// ParseVars returns parsed and validated slice of strings with | ||
// variables names that will be used for monitoring. | ||
func ParseVars(vars string) ([]VarName, error) { | ||
|
@@ -35,48 +37,83 @@ func BaseCommand(cmdline []string) string { | |
return filepath.Base(cmdline[0]) | ||
} | ||
|
||
// ParsePorts converts comma-separated ports into strings slice | ||
func ParsePorts(s string) ([]string, error) { | ||
var ( | ||
ports []string | ||
err error | ||
) | ||
// Try simple mode, ports only ("1234-1235,80") | ||
ports, err = parseRange(s) | ||
if err == nil { | ||
return ports, nil | ||
// flattenURLs returns URLs for the given addr and set of ports. | ||
// | ||
// Note, rawurl shouldn't contain port, as port will be appended. | ||
func flattenURLs(rawurl string, ports []string) ([]url.URL, error) { | ||
var urls []url.URL | ||
|
||
// Add http by default | ||
if !strings.HasPrefix(rawurl, "http") { | ||
rawurl = fmt.Sprintf("http://%s", rawurl) | ||
} | ||
|
||
// Make URL from rawurl | ||
baseUrl, err := url.Parse(rawurl) | ||
if err != nil { | ||
return nil, err | ||
} | ||
baseUrl.Path = ExpvarsPath | ||
|
||
var ErrParsePorts = fmt.Errorf("cannot parse ports argument") | ||
// Create new URL for each port | ||
for _, port := range ports { | ||
u := *baseUrl | ||
u.Host = fmt.Sprintf("%s:%s", u.Host, port) | ||
urls = append(urls, u) | ||
} | ||
return urls, nil | ||
} | ||
|
||
// else, try host:ports notation ("localhost:1234-1235,remote:2000,2345") | ||
// ParsePorts parses and flattens comma-separated ports/urls into URLs slice | ||
func ParsePorts(s string) ([]url.URL, error) { | ||
var urls []url.URL | ||
fields := strings.FieldsFunc(s, func(r rune) bool { return r == ',' }) | ||
for _, field := range fields { | ||
// split host:ports | ||
var host, portsRange string | ||
// Try simple 'ports range' mode, ports only ("1234-1235,80") | ||
// Defaults to "localhost" will be used. | ||
ports, err := parseRange(field) | ||
if err == nil { | ||
furls, err := flattenURLs("http://localhost", ports) | ||
if err != nil { | ||
return nil, err | ||
} | ||
urls = append(urls, furls...) | ||
continue | ||
} | ||
|
||
// then, try host:ports notation ("localhost:1234-1235,https://remote:2000,2345") | ||
var rawurl, portsRange string | ||
parts := strings.FieldsFunc(field, func(r rune) bool { return r == ':' }) | ||
if len(parts) == 1 { | ||
host = "localhost" | ||
} else if len(parts) == 2 { | ||
host, portsRange = parts[0], parts[1] | ||
} else { | ||
return nil, ErrParsePorts | ||
switch len(parts) { | ||
case 1: | ||
// "1234-234" | ||
rawurl = "http://localhost" | ||
case 2: | ||
// "localhost:1234" | ||
rawurl, portsRange = parts[0], parts[1] | ||
default: | ||
// "https://user:[email protected]:1234" | ||
rawurl = strings.Join(parts[:len(parts)-1], ":") | ||
portsRange = parts[len(parts)-1] | ||
} | ||
|
||
pp, err := parseRange(portsRange) | ||
ports, err = parseRange(portsRange) | ||
if err != nil { | ||
return nil, ErrParsePorts | ||
} | ||
|
||
for _, p := range pp { | ||
addr := net.JoinHostPort(host, p) | ||
ports = append(ports, addr) | ||
purls, err := flattenURLs(rawurl, ports) | ||
if err != nil { | ||
return nil, ErrParsePorts | ||
} | ||
|
||
urls = append(urls, purls...) | ||
} | ||
|
||
return ports, nil | ||
return urls, nil | ||
} | ||
|
||
// parseRange flattens port ranges, such as "1234-1240,1333" | ||
func parseRange(s string) ([]string, error) { | ||
portsInt, err := ranges.Parse(s) | ||
if err != nil { | ||
|
@@ -89,3 +126,12 @@ func parseRange(s string) ([]string, error) { | |
} | ||
return ports, nil | ||
} | ||
|
||
// NewURL returns net.URL for the given port, with expvarmon defaults set. | ||
func NewURL(port string) url.URL { | ||
return url.URL{ | ||
Scheme: "http", | ||
Host: fmt.Sprintf("localhost:%s"), | ||
Path: "/debug/vars", | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters