initial commit

This commit is contained in:
daniel fusser 2023-08-28 21:47:36 +02:00
commit 48f26a58c9
12 changed files with 440 additions and 0 deletions

23
config.go Normal file
View File

@ -0,0 +1,23 @@
package util
import (
"os"
"gopkg.in/yaml.v2"
)
func NewConfig[V any](filepath string) (config *V, err error) {
file, err := os.Open(filepath)
if err != nil {
return nil, err
}
defer file.Close()
d := yaml.NewDecoder(file)
if err := d.Decode(&config); err != nil {
return nil, err
}
return config, nil
}

41
config_test.go Normal file
View File

@ -0,0 +1,41 @@
package util
import (
"os"
"testing"
)
func TestNewConfig(t *testing.T) {
f, err := os.CreateTemp("", "testconfig")
if err != nil {
t.Errorf(err.Error())
}
defer os.Remove(f.Name()) // clean up temp dir
type c struct {
Key1 string
Key2 string
}
want := c{
Key1: "val1",
Key2: "val2",
}
yaml := `key1: val1
key2: val2`
if _, err := f.Write([]byte(yaml)); err != nil {
t.Errorf(err.Error())
}
if err := f.Close(); err != nil {
t.Errorf(err.Error())
}
have, err := NewConfig[c](f.Name())
if err != nil {
t.Errorf(err.Error())
}
if want != *have {
t.Errorf("got %v, wanted %v", have, want)
}
}

31
csv.go Normal file
View File

@ -0,0 +1,31 @@
package util
import (
"fmt"
"os"
"strings"
)
// TODO: https://gist.github.com/andrea3bianchi/16d42a277db7b3de43b2a316b7894dbd
func PrintQuotedCsv(data [][]string, filename string, delimiter string) error {
f, err := os.Create(filename)
defer f.Close()
if err != nil {
return err
}
for _, row := range data {
sep := ""
for _, cell := range row {
_, err = f.WriteString(fmt.Sprintf(`%s"%s"`, sep, strings.Replace(cell, `"`, `""`, -1)))
if err != nil {
return err
}
sep = delimiter
}
_, err = f.WriteString("\n")
if err != nil {
return err
}
}
return nil
}

41
csv_test.go Normal file
View File

@ -0,0 +1,41 @@
package util
import (
"os"
"testing"
)
func TestPrintQuotedCsv(t *testing.T) {
data := [][]string{
{"header1", "header2", "header3"},
{"test1", "test2", "test3"},
{"test1", "test2", "test3"},
}
f, err := os.CreateTemp("", "testcsv")
if err != nil {
t.Errorf(err.Error())
}
filename := f.Name()
f.Close()
defer os.Remove(f.Name())
want := `"header1";"header2";"header3"
"test1";"test2";"test3"
"test1";"test2";"test3"
`
if err := PrintQuotedCsv(data, filename, ";"); err != nil {
t.Errorf(err.Error())
}
have, err := os.ReadFile(filename)
if err != nil {
t.Errorf(err.Error())
}
if want != string(have) {
t.Errorf("got \n%v\n wanted\n%v\n", string(have), want)
}
}

24
files.go Normal file
View File

@ -0,0 +1,24 @@
package util
import (
"fmt"
"os"
"path"
"path/filepath"
"time"
)
func MoveFileToDir(file, dir string) (err error) {
nf := filepath.Join(dir, path.Base(file))
return os.Rename(file, nf)
}
func MkdirTimestamped(dir string) (target string, err error) {
// TODO use filepath join
tf := fmt.Sprintf("%s%c%s%c%s%c%s", dir, filepath.Separator, "2006", filepath.Separator, "01", filepath.Separator, "02")
target = time.Now().Format(tf)
if err = os.MkdirAll(target, 0755); err != nil {
return
}
return
}

25
files_test.go Normal file
View File

@ -0,0 +1,25 @@
package util
import (
"os"
"testing"
)
func TestMkdirTimestamped(t *testing.T) {
dir, err := os.CreateTemp("", "testdir")
dirNew, err := MkdirTimestamped(dir.Name())
defer os.Remove(dir.Name())
if err != nil {
t.Errorf("mkdir %v error %v\n", dirNew, err)
}
t.Logf("dir: %v -> %v", dir, dirNew)
_, err = os.Stat(dirNew)
if err != nil {
t.Errorf("mkdir %v error %v\n", dirNew, err)
}
}
// func TestMoveFiletoDir(t *testing.T) { }

15
go.mod Normal file
View File

@ -0,0 +1,15 @@
module git.dafu.dev/dafu/go-util
go 1.21.0
require (
github.com/lmittmann/tint v1.0.1
github.com/pkg/sftp v1.13.6
golang.org/x/crypto v0.12.0
gopkg.in/yaml.v2 v2.4.0
)
require (
github.com/kr/fs v0.1.0 // indirect
golang.org/x/sys v0.11.0 // indirect
)

