static expressvpn list
This commit is contained in:
113
vendor/github.com/pyke369/golang-support/uconfig/uconfig.go
generated
vendored
113
vendor/github.com/pyke369/golang-support/uconfig/uconfig.go
generated
vendored
@@ -23,9 +23,11 @@ import (
|
||||
)
|
||||
|
||||
type UConfig struct {
|
||||
input string
|
||||
config interface{}
|
||||
hash string
|
||||
cache map[string]interface{}
|
||||
separator string
|
||||
cacheLock sync.RWMutex
|
||||
sync.RWMutex
|
||||
}
|
||||
@@ -136,14 +138,23 @@ func reduce(input interface{}) {
|
||||
|
||||
func New(input string, inline ...bool) (*UConfig, error) {
|
||||
config := &UConfig{
|
||||
config: nil,
|
||||
input: input,
|
||||
config: nil,
|
||||
separator: ".",
|
||||
}
|
||||
return config, config.Load(input, inline...)
|
||||
}
|
||||
|
||||
func (this *UConfig) Reload(inline ...bool) error {
|
||||
return this.Load(this.input, inline...)
|
||||
}
|
||||
|
||||
func (this *UConfig) SetSeparator(s string) {
|
||||
this.separator = s
|
||||
}
|
||||
|
||||
func (this *UConfig) Load(input string, inline ...bool) error {
|
||||
this.Lock()
|
||||
this.cache = map[string]interface{}{}
|
||||
base, _ := os.Getwd()
|
||||
content := fmt.Sprintf("/*base:%s*/\n", base)
|
||||
if len(inline) > 0 && inline[0] {
|
||||
@@ -182,36 +193,36 @@ func (this *UConfig) Load(input string, inline ...bool) error {
|
||||
if arguments[0][0:1] != "/" {
|
||||
arguments[0] = fmt.Sprintf("%s/%s", base, arguments[0])
|
||||
}
|
||||
base = ""
|
||||
nbase := ""
|
||||
if elements, err := filepath.Glob(arguments[0]); err == nil {
|
||||
for _, element := range elements {
|
||||
if mcontent, err := ioutil.ReadFile(element); err == nil {
|
||||
base = filepath.Dir(element)
|
||||
nbase = filepath.Dir(element)
|
||||
expanded += string(mcontent)
|
||||
}
|
||||
}
|
||||
}
|
||||
if base != "" && strings.Index(expanded, "\n") >= 0 {
|
||||
expanded = fmt.Sprintf("/*base:%s*/\n%s\n", base, expanded)
|
||||
if nbase != "" && strings.Index(expanded, "\n") >= 0 {
|
||||
expanded = fmt.Sprintf("/*base:%s*/\n%s\n/*base:%s*/\n", nbase, expanded, base)
|
||||
}
|
||||
case "|":
|
||||
if arguments[0][0:1] != "/" {
|
||||
arguments[0] = fmt.Sprintf("%s/%s", base, arguments[0])
|
||||
}
|
||||
base = ""
|
||||
nbase := ""
|
||||
if elements, err := filepath.Glob(arguments[0]); err == nil {
|
||||
for _, element := range elements {
|
||||
if element[0:1] != "/" {
|
||||
element = fmt.Sprintf("%s/%s", base, element)
|
||||
}
|
||||
if mcontent, err := exec.Command(element, strings.Join(arguments[1:], " ")).Output(); err == nil {
|
||||
base = filepath.Dir(element)
|
||||
nbase = filepath.Dir(element)
|
||||
expanded += string(mcontent)
|
||||
}
|
||||
}
|
||||
}
|
||||
if base != "" && strings.Index(expanded, "\n") >= 0 {
|
||||
expanded = fmt.Sprintf("/*base:%s*/\n%s\n", base, expanded)
|
||||
if nbase != "" && strings.Index(expanded, "\n") >= 0 {
|
||||
expanded = fmt.Sprintf("/*base:%s*/\n%s\n/*base:%s*/\n", nbase, expanded, base)
|
||||
}
|
||||
case "@":
|
||||
requester := http.Client{
|
||||
@@ -227,16 +238,27 @@ func (this *UConfig) Load(input string, inline ...bool) error {
|
||||
case "&":
|
||||
expanded += os.Getenv(arguments[0])
|
||||
case "!":
|
||||
arguments[0] = strings.ToLower(arguments[0])
|
||||
for index := 1; index < len(os.Args); index++ {
|
||||
option := strings.ToLower(os.Args[index])
|
||||
if option == "--"+arguments[0] {
|
||||
if index == len(os.Args)-1 || strings.HasPrefix(os.Args[index+1], "--") {
|
||||
expanded = "true"
|
||||
} else {
|
||||
expanded = os.Args[index+1]
|
||||
if matcher := rcache.Get(fmt.Sprintf(`(?i)^--?(no-?)?(?:%s)(?:(=)(.+))?$`, arguments[0])); matcher != nil {
|
||||
for index := 1; index < len(os.Args); index++ {
|
||||
option := os.Args[index]
|
||||
if option == "--" {
|
||||
break
|
||||
}
|
||||
if captures := matcher.FindStringSubmatch(option); captures != nil {
|
||||
if captures[2] == "=" {
|
||||
expanded = captures[3]
|
||||
} else {
|
||||
if index == len(os.Args)-1 || strings.HasPrefix(os.Args[index+1], "-") {
|
||||
expanded = "true"
|
||||
if captures[1] != "" {
|
||||
expanded = "false"
|
||||
}
|
||||
} else {
|
||||
expanded = os.Args[index+1]
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
case "-":
|
||||
@@ -258,15 +280,14 @@ func (this *UConfig) Load(input string, inline ...bool) error {
|
||||
content = fmt.Sprintf("%s%s%s", content[0:indexes[0]], expanded, content[indexes[1]:len(content)])
|
||||
}
|
||||
|
||||
this.hash = fmt.Sprintf("%x", sha1.Sum([]byte(content)))
|
||||
if err := json.Unmarshal([]byte(content), &this.config); err != nil {
|
||||
if err := json.Unmarshal([]byte("{"+content+"}"), &this.config); err != nil {
|
||||
this.config = nil
|
||||
var config interface{}
|
||||
if err := json.Unmarshal([]byte(content), &config); err != nil {
|
||||
if err := json.Unmarshal([]byte("{"+content+"}"), &config); err != nil {
|
||||
if syntax, ok := err.(*json.SyntaxError); ok && syntax.Offset < int64(len(content)) {
|
||||
if start := strings.LastIndex(content[:syntax.Offset], "\n") + 1; start >= 0 {
|
||||
line := strings.Count(content[:start], "\n") + 1
|
||||
this.Unlock()
|
||||
return errors.New(fmt.Sprintf("%s at line %d near %s", syntax, line, content[start:syntax.Offset]))
|
||||
return fmt.Errorf("uconfig: %s at line %d near %s", syntax, line, content[start:syntax.Offset])
|
||||
}
|
||||
}
|
||||
this.Unlock()
|
||||
@@ -274,6 +295,9 @@ func (this *UConfig) Load(input string, inline ...bool) error {
|
||||
}
|
||||
}
|
||||
|
||||
this.config = config
|
||||
this.hash = fmt.Sprintf("%x", sha1.Sum([]byte(content)))
|
||||
this.cache = map[string]interface{}{}
|
||||
reduce(this.config)
|
||||
this.Unlock()
|
||||
return nil
|
||||
@@ -320,8 +344,8 @@ func (this *UConfig) GetPaths(path string) []string {
|
||||
}
|
||||
this.cacheLock.RUnlock()
|
||||
if path != "" {
|
||||
prefix = "."
|
||||
for _, part := range strings.Split(path, ".") {
|
||||
prefix = this.separator
|
||||
for _, part := range strings.Split(path, this.separator) {
|
||||
kind := reflect.TypeOf(current).Kind()
|
||||
index, err := strconv.Atoi(part)
|
||||
if (kind == reflect.Slice && (err != nil || index < 0 || index >= len(current.([]interface{})))) || (kind != reflect.Slice && kind != reflect.Map) {
|
||||
@@ -367,14 +391,14 @@ func (this *UConfig) value(path string) (string, error) {
|
||||
this.RLock()
|
||||
if current == nil || path == "" {
|
||||
this.RUnlock()
|
||||
return "", fmt.Errorf("invalid parameter")
|
||||
return "", errors.New(`uconfig: invalid parameter`)
|
||||
}
|
||||
this.cacheLock.RLock()
|
||||
if this.cache[path] != nil {
|
||||
if current, ok := this.cache[path].(bool); ok && !current {
|
||||
this.cacheLock.RUnlock()
|
||||
this.RUnlock()
|
||||
return "", fmt.Errorf("invalid path")
|
||||
return "", errors.New(`uconfig: invalid path`)
|
||||
}
|
||||
if current, ok := this.cache[path].(string); ok {
|
||||
this.cacheLock.RUnlock()
|
||||
@@ -383,7 +407,7 @@ func (this *UConfig) value(path string) (string, error) {
|
||||
}
|
||||
}
|
||||
this.cacheLock.RUnlock()
|
||||
for _, part := range strings.Split(path, ".") {
|
||||
for _, part := range strings.Split(path, this.separator) {
|
||||
kind := reflect.TypeOf(current).Kind()
|
||||
index, err := strconv.Atoi(part)
|
||||
if (kind == reflect.Slice && (err != nil || index < 0 || index >= len(current.([]interface{})))) || (kind != reflect.Slice && kind != reflect.Map) {
|
||||
@@ -391,7 +415,7 @@ func (this *UConfig) value(path string) (string, error) {
|
||||
this.cache[path] = false
|
||||
this.cacheLock.Unlock()
|
||||
this.RUnlock()
|
||||
return "", fmt.Errorf("invalid path")
|
||||
return "", errors.New(`uconfig: invalid path`)
|
||||
}
|
||||
if kind == reflect.Slice {
|
||||
current = current.([]interface{})[index]
|
||||
@@ -401,7 +425,7 @@ func (this *UConfig) value(path string) (string, error) {
|
||||
this.cache[path] = false
|
||||
this.cacheLock.Unlock()
|
||||
this.RUnlock()
|
||||
return "", fmt.Errorf("invalid path")
|
||||
return "", errors.New(`uconfig: invalid path`)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -416,7 +440,7 @@ func (this *UConfig) value(path string) (string, error) {
|
||||
this.cache[path] = false
|
||||
this.cacheLock.Unlock()
|
||||
this.RUnlock()
|
||||
return "", fmt.Errorf("invalid path")
|
||||
return "", errors.New(`uconfig: invalid path`)
|
||||
}
|
||||
|
||||
func (this *UConfig) GetBoolean(path string, fallback bool) bool {
|
||||
@@ -566,3 +590,28 @@ func (this *UConfig) GetDurationBounds(path string, fallback, min, max float64)
|
||||
}
|
||||
return math.Max(math.Min(nvalue, max), min)
|
||||
}
|
||||
|
||||
func Duration(input float64) time.Duration {
|
||||
return time.Duration(input * float64(time.Second))
|
||||
}
|
||||
|
||||
func Args() (args []string) {
|
||||
for index := 1; index < len(os.Args); index++ {
|
||||
option := os.Args[index]
|
||||
if args == nil {
|
||||
if option[0] == '-' {
|
||||
if option != "-" && option != "--" && strings.Index(option, "=") < 0 && index < len(os.Args)-1 && os.Args[index+1][0] != '-' {
|
||||
index++
|
||||
}
|
||||
} else {
|
||||
args = []string{}
|
||||
}
|
||||
}
|
||||
if args != nil {
|
||||
args = append(args, option)
|
||||
} else if option == "--" {
|
||||
args = []string{}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user