Initial commit
This commit is contained in:
26
vendor/github.com/hyperhq/hypercli/utils/debug.go
generated
vendored
Normal file
26
vendor/github.com/hyperhq/hypercli/utils/debug.go
generated
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
)
|
||||
|
||||
// EnableDebug sets the DEBUG env var to true
|
||||
// and makes the logger to log at debug level.
|
||||
func EnableDebug() {
|
||||
os.Setenv("DEBUG", "1")
|
||||
logrus.SetLevel(logrus.DebugLevel)
|
||||
}
|
||||
|
||||
// DisableDebug sets the DEBUG env var to false
|
||||
// and makes the logger to log at info level.
|
||||
func DisableDebug() {
|
||||
os.Setenv("DEBUG", "")
|
||||
logrus.SetLevel(logrus.InfoLevel)
|
||||
}
|
||||
|
||||
// IsDebugEnabled checks whether the debug flag is set or not.
|
||||
func IsDebugEnabled() bool {
|
||||
return os.Getenv("DEBUG") != ""
|
||||
}
|
||||
43
vendor/github.com/hyperhq/hypercli/utils/debug_test.go
generated
vendored
Normal file
43
vendor/github.com/hyperhq/hypercli/utils/debug_test.go
generated
vendored
Normal file
@@ -0,0 +1,43 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
)
|
||||
|
||||
func TestEnableDebug(t *testing.T) {
|
||||
defer func() {
|
||||
os.Setenv("DEBUG", "")
|
||||
logrus.SetLevel(logrus.InfoLevel)
|
||||
}()
|
||||
EnableDebug()
|
||||
if os.Getenv("DEBUG") != "1" {
|
||||
t.Fatalf("expected DEBUG=1, got %s\n", os.Getenv("DEBUG"))
|
||||
}
|
||||
if logrus.GetLevel() != logrus.DebugLevel {
|
||||
t.Fatalf("expected log level %v, got %v\n", logrus.DebugLevel, logrus.GetLevel())
|
||||
}
|
||||
}
|
||||
|
||||
func TestDisableDebug(t *testing.T) {
|
||||
DisableDebug()
|
||||
if os.Getenv("DEBUG") != "" {
|
||||
t.Fatalf("expected DEBUG=\"\", got %s\n", os.Getenv("DEBUG"))
|
||||
}
|
||||
if logrus.GetLevel() != logrus.InfoLevel {
|
||||
t.Fatalf("expected log level %v, got %v\n", logrus.InfoLevel, logrus.GetLevel())
|
||||
}
|
||||
}
|
||||
|
||||
func TestDebugEnabled(t *testing.T) {
|
||||
EnableDebug()
|
||||
if !IsDebugEnabled() {
|
||||
t.Fatal("expected debug enabled, got false")
|
||||
}
|
||||
DisableDebug()
|
||||
if IsDebugEnabled() {
|
||||
t.Fatal("expected debug disabled, got true")
|
||||
}
|
||||
}
|
||||
9
vendor/github.com/hyperhq/hypercli/utils/experimental.go
generated
vendored
Normal file
9
vendor/github.com/hyperhq/hypercli/utils/experimental.go
generated
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
// +build experimental
|
||||
|
||||
package utils
|
||||
|
||||
// ExperimentalBuild is a stub which always returns true for
|
||||
// builds that include the "experimental" build tag
|
||||
func ExperimentalBuild() bool {
|
||||
return true
|
||||
}
|
||||
12
vendor/github.com/hyperhq/hypercli/utils/names.go
generated
vendored
Normal file
12
vendor/github.com/hyperhq/hypercli/utils/names.go
generated
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
package utils
|
||||
|
||||
import "regexp"
|
||||
|
||||
// RestrictedNameChars collects the characters allowed to represent a name, normally used to validate container and volume names.
|
||||
const RestrictedNameChars = `[a-zA-Z0-9][a-zA-Z0-9_.-]`
|
||||
|
||||
// RestrictedNamePattern is a regular expression to validate names against the collection of restricted characters.
|
||||
var RestrictedNamePattern = regexp.MustCompile(`^/?` + RestrictedNameChars + `+$`)
|
||||
|
||||
// RestrictedVolumeNamePattern is a regular expression to validate volume names against the collection of restricted characters.
|
||||
var RestrictedVolumeNamePattern = regexp.MustCompile(`^` + RestrictedNameChars + `+$`)
|
||||
9
vendor/github.com/hyperhq/hypercli/utils/stubs.go
generated
vendored
Normal file
9
vendor/github.com/hyperhq/hypercli/utils/stubs.go
generated
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
// +build !experimental
|
||||
|
||||
package utils
|
||||
|
||||
// ExperimentalBuild is a stub which always returns false for
|
||||
// builds that do not include the "experimental" build tag
|
||||
func ExperimentalBuild() bool {
|
||||
return false
|
||||
}
|
||||
42
vendor/github.com/hyperhq/hypercli/utils/templates/templates.go
generated
vendored
Normal file
42
vendor/github.com/hyperhq/hypercli/utils/templates/templates.go
generated
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
package templates
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
// basicFunctions are the set of initial
|
||||
// functions provided to every template.
|
||||
var basicFunctions = template.FuncMap{
|
||||
"json": func(v interface{}) string {
|
||||
a, _ := json.Marshal(v)
|
||||
return string(a)
|
||||
},
|
||||
"split": strings.Split,
|
||||
"join": strings.Join,
|
||||
"title": strings.Title,
|
||||
"lower": strings.ToLower,
|
||||
"upper": strings.ToUpper,
|
||||
"pad": padWithSpace,
|
||||
}
|
||||
|
||||
// Parse creates a new annonymous template with the basic functions
|
||||
// and parses the given format.
|
||||
func Parse(format string) (*template.Template, error) {
|
||||
return NewParse("", format)
|
||||
}
|
||||
|
||||
// NewParse creates a new tagged template with the basic functions
|
||||
// and parses the given format.
|
||||
func NewParse(tag, format string) (*template.Template, error) {
|
||||
return template.New(tag).Funcs(basicFunctions).Parse(format)
|
||||
}
|
||||
|
||||
// padWithSpace adds whitespace to the input if the input is non-empty
|
||||
func padWithSpace(source string, prefix, suffix int) string {
|
||||
if source == "" {
|
||||
return source
|
||||
}
|
||||
return strings.Repeat(" ", prefix) + source + strings.Repeat(" ", suffix)
|
||||
}
|
||||
38
vendor/github.com/hyperhq/hypercli/utils/templates/templates_test.go
generated
vendored
Normal file
38
vendor/github.com/hyperhq/hypercli/utils/templates/templates_test.go
generated
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
package templates
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseStringFunctions(t *testing.T) {
|
||||
tm, err := Parse(`{{join (split . ":") "/"}}`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var b bytes.Buffer
|
||||
if err := tm.Execute(&b, "text:with:colon"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := "text/with/colon"
|
||||
if b.String() != want {
|
||||
t.Fatalf("expected %s, got %s", want, b.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewParse(t *testing.T) {
|
||||
tm, err := NewParse("foo", "this is a {{ . }}")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var b bytes.Buffer
|
||||
if err := tm.Execute(&b, "string"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := "this is a string"
|
||||
if b.String() != want {
|
||||
t.Fatalf("expected %s, got %s", want, b.String())
|
||||
}
|
||||
}
|
||||
107
vendor/github.com/hyperhq/hypercli/utils/utils.go
generated
vendored
Normal file
107
vendor/github.com/hyperhq/hypercli/utils/utils.go
generated
vendored
Normal file
@@ -0,0 +1,107 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/docker/distribution/registry/api/errcode"
|
||||
"github.com/hyperhq/hypercli/pkg/archive"
|
||||
"github.com/hyperhq/hypercli/pkg/stringid"
|
||||
)
|
||||
|
||||
var globalTestID string
|
||||
|
||||
// TestDirectory creates a new temporary directory and returns its path.
|
||||
// The contents of directory at path `templateDir` is copied into the
|
||||
// new directory.
|
||||
func TestDirectory(templateDir string) (dir string, err error) {
|
||||
if globalTestID == "" {
|
||||
globalTestID = stringid.GenerateNonCryptoID()[:4]
|
||||
}
|
||||
prefix := fmt.Sprintf("docker-test%s-%s-", globalTestID, GetCallerName(2))
|
||||
if prefix == "" {
|
||||
prefix = "docker-test-"
|
||||
}
|
||||
dir, err = ioutil.TempDir("", prefix)
|
||||
if err = os.Remove(dir); err != nil {
|
||||
return
|
||||
}
|
||||
if templateDir != "" {
|
||||
if err = archive.CopyWithTar(templateDir, dir); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetCallerName introspects the call stack and returns the name of the
|
||||
// function `depth` levels down in the stack.
|
||||
func GetCallerName(depth int) string {
|
||||
// Use the caller function name as a prefix.
|
||||
// This helps trace temp directories back to their test.
|
||||
pc, _, _, _ := runtime.Caller(depth + 1)
|
||||
callerLongName := runtime.FuncForPC(pc).Name()
|
||||
parts := strings.Split(callerLongName, ".")
|
||||
callerShortName := parts[len(parts)-1]
|
||||
return callerShortName
|
||||
}
|
||||
|
||||
// ReplaceOrAppendEnvValues returns the defaults with the overrides either
|
||||
// replaced by env key or appended to the list
|
||||
func ReplaceOrAppendEnvValues(defaults, overrides []string) []string {
|
||||
cache := make(map[string]int, len(defaults))
|
||||
for i, e := range defaults {
|
||||
parts := strings.SplitN(e, "=", 2)
|
||||
cache[parts[0]] = i
|
||||
}
|
||||
|
||||
for _, value := range overrides {
|
||||
// Values w/o = means they want this env to be removed/unset.
|
||||
if !strings.Contains(value, "=") {
|
||||
if i, exists := cache[value]; exists {
|
||||
defaults[i] = "" // Used to indicate it should be removed
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// Just do a normal set/update
|
||||
parts := strings.SplitN(value, "=", 2)
|
||||
if i, exists := cache[parts[0]]; exists {
|
||||
defaults[i] = value
|
||||
} else {
|
||||
defaults = append(defaults, value)
|
||||
}
|
||||
}
|
||||
|
||||
// Now remove all entries that we want to "unset"
|
||||
for i := 0; i < len(defaults); i++ {
|
||||
if defaults[i] == "" {
|
||||
defaults = append(defaults[:i], defaults[i+1:]...)
|
||||
i--
|
||||
}
|
||||
}
|
||||
|
||||
return defaults
|
||||
}
|
||||
|
||||
// GetErrorMessage returns the human readable message associated with
|
||||
// the passed-in error. In some cases the default Error() func returns
|
||||
// something that is less than useful so based on its types this func
|
||||
// will go and get a better piece of text.
|
||||
func GetErrorMessage(err error) string {
|
||||
switch err.(type) {
|
||||
case errcode.Error:
|
||||
e, _ := err.(errcode.Error)
|
||||
return e.Message
|
||||
|
||||
case errcode.ErrorCode:
|
||||
ec, _ := err.(errcode.ErrorCode)
|
||||
return ec.Message()
|
||||
|
||||
default:
|
||||
return err.Error()
|
||||
}
|
||||
}
|
||||
21
vendor/github.com/hyperhq/hypercli/utils/utils_test.go
generated
vendored
Normal file
21
vendor/github.com/hyperhq/hypercli/utils/utils_test.go
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
package utils
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestReplaceAndAppendEnvVars(t *testing.T) {
|
||||
var (
|
||||
d = []string{"HOME=/"}
|
||||
o = []string{"HOME=/root", "TERM=xterm"}
|
||||
)
|
||||
|
||||
env := ReplaceOrAppendEnvValues(d, o)
|
||||
if len(env) != 2 {
|
||||
t.Fatalf("expected len of 2 got %d", len(env))
|
||||
}
|
||||
if env[0] != "HOME=/root" {
|
||||
t.Fatalf("expected HOME=/root got '%s'", env[0])
|
||||
}
|
||||
if env[1] != "TERM=xterm" {
|
||||
t.Fatalf("expected TERM=xterm got '%s'", env[1])
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user