57
go.sum Normal file
View File

@ -0,0 +1,57 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/lmittmann/tint v1.0.1 h1:qBZR+K8XzaVFvhHvvZ7t63oQ3FuzDKa6TuCRujASxK8=
github.com/lmittmann/tint v1.0.1/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
github.com/pkg/sftp v1.13.6 h1:JFZT4XbOU7l77xGSpOdW+pwIMqP044IyjXX6FGyEKFo=
github.com/pkg/sftp v1.13.6/go.mod h1:tz1ryNURKu77RL+GuCzmoJYxQczL3wLNNpPWagdg4Qk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.11.0 h1:F9tnn/DA/Im8nCwm+fX+1/eBwi4qFjRT++MhtVC4ZX0=
golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
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.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

15
join.go Normal file
View File

@ -0,0 +1,15 @@
package util
// join with sep if arg is not empty
func JoinWithSep(sep string, args ...string) (result string) {
// l := len(args)
for i, v := range args {
if v != "" {
if i > 0 {
result = result + sep
}
result = result + v
}
}
return result
}

11
join_test.go Normal file
View File

@ -0,0 +1,11 @@
package util
import "testing"
func TestJoinWithSep(t *testing.T) {
want := "1_2_3_4"
have := JoinWithSep("_", "1", "2", "3", "4")
if want != have {
t.Errorf("got %q, wanted %q", have, want)
}
}

59
log.go Normal file
View File

@ -0,0 +1,59 @@
package util
import (
"os"
"runtime"
"runtime/debug"
"strconv"
"time"
"github.com/lmittmann/tint"
"log/slog"
)
var L *slog.Logger
var LogLvl = &slog.LevelVar{}
func init() {
L = slog.New(
tint.NewHandler(os.Stderr, &tint.Options{
Level: LogLvl,
TimeFormat: time.DateTime,
}),
)
// buildInfo, _ := debug.ReadBuildInfo()
// commit := Commit
// L = L.With(slog.String("app", buildInfo.Main.Path))
// set global logger with custom options
slog.SetDefault(L)
}
var Commit = func() string {
if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
if setting.Key == "vcs.revision" {
return setting.Value
}
}
}
return "not_available"
}()
// TODO clean this up
func LogTraceFunc() {}
func TraceFunc() string {
pc, file, line, ok := runtime.Caller(1)
if !ok {
return "? 0 ?"
}
fn := runtime.FuncForPC(pc)
if fn == nil {
return file + " " + strconv.Itoa(line) + " ?"
}
return file + " " + strconv.Itoa(line) + " " + fn.Name()
}

98
sftp.go Normal file
View File

@ -0,0 +1,98 @@
package util
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"golang.org/x/crypto/ssh"
"github.com/pkg/sftp"
)
type SftpStorageOpts struct {
Host string
Port int
User string
Password string
// Logger *slog.Logger
}
type SftpStorage struct {
SftpStorageOpts
sshcon *ssh.Client
Sftpcon *sftp.Client
}
func NewSsftp(c SftpStorageOpts) SftpStorage {
return SftpStorage{SftpStorageOpts: c}
}
// caller needs to defer
func (s *SftpStorage) Close() {
s.Sftpcon.Close()
s.sshcon.Close()
}
func (s *SftpStorage) Connect() (err error) {
var auths []ssh.AuthMethod
if s.Password != "" {
auths = append(auths, ssh.Password(s.Password))
}
// Initialize client configuration
config := ssh.ClientConfig{
User: s.User,
Auth: auths,
// ignore host key check
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
addr := fmt.Sprintf("%s:%d", s.Host, s.Port)
// Connect to server
s.sshcon, err = ssh.Dial("tcp", addr, &config)
if err != nil {
return fmt.Errorf("failed to connecto to [%s]: %v", addr, err)
}
// Create new SFTP client
s.Sftpcon, err = sftp.NewClient(s.sshcon)
if err != nil {
return fmt.Errorf("unable to start SFTP subsystem [%s]: %v", addr, err)
}
return
}
func (s *SftpStorage) UploadFile(localFile, remoteFile string) (err error) {
srcFile, err := os.Open(localFile)
if err != nil {
return fmt.Errorf("unable to open localfile file: %v", err)
}
defer srcFile.Close()
parent := filepath.Dir(remoteFile)
path := string(filepath.Separator)
dirs := strings.Split(parent, path)
for _, dir := range dirs {
path = filepath.Join(path, dir)
s.Sftpcon.Mkdir(path)
}
dstFile, err := s.Sftpcon.OpenFile(remoteFile, (os.O_WRONLY | os.O_CREATE | os.O_TRUNC))
if err != nil {
return fmt.Errorf("unable to open remote file: %v", err)
}
defer dstFile.Close()
_, err = io.Copy(dstFile, srcFile)
if err != nil {
return fmt.Errorf("unable to upload file: %v", err)
}
return
}