Browse Source

Merge branch 'wip-conn'

wip-main-ui
Jaeha Choi 2 months ago
parent
commit
97c34d3640
Signed by: Jaeha.Choi GPG Key ID: D6133D1D117FF503
  1. 28
      internal/client/client.go
  2. 5
      internal/client/client_test.go
  3. 2
      pkg/go.mod
  4. 4
      pkg/go.sum
  5. 33
      pkg/log/log.go
  6. 16
      pkg/log/log_test.go
  7. 33
      pkg/util/util.go

28
internal/client/client.go

@ -1 +1,29 @@
package client
import (
"crypto/tls"
"github.com/jaeha-choi/Proj_Coconut_Utility/log"
"strconv"
)
type Client struct {
ServerIp string `yaml:"server_ip"`
ServerPort uint16 `yaml:"server_port"`
tlsConfig *tls.Config
}
func Connect() {
client1 := &Client{
ServerIp: "127.0.0.1",
ServerPort: 9129,
tlsConfig: &tls.Config{InsecureSkipVerify: true},
}
dial, err := tls.Dial("tcp", client1.ServerIp+":"+strconv.Itoa(int(client1.ServerPort)), client1.tlsConfig)
if err != nil {
log.Debug(err)
log.Error("Error while connecting to the server")
return
}
_, _ = dial.Write([]byte("hello"))
_ = dial.Close()
}

5
internal/client/client_test.go

@ -3,8 +3,13 @@ package client
import (
"github.com/jaeha-choi/Proj_Coconut_Utility/log"
"os"
"testing"
)
func init() {
log.Init(os.Stdout, log.DEBUG)
}
func TestConnect(t *testing.T) {
Connect()
}

2
pkg/go.mod

@ -1,3 +1,5 @@
module github.com/jaeha-choi/Proj_Coconut_Utility
go 1.16
require gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b

4
pkg/go.sum

@ -0,0 +1,4 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

33
pkg/log/log.go

@ -37,6 +37,13 @@ func Debug(msg ...interface{}) {
}
}
// Debugf logs if LoggingMode is set to DEBUG or lower
func Debugf(format string, msg ...interface{}) {
if mode <= DEBUG {
_ = logger.Output(2, "DEBUG:\t"+fmt.Sprintf(format, msg...))
}
}
// Info logs if LoggingMode is set to INFO or lower
func Info(msg ...interface{}) {
if mode <= INFO {
@ -44,6 +51,13 @@ func Info(msg ...interface{}) {
}
}
// Infof logs if LoggingMode is set to INFO or lower
func Infof(format string, msg ...interface{}) {
if mode <= INFO {
_ = logger.Output(2, "INFO:\t"+fmt.Sprintf(format, msg...))
}
}
// Warning logs if LoggingMode is set to WARNING or lower
func Warning(msg ...interface{}) {
if mode <= WARNING {
@ -51,6 +65,13 @@ func Warning(msg ...interface{}) {
}
}
// Warningf logs if LoggingMode is set to WARNING or lower
func Warningf(format string, msg ...interface{}) {
if mode <= WARNING {
_ = logger.Output(2, "WARNING:\t"+fmt.Sprintf(format, msg...))
}
}
// Error logs if LoggingMode is set to ERROR or lower
func Error(msg ...interface{}) {
if mode <= ERROR {
@ -58,7 +79,19 @@ func Error(msg ...interface{}) {
}
}
// Errorf logs if LoggingMode is set to ERROR or lower
func Errorf(format string, msg ...interface{}) {
if mode <= ERROR {
_ = logger.Output(2, "Error:\t"+fmt.Sprintf(format, msg...))
}
}
// Fatal always logs when used
func Fatal(msg ...interface{}) {
_ = logger.Output(2, "FATAL:\t"+fmt.Sprint(msg...))
}
// Fatalf always logs when used
func Fatalf(format string, msg ...interface{}) {
_ = logger.Output(2, "FATAL:\t"+fmt.Sprintf(format, msg...))
}

16
pkg/log/log_test.go

@ -6,7 +6,7 @@ import (
"testing"
)
func TestInit(t *testing.T) {
func init() {
initTesting(os.Stdout, DEBUG)
}
@ -29,6 +29,20 @@ func TestDebug(t *testing.T) {
}
}
func TestDebugf(t *testing.T) {
var buffer bytes.Buffer
initTesting(&buffer, DEBUG)
Debugf(" %s", "test debug")
if "Test: DEBUG:\t test debug\n" != buffer.String() {
t.Error("Output mismatch")
}
buffer.Reset()
Infof("%s1", "test info")
if "Test: INFO:\ttest info1\n" != buffer.String() {
t.Error("Output mismatch")
}
}
func TestInfo(t *testing.T) {
var buffer bytes.Buffer
initTesting(&buffer, INFO)

33
pkg/util/util.go

@ -4,6 +4,7 @@ import (
"encoding/binary"
"errors"
"github.com/jaeha-choi/Proj_Coconut_Utility/log"
"gopkg.in/yaml.v3"
"io"
"io/ioutil"
"net"
@ -391,3 +392,35 @@ func IntToUint32(n int64) (uint32, error) {
func CheckIPAddress(ip string) bool {
return net.ParseIP(ip) != nil
}
// WriteConfig writes config to fileName in yaml format
func WriteConfig(fileName string, config interface{}) (err error) {
dstFile, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
log.Debug(err)
log.Error("Error while creating file for config")
return err
}
defer func() {
if err := dstFile.Close(); err != nil {
log.Debug(err)
log.Error("Error while closing config file")
return
}
}()
marshal, err := yaml.Marshal(&config)
if err != nil {
log.Debug(err)
log.Error("Error while converting serv struct to []byte")
return err
}
if _, err := dstFile.Write(marshal); err != nil {
log.Debug(err)
log.Error("Error while writing config file")
return err
}
return nil
}

Loading…
Cancel
Save