This commit is contained in:
bel
2020-01-13 03:37:51 +00:00
commit c8eb52f9ba
2023 changed files with 702080 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
package check
import (
"fmt"
"os"
)
// Dir checks the given path, will return error if path not exists or path
// is not directory.
func Dir(path string) error {
if info, err := os.Stat(path); err != nil {
return fmt.Errorf(`directory not exists: %s`, path)
} else if !info.IsDir() {
return fmt.Errorf(`path is not directory: %s`, path)
}
return nil
}

View File

@@ -0,0 +1,37 @@
package check
import (
"fmt"
"os"
)
// ReadableError is just a structure contains readable message that can be
// returned directly to end user.
type ReadableError struct {
Message string
}
// Error returns the description of ReadableError.
func (e ReadableError) Error() string {
return e.Message
}
// NewReadableError creates a ReadableError{} from given message.
func NewReadableError(message string) ReadableError {
return ReadableError{Message: message}
}
// ErrorForExit check the error.
// If error is not nil, print the error message and exit the application.
// If error is nil, do nothing.
func ErrorForExit(name string, err error, code ...int) {
if err != nil {
exitCode := 1
if len(code) > 0 {
exitCode = code[0]
}
fmt.Fprintf(os.Stderr, "%s: %s (%d)\n", name, err.Error(), exitCode)
fmt.Fprintf(os.Stderr, "See \"%s --help\".\n", name)
os.Exit(exitCode)
}
}

View File

@@ -0,0 +1,11 @@
package check
import (
"regexp"
)
// HostAndPort checks whether a string contains host and port.
// It returns true if matched.
func HostAndPort(hostAndPort string) bool {
return regexp.MustCompile(`^[^:]+:[0-9]+$`).MatchString(hostAndPort)
}

View File

@@ -0,0 +1,41 @@
package check
// StringSliceContains iterates over the slice to find the target.
func StringSliceContains(slice []string, target string) bool {
for _, v := range slice {
if v == target {
return true
}
}
return false
}
// IntSliceContains iterates over the slice to find the target.
func IntSliceContains(slice []int, target int) bool {
for _, v := range slice {
if v == target {
return true
}
}
return false
}
// Int32SliceContains iterates over the slice to find the target.
func Int32SliceContains(slice []int32, target int32) bool {
for _, v := range slice {
if v == target {
return true
}
}
return false
}
// Int64SliceContains iterates over the slice to find the target.
func Int64SliceContains(slice []int64, target int64) bool {
for _, v := range slice {
if v == target {
return true
}
}
return false
}