2014-07-28 20:23:38 -04:00
|
|
|
package parsers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FIXME: Change this not to receive default value as parameter
|
2014-11-07 13:44:35 -05:00
|
|
|
func ParseHost(defaultTCPAddr, defaultUnixAddr, addr string) (string, error) {
|
2014-07-28 20:23:38 -04:00
|
|
|
addr = strings.TrimSpace(addr)
|
2014-11-07 13:44:35 -05:00
|
|
|
if addr == "" {
|
|
|
|
addr = fmt.Sprintf("unix://%s", defaultUnixAddr)
|
|
|
|
}
|
|
|
|
addrParts := strings.Split(addr, "://")
|
|
|
|
if len(addrParts) == 1 {
|
|
|
|
addrParts = []string{"tcp", addrParts[0]}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch addrParts[0] {
|
|
|
|
case "tcp":
|
|
|
|
return ParseTCPAddr(addrParts[1], defaultTCPAddr)
|
|
|
|
case "unix":
|
|
|
|
return ParseUnixAddr(addrParts[1], defaultUnixAddr)
|
|
|
|
case "fd":
|
2014-07-28 20:23:38 -04:00
|
|
|
return addr, nil
|
|
|
|
default:
|
2014-11-07 13:44:35 -05:00
|
|
|
return "", fmt.Errorf("Invalid bind address format: %s", addr)
|
2014-07-28 20:23:38 -04:00
|
|
|
}
|
2014-11-07 13:44:35 -05:00
|
|
|
}
|
2014-07-28 20:23:38 -04:00
|
|
|
|
2014-11-07 13:44:35 -05:00
|
|
|
func ParseUnixAddr(addr string, defaultAddr string) (string, error) {
|
|
|
|
addr = strings.TrimPrefix(addr, "unix://")
|
|
|
|
if strings.Contains(addr, "://") {
|
|
|
|
return "", fmt.Errorf("Invalid proto, expected unix: %s", addr)
|
|
|
|
}
|
|
|
|
if addr == "" {
|
|
|
|
addr = defaultAddr
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("unix://%s", addr), nil
|
|
|
|
}
|
2014-07-28 20:23:38 -04:00
|
|
|
|
2014-11-07 13:44:35 -05:00
|
|
|
func ParseTCPAddr(addr string, defaultAddr string) (string, error) {
|
|
|
|
addr = strings.TrimPrefix(addr, "tcp://")
|
|
|
|
if strings.Contains(addr, "://") || addr == "" {
|
|
|
|
return "", fmt.Errorf("Invalid proto, expected tcp: %s", addr)
|
|
|
|
}
|
2014-07-28 20:23:38 -04:00
|
|
|
|
2014-11-07 13:44:35 -05:00
|
|
|
hostParts := strings.Split(addr, ":")
|
|
|
|
if len(hostParts) != 2 {
|
2014-07-28 20:23:38 -04:00
|
|
|
return "", fmt.Errorf("Invalid bind address format: %s", addr)
|
|
|
|
}
|
2014-11-07 13:44:35 -05:00
|
|
|
host := hostParts[0]
|
|
|
|
if host == "" {
|
|
|
|
host = defaultAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := strconv.Atoi(hostParts[1])
|
|
|
|
if err != nil && p == 0 {
|
|
|
|
return "", fmt.Errorf("Invalid bind address format: %s", addr)
|
2014-07-28 20:23:38 -04:00
|
|
|
}
|
2014-11-07 13:44:35 -05:00
|
|
|
return fmt.Sprintf("tcp://%s:%d", host, p), nil
|
2014-07-28 20:23:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get a repos name and returns the right reposName + tag
|
|
|
|
// The tag can be confusing because of a port in a repository name.
|
|
|
|
// Ex: localhost.localdomain:5000/samalba/hipache:latest
|
|
|
|
func ParseRepositoryTag(repos string) (string, string) {
|
|
|
|
n := strings.LastIndex(repos, ":")
|
|
|
|
if n < 0 {
|
|
|
|
return repos, ""
|
|
|
|
}
|
|
|
|
if tag := repos[n+1:]; !strings.Contains(tag, "/") {
|
|
|
|
return repos[:n], tag
|
|
|
|
}
|
|
|
|
return repos, ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func PartParser(template, data string) (map[string]string, error) {
|
|
|
|
// ip:public:private
|
|
|
|
var (
|
|
|
|
templateParts = strings.Split(template, ":")
|
|
|
|
parts = strings.Split(data, ":")
|
|
|
|
out = make(map[string]string, len(templateParts))
|
|
|
|
)
|
|
|
|
if len(parts) != len(templateParts) {
|
|
|
|
return nil, fmt.Errorf("Invalid format to parse. %s should match template %s", data, template)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, t := range templateParts {
|
|
|
|
value := ""
|
|
|
|
if len(parts) > i {
|
|
|
|
value = parts[i]
|
|
|
|
}
|
|
|
|
out[t] = value
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseKeyValueOpt(opt string) (string, string, error) {
|
|
|
|
parts := strings.SplitN(opt, "=", 2)
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return "", "", fmt.Errorf("Unable to parse key/value option: %s", opt)
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]), nil
|
|
|
|
}
|
2014-11-03 13:15:55 -05:00
|
|
|
|
|
|
|
func ParsePortRange(ports string) (uint64, uint64, error) {
|
|
|
|
if ports == "" {
|
|
|
|
return 0, 0, fmt.Errorf("Empty string specified for ports.")
|
|
|
|
}
|
|
|
|
if !strings.Contains(ports, "-") {
|
|
|
|
start, err := strconv.ParseUint(ports, 10, 16)
|
|
|
|
end := start
|
|
|
|
return start, end, err
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.Split(ports, "-")
|
|
|
|
start, err := strconv.ParseUint(parts[0], 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
end, err := strconv.ParseUint(parts[1], 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
if end < start {
|
|
|
|
return 0, 0, fmt.Errorf("Invalid range specified for the Port: %s", ports)
|
|
|
|
}
|
|
|
|
return start, end, nil
|
|
|
|
}
|