commit 28032406154a57456c15f60b83dc851b6fdb1588 Author: Softonik Date: Mon Feb 19 16:53:07 2024 +0300 Начало diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..0fbe4b0 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,30 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "type": "process", + "label": "go: test package", + "command": "/s/t", + "args": [ + "${fileDirname}", + "${fileDirnameBasename}" + ], + "problemMatcher": [ + "$go" + ], + "group": { + "kind": "test", + "isDefault": true + }, + "detail": "cd /app; go test ${fileDirname}", + "presentation": { + "echo": false, + "reveal": "always", + "focus": false, + "panel": "shared", + "showReuseMessage": false, + "clear": true + } + } + ] +} diff --git a/Magefile.go b/Magefile.go new file mode 100644 index 0000000..a11bc15 --- /dev/null +++ b/Magefile.go @@ -0,0 +1,271 @@ +//go:build mage + +package main + +import ( + "context" + "log" + "os" + "strings" + + // mage:import + . "magefile/docker" + + "github.com/magefile/mage/mg" +) + +var ( + GolangVolume = "golang.my" + tests_to_run = []string{} +) + +func init() { + AppName = "ktgo" + ImageName = "my/go" + + BuildFn = build +} + +func Test() { + mg.Deps(TestLibs) + mg.Deps(TestIntegration) + mg.Deps(TestSharedLibsBuild) +} +func TestLibs(ctx context.Context) error { + paths := []string{ + "pkg", + "pkg/printer", + "pkg/scenarios", + "pkg/link", + "pkg/link/wifi", + "pkg/parser", + "internal", + } + + listTests(paths) + + // err := addTest("test") + // if err != nil { + // println("ERROR: " + "test") + // return err + // } + + return runListedTests() +} +func TestIntegration(ctx context.Context) error { + return runTests("./test/int") +} +func TestSystem(ctx context.Context) error { + return runTests("./test") +} + +func listTests(paths []string) error { + for _, p := range paths { + listAndTest(p) + } + + return nil +} + +func listAndTest(p string) error { + entries, err := os.ReadDir(p) + if err != nil { + log.Fatal(err) + } + + return testList(p, entries) +} + +func testList(p string, entries []os.DirEntry) error { + for _, e := range entries { + testOne(p, e) + } + + return nil +} + +func testOne(p string, e os.DirEntry) error { + if !e.IsDir() { + return nil + } + if e.Name() == "driver" { + return nil + } + if e.Name() == "lib" { + return nil + } + if e.Name() == "testlib" { + return nil + } + + entries, err := os.ReadDir(p + "/" + e.Name()) + if err != nil { + log.Fatal(err) + } + + found := false + for _, e2 := range entries { + if e2.IsDir() { + continue + } + + if strings.Contains(e2.Name(), "_test.go") { + found = true + break + } + } + if !found { + return nil + } + + testPath := p + "/" + e.Name() + err = addTest(testPath) + if err != nil { + println("ERROR: " + testPath) + return err + } + + return nil +} + +func addTest(p string) error { + tests_to_run = append(tests_to_run, "./"+p) + return nil +} + +func runListedTests() error { + test_paths := strings.Join(tests_to_run, " ") + return runTests(test_paths) +} + +func runTests(paths string) error { + Bash(`cp -f $HOME/.Xauthority /tmp/._test_Xauthority`) + return Bash(`sudo docker run -ti --rm \ + -h host \ + --net=none \ + -v /etc/localtime:/etc/localtime:ro \ + -v ` + GolangVolume + `:/usr/local/go:ro \ + \ + -v /gopath:/gopath:rw \ + -v ${PWD}:/app \ + -v /usr/bin/docker:/usr/bin/docker:ro \ + -v /var/run/docker.sock:/var/run/docker.sock:ro \ + -v /usr/bin/socat:/usr/bin/socat \ + \ + -e GOPATH=/gopath \ + -e GOCACHE=/gopath/gocache \ + -e APPDIR=${PWD} \ + \ + --device=/dev/dri/card0 \ + -e DISPLAY=:0 \ + -v /tmp/.X11-unix:/tmp/.X11-unix:ro \ + -v /tmp/._test_Xauthority:/home/user/.Xauthority:ro \ + \ + -w /app \ + -u 0 \ + \ + --entrypoint=/bin/bash \ + \ + ` + ImageName + " -c '" + ` \ + for t in ` + paths + `; do \ + go test -v -p 1 -gcflags=-l -count=1 $t || exit 1; \ + done'`) +} + +func TestSharedLibsBuild(ctx context.Context) error { + mg.Deps(Build_shared_lib) + return Bash(`rm lib/lib3dscreen.*`) +} +func Build_shared_lib(ctx context.Context) error { + return Bash(`sudo docker run --rm \ + -h host \ + --net=none \ + -v /etc/localtime:/etc/localtime:ro \ + -v ` + GolangVolume + `:/usr/local/go:ro \ + \ + -v /gopath:/gopath:rw \ + -v ${PWD}:/app \ + \ + -e GOPATH=/gopath \ + -e GOCACHE=/gopath/gocache \ + \ + -w /app/pkg/sharedlib \ + -u 1000 \ + \ + --entrypoint=/bin/bash \ + \ + ` + ImageName + " -c '" + ` \ + go build -o /app/lib/libktgo.so -buildmode=c-shared \ + && strip /app/lib/libktgo.so \ + '`) +} + +func Build_installer(ctx context.Context) error { + return Bash(`sudo docker run --rm \ + -h host \ + --net=none \ + -v /etc/localtime:/etc/localtime:ro \ + -v ` + GolangVolume + `:/usr/local/go:ro \ + \ + -v /gopath:/gopath:rw \ + -v ${PWD}:/app \ + \ + -e GOPATH=/gopath \ + -e GOCACHE=/gopath/gocache \ + \ + -w /app/cmd/installer \ + -u 1000 \ + \ + --entrypoint=/bin/bash \ + \ + ` + ImageName + " -c '" + ` \ + mkdir -p /app/bin/ \ + && GOARCH=arm go build -o /app/install_helper \ + ' \ + && arm-linux-gnueabi-strip install_helper \ + && mv -fv install_helper ../../../src/installer/ \ + `) +} + +func build() { + Bash(`GOPHERJS_GOROOT=/gopath/go/1.18/ gopherjs build 3diy/3dscreen/web/main -o js/app.js \ + && ls -l js/app.js \ + && node -e 'go = require("./js/app.js"); console.log(go);'`) +} + +func ControlLinkWifi0() { + Bash(`curl '0:5005?conn=Wifi&enabled=0'`) +} +func ControlLinkWifi1() { + Bash(`curl '0:5005?conn=Wifi&enabled=1'`) +} +func ControlLinkWifiConn0() { + Bash(`curl '0:5005?conn=Wifi&connected=0'`) +} +func ControlLinkWifiConn1() { + Bash(`curl '0:5005?conn=Wifi&connected=1&ip=192.168.1.150&gw=192.168.1.1'`) +} + +func ControlLinkEther0() { + Bash(`curl '0:5005?conn=Ether&enabled=0'`) +} +func ControlLinkEther1() { + Bash(`curl '0:5005?conn=Ether&enabled=1'`) +} +func ControlLinkEtherConn0() { + Bash(`curl '0:5005?conn=Ether&connected=0'`) +} +func ControlLinkEtherConn1() { + Bash(`curl '0:5005?conn=Ether&connected=1&ip=192.168.1.150&gw=192.168.1.1'`) +} + +func TestControlLinkStatus() { + Bash(`curl '0:5005/link_status?interface=eth0&reason=RECONFIGURE&ip=192.168.1.150&routers=192.168.1.1'`) +} + +func TestControlZamenaInsertFilament() { + Bash(`curl '0:5005/send_from_terminal?m=Insert%20filament%20and%20press%20button'`) +} +func TestControlZamenaPurgeMore() { + Bash(`curl '0:5005/send_from_terminal?m=PurgeMore'`) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..003264e --- /dev/null +++ b/go.mod @@ -0,0 +1,18 @@ +module my/ktgo + +go 1.21 + +require ( + github.com/cucumber/godog v0.0.0-00010101000000-000000000000 + github.com/davecgh/go-spew v1.1.1 + github.com/fsnotify/fsnotify v1.7.0 + github.com/gopherjs/gopherjs v1.18.0-beta1 + github.com/hashicorp/go-version v1.6.0 + github.com/magefile/mage v1.14.0 + github.com/miratronix/jopher v0.0.0-20200228204442-56bcab34f1a3 + github.com/onsi/gomega v1.24.1 +) + +require golang.org/x/sys v0.4.0 // indirect + +replace github.com/cucumber/godog => /gopath/src/my/godog diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..c083aeb --- /dev/null +++ b/go.sum @@ -0,0 +1,218 @@ +github.com/agiledragon/gomonkey/v2 v2.9.0 h1:PDiKKybR596O6FHW+RVSG0Z7uGCBNbmbUXh3uCNQ7Hc= +github.com/agiledragon/gomonkey/v2 v2.9.0/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY= +github.com/agiledragon/gomonkey/v2 v2.10.1 h1:FPJJNykD1957cZlGhr9X0zjr291/lbazoZ/dmc4mS4c= +github.com/agiledragon/gomonkey/v2 v2.10.1/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY= +github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY= +github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= +github.com/cucumber/gherkin-go/v19 v19.0.3 h1:mMSKu1077ffLbTJULUfM5HPokgeBcIGboyeNUof1MdE= +github.com/cucumber/gherkin-go/v19 v19.0.3/go.mod h1:jY/NP6jUtRSArQQJ5h1FXOUgk5fZK24qtE7vKi776Vw= +github.com/cucumber/messages-go/v16 v16.0.0/go.mod h1:EJcyR5Mm5ZuDsKJnT2N9KRnBK30BGjtYotDKpwQ0v6g= +github.com/cucumber/messages-go/v16 v16.0.1 h1:fvkpwsLgnIm0qugftrw2YwNlio+ABe2Iu94Ap8GMYIY= +github.com/cucumber/messages-go/v16 v16.0.1/go.mod h1:EJcyR5Mm5ZuDsKJnT2N9KRnBK30BGjtYotDKpwQ0v6g= +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/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= +github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= +github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= +github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg= +github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= +github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M= +github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0= +github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= +github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= +github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI= +github.com/go-resty/resty/v2 v2.7.0 h1:me+K9p3uhSmXtrBZ4k9jcEAfJmuC8IivWHwaLZwPrFY= +github.com/go-resty/resty/v2 v2.7.0/go.mod h1:9PWDzw47qPphMRFfhsyk0NnSgvluHcljSMVIq3w7q0I= +github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo= +github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw= +github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM= +github.com/goccy/go-yaml v1.11.0 h1:n7Z+zx8S9f9KgzG6KtQKf+kwqXZlLNR2F6018Dgau54= +github.com/goccy/go-yaml v1.11.0/go.mod h1:H+mJrWtjPTJAHvRbV09MCK9xYwODM+wRTVFFTWckfng= +github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= +github.com/gofrs/uuid v4.2.0+incompatible h1:yyYWMnhkhrKwwr8gAOcOCYxOOscHgDS9yZgBrnJfGa0= +github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= +github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= +github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/gopherjs/gopherjs v1.18.0-beta1 h1:IbykhVEq4SAjwyBRuNHl0aOO6w6IqgL3RUdMhoBo4mY= +github.com/gopherjs/gopherjs v1.18.0-beta1/go.mod h1:6UY8PXRnu51MqjYCCY4toG0S5GeH5uVJ3qDxIsa+kqo= +github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/hashicorp/go-immutable-radix v1.3.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= +github.com/hashicorp/go-immutable-radix v1.3.1 h1:DKHmCUm2hRBK510BaiZlwvpD40f8bJFeZnpfm2KLowc= +github.com/hashicorp/go-immutable-radix v1.3.1/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= +github.com/hashicorp/go-memdb v1.3.2 h1:RBKHOsnSszpU6vxq80LzC2BaQjuuvoyaQbkLTf7V7g8= +github.com/hashicorp/go-memdb v1.3.2/go.mod h1:Mluclgwib3R93Hk5fxEfiRhB+6Dar64wWh71LpNSe3g= +github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hashicorp/go-uuid v1.0.2 h1:cfejS+Tpcp13yd5nYHWDI6qVCny6wyX2Mt5SGur2IGE= +github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek= +github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= +github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc= +github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= +github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= +github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= +github.com/kardianos/service v1.2.2 h1:ZvePhAHfvo0A7Mftk/tEzqEZ7Q4lgnR8sGz4xu1YX60= +github.com/kardianos/service v1.2.2/go.mod h1:CIMRFEJVL+0DS1a3Nx06NaMn4Dz63Ng6O7dl0qH0zVM= +github.com/klauspost/compress v1.10.3 h1:OP96hzwJVBIHYU52pVTI6CczrxPvrGfgqF9N5eTO0Q8= +github.com/klauspost/compress v1.10.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= +github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/labstack/echo v3.3.10+incompatible h1:pGRcYk231ExFAyoAjAfD85kQzRJCRI8bbnE7CX5OEgg= +github.com/labstack/echo v3.3.10+incompatible/go.mod h1:0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s= +github.com/labstack/echo/v4 v4.10.2 h1:n1jAhnq/elIFTHr1EYpiYtyKgx4RW9ccVgkqByZaN2M= +github.com/labstack/echo/v4 v4.10.2/go.mod h1:OEyqf2//K1DFdE57vw2DRgWY0M7s65IVQO2FzvI4J5k= +github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8= +github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM= +github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= +github.com/looplab/fsm v1.0.1 h1:OEW0ORrIx095N/6lgoGkFkotqH6s7vaFPsgjLAaF5QU= +github.com/looplab/fsm v1.0.1/go.mod h1:PmD3fFvQEIsjMEfvZdrCDZ6y8VwKTwWNjlpEr6IKPO4= +github.com/magefile/mage v1.14.0 h1:6QDX3g6z1YvJ4olPhT1wksUcSa/V0a1B+pJb73fBjyo= +github.com/magefile/mage v1.14.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= +github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= +github.com/mattn/go-colorable v0.1.11 h1:nQ+aFkoE2TMGc0b68U2OKSexC+eq46+XwZzWXHRmPYs= +github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= +github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/miratronix/jopher v0.0.0-20200228204442-56bcab34f1a3 h1:lOZEb4IMbMjOLMLvo4WjX6zJ8y3KsFrUtVa2rJS40HQ= +github.com/miratronix/jopher v0.0.0-20200228204442-56bcab34f1a3/go.mod h1:u6ZTEUu3UCNJmCOT3vqHPyI0i4cT7mplTbuHApP/lD8= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/olebedev/emitter v0.0.0-20230411050614-349169dec2ba h1:/Q5vvLs180BFH7u+Nakdrr1B9O9RAxVaIurFQy0c8QQ= +github.com/olebedev/emitter v0.0.0-20230411050614-349169dec2ba/go.mod h1:eT2/Pcsim3XBjbvldGiJBvvgiqZkAFyiOJJsDKXs/ts= +github.com/onsi/ginkgo/v2 v2.5.0 h1:TRtrvv2vdQqzkwrQ1ke6vtXf7IK34RBUJafIy1wMwls= +github.com/onsi/gomega v1.24.1 h1:KORJXNNTzJXzu4ScJWssJfJMnJ+2QJqhoQSRwNlze9E= +github.com/onsi/gomega v1.24.1/go.mod h1:3AOiACssS3/MajrniINInwbfOOtfZvplPzuRSmvt1jM= +github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4= +github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= +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/rainycape/dl v0.0.0-20151222075243-1b01514224a1 h1:XZlja+DeIOJeEPWAfM9M5wko0keB9qgRfuuWbry6VBQ= +github.com/rainycape/dl v0.0.0-20151222075243-1b01514224a1/go.mod h1:lh74SQgfeEuNq74dKWzDLuVB+/ntX5c4g1oDyL4GkGg= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= +github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= +github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= +github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= +github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +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/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/tomwright/dasel/v2 v2.5.0 h1:PiTh6j1BSpSFs0SgIEd4gJ6qgR2aaM7JFY2Jtj0RN48= +github.com/tomwright/dasel/v2 v2.5.0/go.mod h1:D9A334pSIDbSyVayctKXjyS8R2PEZmwD1X6xBe4v7Ps= +github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= +github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasttemplate v1.2.1 h1:TVEnxayobAdVkhQfrfes2IzOB6o+z4roRkPF52WA1u4= +github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= +github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo= +github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 h1:kUhD7nTDoI3fVd9G4ORWrbV5NY0liEs/Jg2pv5f+bBA= +golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/crypto v0.6.0 h1:qfktjS5LUO+fFKeJXZ+ikTRijMmljikvG68fpMMruSc= +golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58= +golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A= +golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= +golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA= +golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.2.0 h1:sZfSu1wtKLGlWI4ZZayP0ck9Y73K1ynO6gqzTdBVdPU= +golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= +golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g= +golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ= +golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= +golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg= +golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A= +golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18= +golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= +golang.org/x/sys v0.6.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/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= +golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +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/term v0.14.0 h1:LGK9IlZ8T9jvdy6cTdfKUCltatMFOehAQo9SRC46UQ8= +golang.org/x/term v0.14.0/go.mod h1:TySc+nGkYR6qt8km8wUhuFRTVSMIX3XPR58y2lC8vww= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg= +golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68= +golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f h1:GGU+dLjvlC3qDwqYgL6UgRmHXhOOgns0bZu2Ty5mm6U= +golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= +gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +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.0-20210107192922-496545a6307b/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= +nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g= +nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0= diff --git a/lib/libktgo.h b/lib/libktgo.h new file mode 100644 index 0000000..3c4c837 --- /dev/null +++ b/lib/libktgo.h @@ -0,0 +1,81 @@ +/* Code generated by cmd/cgo; DO NOT EDIT. */ + +/* package my/ktgo/pkg/sharedlib */ + + +#line 1 "cgo-builtin-export-prolog" + +#include + +#ifndef GO_CGO_EXPORT_PROLOGUE_H +#define GO_CGO_EXPORT_PROLOGUE_H + +#ifndef GO_CGO_GOSTRING_TYPEDEF +typedef struct { const char *p; ptrdiff_t n; } _GoString_; +#endif + +#endif + +/* Start of preamble from import "C" comments. */ + + + + +/* End of preamble from import "C" comments. */ + + +/* Start of boilerplate cgo prologue. */ +#line 1 "cgo-gcc-export-header-prolog" + +#ifndef GO_CGO_PROLOGUE_H +#define GO_CGO_PROLOGUE_H + +typedef signed char GoInt8; +typedef unsigned char GoUint8; +typedef short GoInt16; +typedef unsigned short GoUint16; +typedef int GoInt32; +typedef unsigned int GoUint32; +typedef long long GoInt64; +typedef unsigned long long GoUint64; +typedef GoInt64 GoInt; +typedef GoUint64 GoUint; +typedef size_t GoUintptr; +typedef float GoFloat32; +typedef double GoFloat64; +#ifdef _MSC_VER +#include +typedef _Fcomplex GoComplex64; +typedef _Dcomplex GoComplex128; +#else +typedef float _Complex GoComplex64; +typedef double _Complex GoComplex128; +#endif + +/* + static assertion to make sure the file is being used on architecture + at least with matching size of GoInt. +*/ +typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1]; + +#ifndef GO_CGO_GOSTRING_TYPEDEF +typedef _GoString_ GoString; +#endif +typedef void *GoMap; +typedef void *GoChan; +typedef struct { void *t; void *v; } GoInterface; +typedef struct { void *data; GoInt len; GoInt cap; } GoSlice; + +#endif + +/* End of boilerplate cgo prologue. */ + +#ifdef __cplusplus +extern "C" { +#endif + +extern char IsBlackListed(char* in); + +#ifdef __cplusplus +} +#endif diff --git a/pkg/lib/lib.go b/pkg/lib/lib.go new file mode 100644 index 0000000..9c409f5 --- /dev/null +++ b/pkg/lib/lib.go @@ -0,0 +1,90 @@ +//go:build !js || js + +package lib + +import ( + "io" + "log" + "os" + "os/exec" +) + +func Bash(cmd string) error { + c := exec.Command("/bin/bash", "-c", cmd) + c.Stderr = os.Stderr + c.Stdout = os.Stdout + c.Stdin = os.Stdin + return c.Run() +} +func BashV(cmd string) error { + log.Println("cmd:", cmd) + return Bash(cmd) +} + +func BashOutput(cmd string) (string, error) { + out, err := exec.Command("/bin/bash", "-c", cmd).CombinedOutput() + return string(out), err +} + +func ReadFile(path string) (string, error) { + ret, err := os.ReadFile(path) + if err != nil { + return "", err + } + return string(ret), nil +} +func WriteFile(path, data string) error { + return os.WriteFile(path, []byte(data), 0644) +} +func AppendToFile(path, data string) error { + f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644) + if err != nil { + return err + } + + _, err = f.Write([]byte(data)) + f.Close() + return nil +} + +func CopyFile(src, dst string) (err error) { + in, err := os.Open(src) + if err != nil { + return + } + + defer in.Close() + + out, err := os.Create(dst) + if err != nil { + return + } + + defer func() { + cerr := out.Close() + if err == nil { + err = cerr + } + }() + + if _, err = io.Copy(out, in); err != nil { + return + } + + err = out.Sync() + + return +} + +func Plus(in bool) string { + if in { + return "+" + } + return "-" +} +func BoolToInt(in bool) int { + if in { + return 1 + } + return 0 +} diff --git a/pkg/sharedlib/lib.go b/pkg/sharedlib/lib.go new file mode 100644 index 0000000..346cf09 --- /dev/null +++ b/pkg/sharedlib/lib.go @@ -0,0 +1,21 @@ +package main + +import "strings" + +func isBlackListed(s string) bool { + for _, b := range *blockedList_case.GetList() { + if strings.Contains(s, b) { + return true + } + } + + s_low := strings.ToLower(s) + for _, b := range *blockedList.GetList() { + b = strings.ToLower(b) + if strings.Contains(s_low, b) { + return true + } + } + + return false +} diff --git a/pkg/sharedlib/main.go b/pkg/sharedlib/main.go new file mode 100644 index 0000000..55449b0 --- /dev/null +++ b/pkg/sharedlib/main.go @@ -0,0 +1,27 @@ +package main + +import "C" +import ( + "fmt" +) + +func init() { + fmt.Printf("KTGO: init\n") + + initStorage() +} + +//export IsBlackListed +func IsBlackListed(in *C.char) C.char { + m := C.GoString(in) + r := isBlackListed(m) + // fmt.Printf("KTGO: IsBlackListed: %v\n", r) + res := byte(0) + if r { + res = 1 + } + + return C.char(res) +} + +func main() {} diff --git a/pkg/sharedlib/storage.go b/pkg/sharedlib/storage.go new file mode 100644 index 0000000..4d8b525 --- /dev/null +++ b/pkg/sharedlib/storage.go @@ -0,0 +1,32 @@ +package main + +import ( + "log" + "my/ktgo/pkg/storage" +) + +const ( + DataDir = "/data" + BlockedListFileName = DataDir + "/blocked_list.txt" + BlockedListFileName_case = DataDir + "/blocked_list_case.txt" +) + +var ( + blockedList, blockedList_case *storage.Storage +) + +func initStorage() { + var err error + + blockedList, err = storage.NewStorage(BlockedListFileName) + if err != nil { + log.Fatalf("Storage: %v: %v\n", BlockedListFileName, err) + return + } + + blockedList_case, err = storage.NewStorage(BlockedListFileName_case) + if err != nil { + log.Fatalf("Storage: %v: %v\n", BlockedListFileName, err) + return + } +} diff --git a/pkg/storage/features/app.feature b/pkg/storage/features/app.feature new file mode 100644 index 0000000..cba3ddd --- /dev/null +++ b/pkg/storage/features/app.feature @@ -0,0 +1,15 @@ +# language: ru +Функциональность: Хранение списков + + Сценарий: Перечитывание при изменении + * Файл со списком: + ``` + строка1 + строка2 + строка3 + + ``` + * Хранилище + * В буфере строк: 3 + * В файл дописывается строка "строкаX" + * В буфере строк: 4 diff --git a/pkg/storage/init_test.go b/pkg/storage/init_test.go new file mode 100644 index 0000000..abe5129 --- /dev/null +++ b/pkg/storage/init_test.go @@ -0,0 +1,55 @@ +package storage + +import ( + "context" + "os" + "testing" + + . "my/ktgo/pkg/testlib" + + "github.com/cucumber/godog" + "github.com/cucumber/godog/colors" + + . "github.com/onsi/gomega" +) + +func InitializeScenario(ctx *godog.ScenarioContext) { + ctx.Step(`^Файл со списком:$`, файлСоСписком) + ctx.Step(`^Хранилище$`, хранилище) + ctx.Step(`^В буфере строк: (\d+)$`, вБуфереСтрок) + ctx.Step(`^В файл дописывается строка "([^"]*)"$`, вФайлДописываетсяСтрока) + + // ----------------------- + ctx.Before(func(ctx context.Context, sc *godog.Scenario) (context.Context, error) { + beforeScenario() + return ctx, nil + }) + ctx.After(func(ctx context.Context, sc *godog.Scenario, err error) (context.Context, error) { + afterScenario() + return ctx, nil + }) + InitializeGomegaForGodog(ctx) + _ = Ω +} + +func InitializeSuite(tsc *godog.TestSuiteContext) { + tsc.BeforeSuite(beforeSuite) + tsc.AfterSuite(afterSuite) +} + +func TestMain(t *testing.T) { + var opts = godog.Options{ + Output: colors.Colored(os.Stdout), + Strict: true, + StopOnFailure: true, + TestingT: t, + } + + godog.BindCommandLineFlags("godog.", &opts) + godog.TestSuite{ + Name: "app", + TestSuiteInitializer: InitializeSuite, + ScenarioInitializer: InitializeScenario, + Options: &opts, + }.Run() +} diff --git a/pkg/storage/storage.go b/pkg/storage/storage.go new file mode 100644 index 0000000..4136881 --- /dev/null +++ b/pkg/storage/storage.go @@ -0,0 +1,169 @@ +package storage + +import ( + "bufio" + "log" + "my/ktgo/pkg/lib" + "os" + "strings" + "sync" + "time" + + "github.com/fsnotify/fsnotify" +) + +type Storage struct { + filename string + fd *os.File + watcher *fsnotify.Watcher + list List + lock sync.RWMutex +} +type List []string + +func NewStorage(filename string) (*Storage, error) { + s := &Storage{ + filename: filename, + } + + err := s.reCreateWatcher() + if err != nil { + return nil, err + } + + return s, nil +} + +func (s *Storage) reReadFile() error { + fd, err := os.OpenFile(s.filename, os.O_RDONLY, 0) + if err != nil { + return err + } + + var newlist List + + scanner := bufio.NewScanner(fd) + for { + ok := scanner.Scan() + if !ok { + break + } + + line := scanner.Text() + newlist = append(newlist, line) + } + + s.lock.Lock() + defer s.lock.Unlock() + + s.list = newlist + + return nil +} + +func (s *Storage) reCreateWatcher() error { + go s.reCreateWatcher_sync() + return nil +} +func (s *Storage) reCreateWatcher_sync() error { + s.closeWatcher() + s.waitForAndReadFile() + + watcher, err := s.createNewWatcher() + if err != nil { + return err + } + + s.lock.Lock() + defer s.lock.Unlock() + + s.watcher = watcher + + go s.watcherListener() + + return nil +} + +func (s *Storage) waitForAndReadFile() { + for { + err := s.reReadFile() + if err == nil { + break + } + time.Sleep(100 * time.Millisecond) + } +} + +func (s *Storage) createNewWatcher() (*fsnotify.Watcher, error) { + watcher, err := fsnotify.NewWatcher() + if err != nil { + return nil, err + } + + err = watcher.Add(s.filename) + if err != nil { + return nil, err + } + return watcher, nil +} + +func (s *Storage) watcherListener() { + for { + select { + case event, ok := <-s.watcher.Events: + if !ok { + return + } + s.handleWatcherEvent(event) + case err, ok := <-s.watcher.Errors: + if !ok { + return + } + log.Println("error:", err) + } + } +} +func (s *Storage) handleWatcherEvent(event fsnotify.Event) { + log.Printf("Storage: %v: %v", s.filename, event.String()) + + s.onStorageUpdated() +} +func (s *Storage) onStorageUpdated() { + err := s.reCreateWatcher() + if err != nil { + log.Printf("Storage watcher creating: %v", err) + } +} + +func (s *Storage) GetList() *List { + s.lock.RLock() + defer s.lock.RUnlock() + + return &s.list +} + +func (s *Storage) AddLine(p, v string) error { + return nil +} +func (s *Storage) Save() error { + s.makeBackup() + return s.doSave() +} +func (s *Storage) doSave() error { + buf := strings.Join(s.list, "\n") + return lib.WriteFile(s.filename, buf) +} +func (s *Storage) makeBackup() { + cmd := "cp " + s.filename + " " + s.filename + ".backup" + lib.Bash(cmd) +} + +func (s *Storage) Close() { + s.closeWatcher() +} +func (s *Storage) closeWatcher() { + if s.watcher != nil { + s.watcher.Close() + s.watcher = nil + } +} diff --git a/pkg/storage/storage_test.go b/pkg/storage/storage_test.go new file mode 100644 index 0000000..ec71158 --- /dev/null +++ b/pkg/storage/storage_test.go @@ -0,0 +1,67 @@ +package storage + +import ( + "my/ktgo/pkg/lib" + . "my/ktgo/pkg/testlib" + "os" + "time" + + "github.com/cucumber/godog" + . "github.com/onsi/gomega" +) + +const ( + TestStorageFileName = "/tmp/test_storage.txt" +) + +type testData struct { + storage *Storage +} + +var ( + t *testData +) + +func resetTestData() { + t = &testData{} +} + +func beforeSuite() { + _ = Ω +} +func afterSuite() { +} + +func beforeScenario() { + resetTestData() +} +func afterScenario() { + os.RemoveAll(TestStorageFileName) +} + +// ----------------------- +func файлСоСписком(in *godog.DocString) { + err := lib.WriteFile(TestStorageFileName, in.Content) + Ok(err) +} +func хранилище() { + s, err := NewStorage(TestStorageFileName) + Ok(err) + t.storage = s + time.Sleep(10 * time.Millisecond) +} +func вБуфереСтрок(кво int) { + _, err := lib.ReadFile(TestStorageFileName) + Ok(err) + + // println("---content---") + // print(content) + // println("---content---") + + Ω(*t.storage.GetList()).To(HaveLen(кво), "GetList") +} +func вФайлДописываетсяСтрока(s string) { + err := lib.AppendToFile(TestStorageFileName, s+"\n") + Ok(err) + time.Sleep(10 * time.Millisecond) +} diff --git a/pkg/testlib/patching.go b/pkg/testlib/patching.go new file mode 100644 index 0000000..02a35ae --- /dev/null +++ b/pkg/testlib/patching.go @@ -0,0 +1,19 @@ +package testlib + +import ( + "unicode" + + . "github.com/agiledragon/gomonkey/v2" +) + +func Patch(target interface{}, methodName string, double interface{}) *Patches { + c := []rune(methodName)[0] + if unicode.IsUpper(c) { + return ApplyMethod(target, methodName, double) + } + return ApplyPrivateMethod(target, methodName, double) +} + +func PatchFunc(target interface{}, double interface{}) *Patches { + return ApplyFunc(target, double) +} diff --git a/pkg/testlib/testlib.go b/pkg/testlib/testlib.go new file mode 100644 index 0000000..0257cbe --- /dev/null +++ b/pkg/testlib/testlib.go @@ -0,0 +1,127 @@ +package testlib + +import ( + "context" + "fmt" + "math" + "net" + "strconv" + "time" + + "github.com/cucumber/godog" + . "github.com/onsi/gomega" +) + +var Be = Equal + +func NoErr(err error) { + ExpectWithOffset(1, err).NotTo(HaveOccurred()) +} +func NoErr2(err error, ошибка string) bool { + if ошибка == "" { + ExpectWithOffset(1, err).To(BeNil(), ошибка) + return false + } else { + ExpectWithOffset(1, err.Error()).To(Be(ошибка), ошибка) + return true + } +} + +var Ok = NoErr +var Ok2 = NoErr2 + +func Yes(b bool) { + ExpectWithOffset(1, b).To(BeTrue()) +} +func YesText(b bool, text string) { + ExpectWithOffset(1, b).To(BeTrue(), text) +} + +var TestResult error + +func InitializeGomegaForGodog(ctx *godog.ScenarioContext) { + ctx.StepContext().Before(func(ctx context.Context, st *godog.Step) (context.Context, error) { + TestResult = nil + return ctx, nil + }) + ctx.StepContext().After(func(ctx context.Context, st *godog.Step, status godog.StepResultStatus, err error) (context.Context, error) { + return ctx, TestResult + }) + RegisterFailHandler(func(message string, callerSkip ...int) { + // remember only the expectation failed first + // anything thereafter is not to be believed + if TestResult == nil { + TestResult = fmt.Errorf(message) + } + }) +} + +func Sleep(m time.Duration) { + time.Sleep(m * time.Millisecond) +} + +func Atoi(in string) int { + res, err := strconv.Atoi(in) + Ok(err) + return res +} +func Atof(in string) float64 { + res, err := strconv.ParseFloat(in, 64) + Ok(err) + res = RoundPlusMinus(res) + return res +} +func RoundPlusMinus(number float64) float64 { + return RoundFloat(number, 4) +} +func RoundFloat(number float64, roundingTempDigitsCount int) float64 { + temp := math.Pow(10, float64(roundingTempDigitsCount)) + return math.Round(number*temp) / temp +} + +func Stof(znak string, s1, s2 int) float64 { + l := fmt.Sprintf("%v.%v", s1, s2) + r := Atof(l) + if znak == "-" { + r = -r + } + return r +} + +func WaitForOpenPort(sp string) { + start_time_limit := time.Now().Add(5 * time.Second) + + for { + time.Sleep(5 * time.Millisecond) + + Conn, err := net.Dial("tcp4", "localhost:"+sp) + if err == nil { + Conn.Close() + break + } + + if start_time_limit.Before(time.Now()) { + Ω(false).To(BeTrue(), "Timeout to wait for process port: "+sp) + } + } +} + +func WaitForOpenPortInt(p int) { + sp := fmt.Sprint(p) + WaitForOpenPort(sp) +} + +func IsTrue(in string) bool { + switch in { + case "t", "true", "+", "да": + return true + } + return false +} +func Истина(in string) bool { + switch in { + case "t", "true", "+", "да": + return true + } + return false +}