|
|
package tools |
|
|
|
|
|
import ( |
|
|
jsoniter "github.com/json-iterator/go" |
|
|
"os" |
|
|
"os/signal" |
|
|
"syscall" |
|
|
) |
|
|
|
|
|
func HandleSignals(exitCode int) int { |
|
|
s := make(chan os.Signal) |
|
|
signal.Notify(s, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) |
|
|
EXIT: |
|
|
for { |
|
|
switch <-s { |
|
|
case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT: |
|
|
exitCode = 0 |
|
|
break EXIT |
|
|
case syscall.SIGHUP: |
|
|
default: |
|
|
break EXIT |
|
|
} |
|
|
} |
|
|
return exitCode |
|
|
} |
|
|
|
|
|
|
|
|
func Struct2Bytes(v interface{}) ([]byte, error) { |
|
|
|
|
|
configCompatibleWithStandardLibrary := jsoniter.ConfigCompatibleWithStandardLibrary |
|
|
|
|
|
bytes_, err := configCompatibleWithStandardLibrary.Marshal(v) |
|
|
if err != nil { |
|
|
return nil, err |
|
|
} |
|
|
return bytes_, nil |
|
|
} |
|
|
|
|
|
|
|
|
func Bytes2Struct(data []byte, v interface{}) error { |
|
|
|
|
|
configCompatibleWithStandardLibrary := jsoniter.ConfigCompatibleWithStandardLibrary |
|
|
|
|
|
err := configCompatibleWithStandardLibrary.Unmarshal(data, v) |
|
|
if err != nil { |
|
|
return err |
|
|
} |
|
|
return nil |
|
|
} |
|
|
|