expvarmon/utils.go

50 lines
1.0 KiB
Go
Raw Normal View History

2015-05-01 21:49:19 +08:00
package main
import (
"errors"
2015-05-04 00:02:28 +08:00
"fmt"
2015-05-02 15:56:09 +08:00
"path/filepath"
2015-05-01 21:49:19 +08:00
"strings"
2015-05-04 00:02:28 +08:00
"github.com/bsiegert/ranges"
2015-05-01 21:49:19 +08:00
)
// ParseVars returns parsed and validated slice of strings with
// variables names that will be used for monitoring.
2015-05-02 01:12:23 +08:00
func ParseVars(vars string) ([]VarName, error) {
if vars == "" {
2015-05-01 21:49:19 +08:00
return nil, errors.New("no vars specified")
}
2015-05-02 01:12:23 +08:00
ss := strings.FieldsFunc(vars, func(r rune) bool { return r == ',' })
2015-05-01 23:48:34 +08:00
var ret []VarName
2015-05-02 01:12:23 +08:00
for _, s := range ss {
ret = append(ret, VarName(s))
}
2015-05-01 21:49:19 +08:00
return ret, nil
}
2015-05-02 15:56:09 +08:00
// BaseCommand returns cleaned command name from Cmdline array.
//
// I.e. "./some.service/binary.name -arg 1 -arg" will be "binary.name".
func BaseCommand(cmdline []string) string {
if len(cmdline) == 0 {
return ""
}
return filepath.Base(cmdline[0])
}
2015-05-02 15:56:55 +08:00
// ParsePorts converts comma-separated ports into strings slice
func ParsePorts(s string) ([]string, error) {
2015-05-04 00:02:28 +08:00
portsInt, err := ranges.Parse(s)
if err != nil {
return nil, err
2015-05-02 15:56:55 +08:00
}
2015-05-04 00:02:28 +08:00
var ports []string
for _, port := range portsInt {
ports = append(ports, fmt.Sprintf("%d", port))
}
2015-05-02 15:56:55 +08:00
return ports, nil
}