master > master: code - go project hinzugefügt
This commit is contained in:
116
code/golang/internal/core/logging/logging.go
Normal file
116
code/golang/internal/core/logging/logging.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package logging
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* IMPORTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"ads/internal/core/utils"
|
||||
)
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* GLOBAL VARIABLES
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
var quietmode bool = false
|
||||
var debugmode bool = false
|
||||
var ansimode bool = true
|
||||
var loggingPrefix string = ""
|
||||
var force bool = false
|
||||
var tagAll bool = false
|
||||
|
||||
func GetQuietMode() bool {
|
||||
return quietmode
|
||||
}
|
||||
|
||||
func SetQuietMode(mode bool) {
|
||||
quietmode = mode
|
||||
}
|
||||
|
||||
func GetDebugMode() bool {
|
||||
return debugmode
|
||||
}
|
||||
|
||||
func SetDebugMode(mode bool) {
|
||||
debugmode = mode
|
||||
}
|
||||
|
||||
func GetAnsiMode() bool {
|
||||
return ansimode
|
||||
}
|
||||
|
||||
func SetAnsiMode(mode bool) {
|
||||
ansimode = mode
|
||||
}
|
||||
|
||||
func GetForce() bool {
|
||||
return force
|
||||
}
|
||||
|
||||
func SetForce(mode bool) {
|
||||
force = mode
|
||||
}
|
||||
|
||||
func SetTagAll(mode bool) {
|
||||
tagAll = mode
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* METHOD logging
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func logGeneric(tag string, lines ...interface{}) {
|
||||
if !force && quietmode {
|
||||
return
|
||||
}
|
||||
if !(tag == "") {
|
||||
tag = tag + " "
|
||||
}
|
||||
for _, line := range lines {
|
||||
_line := fmt.Sprintf("%[1]s%[2]s%[3]v", loggingPrefix, tag, line)
|
||||
if !ansimode {
|
||||
_line = utils.StripAnsi(_line)
|
||||
}
|
||||
fmt.Println(_line)
|
||||
if !tagAll {
|
||||
tag = ""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func LogPlain(lines ...interface{}) {
|
||||
SetTagAll(false)
|
||||
logGeneric("", lines...)
|
||||
}
|
||||
|
||||
func LogInfo(lines ...interface{}) {
|
||||
SetTagAll(true)
|
||||
logGeneric("[\033[94;1mINFO\033[0m]", lines...)
|
||||
}
|
||||
|
||||
func LogDebug(lines ...interface{}) {
|
||||
if !debugmode {
|
||||
return
|
||||
}
|
||||
SetTagAll(true)
|
||||
logGeneric("[\033[96;1mDEBUG\033[0m]", lines...)
|
||||
}
|
||||
|
||||
func LogWarn(lines ...interface{}) {
|
||||
SetTagAll(false)
|
||||
logGeneric("[\033[93;1mWARNING\033[0m]", lines...)
|
||||
}
|
||||
|
||||
func LogError(lines ...interface{}) {
|
||||
SetTagAll(false)
|
||||
logGeneric("[\033[91;1mERROR\033[0m]", lines...)
|
||||
}
|
||||
|
||||
func LogFatal(lines ...interface{}) {
|
||||
SetTagAll(false)
|
||||
logGeneric("[\033[91;1mFATAL\033[0m]", lines...)
|
||||
os.Exit(1)
|
||||
}
|
||||
85
code/golang/internal/core/utils/utils_arrays.go
Normal file
85
code/golang/internal/core/utils/utils_arrays.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package utils
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* IMPORTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* METHOD array contains
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func ArrayContains(x interface{}, elem interface{}) bool {
|
||||
xAsArray := reflect.ValueOf(x)
|
||||
if xAsArray.Kind() == reflect.Slice {
|
||||
for i := 0; i < xAsArray.Len(); i++ {
|
||||
if xAsArray.Index(i).Interface() == elem {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// func ListComprehension(x interface{}, cond (interface{}) bool) bool {
|
||||
// xAsArray := reflect.ValueOf(x)
|
||||
// if xAsArray.Kind() == reflect.Slice {
|
||||
// for i := 0; i < xAsArray.Len(); i++ {
|
||||
// if xAsArray.Index(i).Interface() == elem {
|
||||
// return true
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return false
|
||||
// }
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* METHOD array contains
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func UniqueListOfStrings(X []string) []string {
|
||||
var ok bool
|
||||
m := map[string]bool{}
|
||||
X_unique := []string{}
|
||||
for _, x := range X {
|
||||
if _, ok = m[x]; !ok {
|
||||
X_unique = append(X_unique, x)
|
||||
}
|
||||
}
|
||||
return X_unique
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* METHOD get value from array of unknown length
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func GetArrayStringValue(arr *[]string, index int, Default string) string {
|
||||
if arr != nil && len(*arr) > index {
|
||||
return (*arr)[index]
|
||||
}
|
||||
return Default
|
||||
}
|
||||
|
||||
func GetArrayBoolValue(arr *[]bool, index int, Default bool) bool {
|
||||
if arr != nil && len(*arr) > index {
|
||||
return (*arr)[index]
|
||||
}
|
||||
return Default
|
||||
}
|
||||
|
||||
func GetArrayIntValue(arr *[]int, index int, Default int) int {
|
||||
if arr != nil && len(*arr) > index {
|
||||
return (*arr)[index]
|
||||
}
|
||||
return Default
|
||||
}
|
||||
|
||||
func GetArrayInterfaceValue(arr *[](interface{}), index int, Default interface{}) interface{} {
|
||||
if arr != nil && len(*arr) > index {
|
||||
return (*arr)[index]
|
||||
}
|
||||
return Default
|
||||
}
|
||||
96
code/golang/internal/core/utils/utils_strings.go
Normal file
96
code/golang/internal/core/utils/utils_strings.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package utils
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* IMPORTS
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/lithammer/dedent"
|
||||
"github.com/slongfield/pyfmt"
|
||||
|
||||
"ads/pkg/re"
|
||||
)
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* METHOD format strings with dictionary substitution
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func FormatPythonString(text string, arguments map[string]interface{}) string {
|
||||
var (
|
||||
err error
|
||||
key string
|
||||
value interface{}
|
||||
kind reflect.Kind
|
||||
refValue reflect.Value
|
||||
)
|
||||
// force compatibility of expressions with python
|
||||
for key, value = range arguments {
|
||||
kind = reflect.TypeOf(value).Kind()
|
||||
switch kind {
|
||||
case reflect.Ptr:
|
||||
refValue = reflect.ValueOf(value)
|
||||
if refValue.IsNil() {
|
||||
arguments[key] = "None"
|
||||
}
|
||||
case reflect.Bool:
|
||||
arguments[key] = strings.Title(fmt.Sprintf(`%v`, value))
|
||||
}
|
||||
}
|
||||
text, err = pyfmt.Fmt(text, arguments)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return text
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* METHOD dedent textblock and expand escaped symbols
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func DedentIgnoreEmptyLines(text string) string {
|
||||
return dedent.Dedent(text)
|
||||
}
|
||||
|
||||
func DedentIgnoreFirstAndLast(text string) string {
|
||||
text = re.Sub(`^\s*[\n\r]|[\n\r]\s*$`, ``, text)
|
||||
return DedentIgnoreEmptyLines(text)
|
||||
}
|
||||
|
||||
func DedentAndExpand(text string) string {
|
||||
var err error
|
||||
var result []string
|
||||
result = []string{}
|
||||
text = dedent.Dedent(text)
|
||||
lines := strings.Split(text, "\n")
|
||||
for _, line := range lines {
|
||||
line = fmt.Sprintf(`"%s"`, line)
|
||||
line, err = strconv.Unquote(line)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
result = append(result, line)
|
||||
}
|
||||
return strings.Join(result, "\n")
|
||||
}
|
||||
|
||||
func FormatTextBlockAsList(text string, options ...bool) []string {
|
||||
var unindent bool = GetArrayBoolValue(&options, 0, true)
|
||||
if unindent {
|
||||
text = DedentIgnoreFirstAndLast(text)
|
||||
}
|
||||
return re.Split(`\n`, text)
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- *
|
||||
* METHODS ansi
|
||||
* ---------------------------------------------------------------- */
|
||||
|
||||
func StripAnsi(text string) string {
|
||||
return re.Sub(`\x1b[^m]*m`, ``, text)
|
||||
}
|
||||
Reference in New Issue
Block a user