Browse Source

utils移动到common库下的一个包中

pull/1/head
Sydonian 2 years ago
parent
commit
12fab80e35
18 changed files with 1116 additions and 0 deletions
  1. +55
    -0
      go.mod
  2. +122
    -0
      go.sum
  3. +80
    -0
      utils/config.go
  4. +38
    -0
      utils/config/config.go
  5. +120
    -0
      utils/grpc/file_transport.go
  6. +64
    -0
      utils/io/io.go
  7. +5
    -0
      utils/ipfs/config.go
  8. +91
    -0
      utils/ipfs/ipfs.go
  9. +8
    -0
      utils/logger/config.go
  10. +126
    -0
      utils/logger/logger.go
  11. +19
    -0
      utils/math/math.go
  12. +82
    -0
      utils/ping.go
  13. +13
    -0
      utils/reflect/reflect.go
  14. +108
    -0
      utils/serder/serder.go
  15. +72
    -0
      utils/serder/serder_test.go
  16. +43
    -0
      utils/serder/type_name_resolver.go
  17. +62
    -0
      utils/sort/sort.go
  18. +8
    -0
      utils/utils.go

+ 55
- 0
go.mod View File

@@ -1,3 +1,58 @@
module gitlink.org.cn/cloudream/common

go 1.18

require (
github.com/antonfisher/nested-logrus-formatter v1.3.1
github.com/beevik/etree v1.2.0
github.com/go-ping/ping v1.1.0
github.com/imdario/mergo v0.3.15
github.com/ipfs/go-ipfs-api v0.6.0
github.com/mitchellh/mapstructure v1.5.0
github.com/sirupsen/logrus v1.9.2
github.com/smartystreets/goconvey v1.8.0
gitlink.org.cn/cloudream/proto v0.0.0
golang.org/x/exp v0.0.0-20230519143937-03e91628a987
)

require (
github.com/benbjohnson/clock v1.3.0 // indirect
github.com/crackcomm/go-gitignore v0.0.0-20170627025303-887ab5e44cc3 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gopherjs/gopherjs v1.17.2 // indirect
github.com/ipfs/boxo v0.8.0 // indirect
github.com/ipfs/go-cid v0.4.0 // indirect
github.com/jtolds/gls v4.20.0+incompatible // indirect
github.com/klauspost/cpuid/v2 v2.2.3 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/libp2p/go-flow-metrics v0.1.0 // indirect
github.com/libp2p/go-libp2p v0.26.3 // indirect
github.com/minio/sha256-simd v1.0.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/multiformats/go-base32 v0.1.0 // indirect
github.com/multiformats/go-base36 v0.2.0 // indirect
github.com/multiformats/go-multiaddr v0.8.0 // indirect
github.com/multiformats/go-multibase v0.1.1 // indirect
github.com/multiformats/go-multicodec v0.8.1 // indirect
github.com/multiformats/go-multihash v0.2.1 // indirect
github.com/multiformats/go-multistream v0.4.1 // indirect
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/smartystreets/assertions v1.13.1 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/whyrusleeping/tar-utils v0.0.0-20180509141711-8c6c8ba81d5c // indirect
golang.org/x/crypto v0.6.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
google.golang.org/genproto v0.0.0-20230403163135-c38d8f061ccd // indirect
google.golang.org/grpc v1.54.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
lukechampine.com/blake3 v1.1.7 // indirect
)

// 运行go mod tidy时需要将下面几行取消注释
replace gitlink.org.cn/cloudream/proto => ../proto

+ 122
- 0
go.sum View File

@@ -0,0 +1,122 @@
github.com/antonfisher/nested-logrus-formatter v1.3.1 h1:NFJIr+pzwv5QLHTPyKz9UMEoHck02Q9L0FP13b/xSbQ=
github.com/antonfisher/nested-logrus-formatter v1.3.1/go.mod h1:6WTfyWFkBc9+zyBaKIqRrg/KwMqBbodBjgbHjDz7zjA=
github.com/beevik/etree v1.2.0 h1:l7WETslUG/T+xOPs47dtd6jov2Ii/8/OjCldk5fYfQw=
github.com/beevik/etree v1.2.0/go.mod h1:aiPf89g/1k3AShMVAzriilpcE4R/Vuor90y83zVZWFc=
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/cheekybits/is v0.0.0-20150225183255-68e9c0620927 h1:SKI1/fuSdodxmNNyVBR8d7X/HuLnRpvvFO0AgyQk764=
github.com/crackcomm/go-gitignore v0.0.0-20170627025303-887ab5e44cc3 h1:HVTnpeuvF6Owjd5mniCL8DEXo7uYXdQEmOP4FJbV5tg=
github.com/crackcomm/go-gitignore v0.0.0-20170627025303-887ab5e44cc3/go.mod h1:p1d6YEZWvFzEh4KLyvBcVSnrfNDDvK2zfK/4x2v/4pE=
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/decred/dcrd/crypto/blake256 v1.0.0 h1:/8DMNYp9SGi5f0w7uCm6d6M4OU2rGFK09Y2A4Xv7EE0=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 h1:HbphB4TFFXpv7MNrT52FGrrgVXF1owhMVTHFZIlnvd4=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0/go.mod h1:DZGJHZMqrU4JJqFAWUS2UO1+lbSKsdiOoYi9Zzey7Fc=
github.com/go-ping/ping v1.1.0 h1:3MCGhVX4fyEUuhsfwPrsEdQw6xspHkv5zHsiSoDFZYw=
github.com/go-ping/ping v1.1.0/go.mod h1:xIFjORFzTxqIV/tDVGO4eDy/bLuSyawEeojSm3GfRGk=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gopherjs/gopherjs v1.17.2 h1:fQnZVsXk8uxXIStYb0N4bGk7jeyTalG/wsZjQ25dO0g=
github.com/gopherjs/gopherjs v1.17.2/go.mod h1:pRRIvn/QzFLrKfvEz3qUuEhtE/zLCWfreZ6J5gM2i+k=
github.com/imdario/mergo v0.3.15 h1:M8XP7IuFNsqUx6VPK2P9OSmsYsI/YFaGil0uD21V3dM=
github.com/imdario/mergo v0.3.15/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
github.com/ipfs/boxo v0.8.0 h1:UdjAJmHzQHo/j3g3b1bAcAXCj/GM6iTwvSlBDvPBNBs=
github.com/ipfs/boxo v0.8.0/go.mod h1:RIsi4CnTyQ7AUsNn5gXljJYZlQrHBMnJp94p73liFiA=
github.com/ipfs/go-cid v0.4.0 h1:a4pdZq0sx6ZSxbCizebnKiMCx/xI/aBBFlB73IgH4rA=
github.com/ipfs/go-cid v0.4.0/go.mod h1:uQHwDeX4c6CtyrFwdqyhpNcxVewur1M7l7fNU7LKwZk=
github.com/ipfs/go-ipfs-api v0.6.0 h1:JARgG0VTbjyVhO5ZfesnbXv9wTcMvoKRBLF1SzJqzmg=
github.com/ipfs/go-ipfs-api v0.6.0/go.mod h1:iDC2VMwN9LUpQV/GzEeZ2zNqd8NUdRmWcFM+K/6odf0=
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/klauspost/cpuid/v2 v2.0.4/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.3 h1:sxCkb+qR91z4vsqw4vGGZlDgPz3G7gjaLyK3V8y70BU=
github.com/klauspost/cpuid/v2 v2.2.3/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
github.com/libp2p/go-buffer-pool v0.1.0 h1:oK4mSFcQz7cTQIfqbe4MIj9gLW+mnanjyFtc6cdF0Y8=
github.com/libp2p/go-buffer-pool v0.1.0/go.mod h1:N+vh8gMqimBzdKkSMVuydVDq+UV5QTWy5HSiZacSbPg=
github.com/libp2p/go-flow-metrics v0.1.0 h1:0iPhMI8PskQwzh57jB9WxIuIOQ0r+15PChFGkx3Q3WM=
github.com/libp2p/go-flow-metrics v0.1.0/go.mod h1:4Xi8MX8wj5aWNDAZttg6UPmc0ZrnFNsMtpsYUClFtro=
github.com/libp2p/go-libp2p v0.26.3 h1:6g/psubqwdaBqNNoidbRKSTBEYgaOuKBhHl8Q5tO+PM=
github.com/libp2p/go-libp2p v0.26.3/go.mod h1:x75BN32YbwuY0Awm2Uix4d4KOz+/4piInkp4Wr3yOo8=
github.com/minio/sha256-simd v1.0.0 h1:v1ta+49hkWZyvaKwrQB8elexRqm6Y0aMLjCNsrYxo6g=
github.com/minio/sha256-simd v1.0.0/go.mod h1:OuYzVNI5vcoYIAmbIvHPl3N3jUzVedXbKy5RFepssQM=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o=
github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
github.com/multiformats/go-base32 v0.1.0 h1:pVx9xoSPqEIQG8o+UbAe7DNi51oej1NtK+aGkbLYxPE=
github.com/multiformats/go-base32 v0.1.0/go.mod h1:Kj3tFY6zNr+ABYMqeUNeGvkIC/UYgtWibDcT0rExnbI=
github.com/multiformats/go-base36 v0.2.0 h1:lFsAbNOGeKtuKozrtBsAkSVhv1p9D0/qedU9rQyccr0=
github.com/multiformats/go-base36 v0.2.0/go.mod h1:qvnKE++v+2MWCfePClUEjE78Z7P2a1UV0xHgWc0hkp4=
github.com/multiformats/go-multiaddr v0.8.0 h1:aqjksEcqK+iD/Foe1RRFsGZh8+XFiGo7FgUCZlpv3LU=
github.com/multiformats/go-multiaddr v0.8.0/go.mod h1:Fs50eBDWvZu+l3/9S6xAE7ZYj6yhxlvaVZjakWN7xRs=
github.com/multiformats/go-multibase v0.1.1 h1:3ASCDsuLX8+j4kx58qnJ4YFq/JWTJpCyDW27ztsVTOI=
github.com/multiformats/go-multibase v0.1.1/go.mod h1:ZEjHE+IsUrgp5mhlEAYjMtZwK1k4haNkcaPg9aoe1a8=
github.com/multiformats/go-multicodec v0.8.1 h1:ycepHwavHafh3grIbR1jIXnKCsFm0fqsfEOsJ8NtKE8=
github.com/multiformats/go-multicodec v0.8.1/go.mod h1:L3QTQvMIaVBkXOXXtVmYE+LI16i14xuaojr/H7Ai54k=
github.com/multiformats/go-multihash v0.2.1 h1:aem8ZT0VA2nCHHk7bPJ1BjUbHNciqZC/d16Vve9l108=
github.com/multiformats/go-multihash v0.2.1/go.mod h1:WxoMcYG85AZVQUyRyo9s4wULvW5qrI9vb2Lt6evduFc=
github.com/multiformats/go-multistream v0.4.1 h1:rFy0Iiyn3YT0asivDUIR05leAdwZq3de4741sbiSdfo=
github.com/multiformats/go-multistream v0.4.1/go.mod h1:Mz5eykRVAjJWckE2U78c6xqdtyNUEhKSM0Lwar2p77Q=
github.com/multiformats/go-varint v0.0.7 h1:sWSGR+f/eu5ABZA2ZpYKBILXTTs9JWpdEM/nEGOHFS8=
github.com/multiformats/go-varint v0.0.7/go.mod h1:r8PUYw/fD/SjBCiKOoDlGF6QawOELpZAu9eioSos/OU=
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/sirupsen/logrus v1.9.2 h1:oxx1eChJGI6Uks2ZC4W1zpLlVgqB8ner4EuQwV4Ik1Y=
github.com/sirupsen/logrus v1.9.2/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/smartystreets/assertions v1.13.1 h1:Ef7KhSmjZcK6AVf9YbJdvPYG9avaF0ZxudX+ThRdWfU=
github.com/smartystreets/assertions v1.13.1/go.mod h1:cXr/IwVfSo/RbCSPhoAPv73p3hlSdrBH/b3SdnW/LMY=
github.com/smartystreets/goconvey v1.8.0 h1:Oi49ha/2MURE0WexF052Z0m+BNSGirfjg5RL+JXWq3w=
github.com/smartystreets/goconvey v1.8.0/go.mod h1:EdX8jtrTIj26jmjCOVNMVSIYAtgexqXKHOXW2Dx9JLg=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/whyrusleeping/tar-utils v0.0.0-20180509141711-8c6c8ba81d5c h1:GGsyl0dZ2jJgVT+VvWBf/cNijrHRhkrTjkmp5wg7li0=
github.com/whyrusleeping/tar-utils v0.0.0-20180509141711-8c6c8ba81d5c/go.mod h1:xxcJeBb7SIUl/Wzkz1eVKJE/CB34YNrqX2TQI6jY9zs=
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/exp v0.0.0-20230519143937-03e91628a987 h1:3xJIFvzUFbu4ls0BTBYcgbCGhA63eAOEMxIHugyXJqA=
golang.org/x/exp v0.0.0-20230519143937-03e91628a987/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc=
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/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/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
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/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto v0.0.0-20230403163135-c38d8f061ccd h1:sLpv7bNL1AsX3fdnWh9WVh7ejIzXdOc1RRHGeAmeStU=
google.golang.org/genproto v0.0.0-20230403163135-c38d8f061ccd/go.mod h1:UUQDJDOlWu4KYeJZffbWgBkS1YFobzKbLVfK69pe0Ak=
google.golang.org/grpc v1.54.0 h1:EhTqbhiYeixwWQtAEZAxmV9MGqcjEU2mFx52xCzNyag=
google.golang.org/grpc v1.54.0/go.mod h1:PUSEXI6iWghWaB6lXM4knEgpJNu2qUcKfDtNci3EC2g=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
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=
lukechampine.com/blake3 v1.1.7 h1:GgRMhmdsuK8+ii6UZFDL8Nb+VyMwadAgcJyfYHxG6n0=
lukechampine.com/blake3 v1.1.7/go.mod h1:tkKEOtDkNtklkXtLNEOGNq5tcV90tJiA1vAA12R78LA=

+ 80
- 0
utils/config.go View File

@@ -0,0 +1,80 @@
package utils

import (
"fmt"
"strconv"
"regexp"
"github.com/beevik/etree"
)

type EcConfig struct{
ecid string `xml:"ecid"`
class string `xml:"class"`
n int `xml:"n"`
k int `xml:"k"`
w int `xml:"w"`
opt int `xml:"opt"`
}

func (r *EcConfig)GetK() int{
return r.k
}

func (r *EcConfig)GetN() int{
return r.n
}

func GetEcPolicy() *map[string]EcConfig{
doc := etree.NewDocument()
if err := doc.ReadFromFile("../../conf/sysSetting.xml"); err != nil {
panic(err)
}
ecMap := make(map[string]EcConfig, 20)
root := doc.SelectElement("setting")
for _, attr := range root.SelectElements("attribute") {
if name := attr.SelectElement("name"); name.Text() == "ec.policy"{
for _, eci := range attr.SelectElements("value") {
tt := EcConfig{}
tt.ecid = eci.SelectElement("ecid").Text()
tt.class = eci.SelectElement("class").Text()
tt.n,_ = strconv.Atoi(eci.SelectElement("n").Text())
tt.k,_ = strconv.Atoi(eci.SelectElement("k").Text())
tt.w,_ = strconv.Atoi(eci.SelectElement("w").Text())
tt.opt,_ = strconv.Atoi(eci.SelectElement("opt").Text())
ecMap[tt.ecid] = tt
}
}
}
fmt.Println(ecMap)
return &ecMap
//
}

func GetAgentIps() []string{
doc := etree.NewDocument()
if err := doc.ReadFromFile("../../conf/sysSetting.xml"); err != nil {
panic(err)
}
root := doc.SelectElement("setting")
var ips []string // 定义存储 IP 的字符串切片

for _, attr := range root.SelectElements("attribute") {
if name := attr.SelectElement("name"); name.Text() == "agents.addr"{
for _, ip := range attr.SelectElements("value") {
ipRegex := regexp.MustCompile(`\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b`)
match := ipRegex.FindString(ip.Text())
print(match)
ips = append(ips, match)
}
}
}

return ips
}








+ 38
- 0
utils/config/config.go View File

@@ -0,0 +1,38 @@
package config

import (
"encoding/json"
"fmt"
"os"
"path/filepath"

"github.com/imdario/mergo"
)

// Load 加载配置文件
func Load(filePath string, cfg interface{}) error {
fileData, err := os.ReadFile(filePath)
if err != nil {
return err
}

return json.Unmarshal(fileData, cfg)
}

// DefaultLoad 默认的加载配置的方式:
// 从应用程序上上级的conf目录中读取,文件名:<moduleName>.config.json
func DefaultLoad(modeulName string, defCfg interface{}) error {
execPath, err := os.Executable()
if err != nil {
return err
}

// TODO 可以考虑根据环境变量读取不同的配置
configFilePath := filepath.Join(filepath.Dir(execPath), "..", "conf", fmt.Sprintf("%s.config.json", modeulName))
return Load(configFilePath, defCfg)
}

// Merge 合并两个配置结构体。会将src中的非空字段覆盖到dst的同名字段中。两个结构的类型必须相同
func Merge(dst interface{}, src interface{}) error {
return mergo.Merge(dst, src)
}

+ 120
- 0
utils/grpc/file_transport.go View File

@@ -0,0 +1,120 @@
package grpc

import (
"context"
"fmt"
"io"

myio "gitlink.org.cn/cloudream/common/utils/io"
"gitlink.org.cn/cloudream/proto"
)

type fileReadCloser struct {
io.ReadCloser
stream proto.FileTransport_GetFileClient
cancelFn context.CancelFunc
readData []byte
}

func (s *fileReadCloser) Read(p []byte) (int, error) {

if s.readData == nil {
resp, err := s.stream.Recv()
if err != nil {
return 0, err
}

if resp.Type == proto.FileDataPacketType_Data {
s.readData = resp.Data

} else if resp.Type == proto.FileDataPacketType_EOF {
return 0, io.EOF

} else {
return 0, fmt.Errorf("unsuppoted packt type: %v", resp.Type)
}
}

cnt := copy(p, s.readData)

if len(s.readData) == cnt {
s.readData = nil
} else {
s.readData = s.readData[cnt:]
}

return cnt, nil
}

func (s *fileReadCloser) Close() error {
s.cancelFn()

return nil
}

func GetFileAsStream(client proto.FileTransportClient, fileHash string) (io.ReadCloser, error) {
ctx, cancel := context.WithCancel(context.Background())

stream, err := client.GetFile(ctx, &proto.GetReq{
FileHash: fileHash,
})
if err != nil {
cancel()
return nil, fmt.Errorf("request grpc failed, err: %w", err)
}

return &fileReadCloser{
stream: stream,
cancelFn: cancel,
}, nil
}

type fileWriteCloser struct {
myio.PromiseWriteCloser[string]
stream proto.FileTransport_SendFileClient
}

func (s *fileWriteCloser) Write(p []byte) (int, error) {
err := s.stream.Send(&proto.FileDataPacket{
Type: proto.FileDataPacketType_Data,
Data: p,
})

if err != nil {
return 0, err
}

return len(p), nil
}

func (s *fileWriteCloser) Abort(err error) {
s.stream.CloseSend()
}

func (s *fileWriteCloser) Finish() (string, error) {
err := s.stream.Send(&proto.FileDataPacket{
Type: proto.FileDataPacketType_EOF,
})

if err != nil {
return "", fmt.Errorf("send EOF packet failed, err: %w", err)
}

resp, err := s.stream.CloseAndRecv()
if err != nil {
return "", fmt.Errorf("receive response failed, err: %w", err)
}

return resp.FileHash, nil
}

func SendFileAsStream(client proto.FileTransportClient) (myio.PromiseWriteCloser[string], error) {
stream, err := client.SendFile(context.Background())
if err != nil {
return nil, err
}

return &fileWriteCloser{
stream: stream,
}, nil
}

+ 64
- 0
utils/io/io.go View File

@@ -0,0 +1,64 @@
package io

import "io"

type PromiseWriteCloser[T any] interface {
io.Writer
Abort(err error) // 中断发送文件
Finish() (T, error) // 发送文件完成,等待返回结果
}

func WriteAll(writer io.Writer, data []byte) error {
pos := 0
dataLen := len(data)

for pos < dataLen {
writeLen, err := writer.Write(data[pos:])
if err != nil {
return err
}

pos += writeLen
}

return nil
}

type readCloserHook struct {
readCloser io.ReadCloser
callback func(closer io.ReadCloser)
isBefore bool // callback调用时机,true则在closer的Close之前调用
}

func (hook *readCloserHook) Read(buf []byte) (n int, err error) {
return hook.readCloser.Read(buf)
}

func (hook *readCloserHook) Close() error {
if hook.isBefore {
hook.callback(hook.readCloser)
}

err := hook.readCloser.Close()

if !hook.isBefore {
hook.callback(hook.readCloser)
}
return err
}

func BeforeReadClosing(closer io.ReadCloser, callback func(closer io.ReadCloser)) io.ReadCloser {
return &readCloserHook{
readCloser: closer,
callback: callback,
isBefore: true,
}
}

func AfterReadClosed(closer io.ReadCloser, callback func(closer io.ReadCloser)) io.ReadCloser {
return &readCloserHook{
readCloser: closer,
callback: callback,
isBefore: false,
}
}

+ 5
- 0
utils/ipfs/config.go View File

@@ -0,0 +1,5 @@
package ipfs

type Config struct {
Port int `json:"port"`
}

+ 91
- 0
utils/ipfs/ipfs.go View File

@@ -0,0 +1,91 @@
package ipfs

import (
"fmt"
"io"

shell "github.com/ipfs/go-ipfs-api"
myio "gitlink.org.cn/cloudream/common/utils/io"
)

type IPFS struct {
shell *shell.Shell
}

func NewIPFS(cfg *Config) (*IPFS, error) {
ipfsAddr := fmt.Sprintf("localhost:%d", cfg.Port)
sh := shell.NewShell(ipfsAddr)

// 检测连通性
if !sh.IsUp() {
return nil, fmt.Errorf("cannot connect to %s", ipfsAddr)
}

return &IPFS{
shell: sh,
}, nil
}

func (fs *IPFS) IsUp() bool {
return fs.shell.IsUp()
}

func (fs *IPFS) CreateFile() (myio.PromiseWriteCloser[string], error) {
pr, pw := io.Pipe()

ipfsWriter := ipfsWriter{
writer: pw,
finished: make(chan any, 1),
}

go func() {
hash, err := fs.shell.Add(pr)
ipfsWriter.finishErr = err
ipfsWriter.fileHash = hash
close(ipfsWriter.finished)
pr.CloseWithError(err)
}()

return &ipfsWriter, nil
}

func (fs *IPFS) OpenRead(hash string) (io.ReadCloser, error) {
return fs.shell.Cat(hash)
}

func (fs *IPFS) Pin(hash string) error {
return fs.shell.Pin(hash)
}

func (fs *IPFS) Unpin(hash string) error {
return fs.shell.Unpin(hash)
}

type ipfsWriter struct {
writer *io.PipeWriter
finished chan any
finishErr error
fileHash string
}

func (p *ipfsWriter) Write(data []byte) (n int, err error) {
return p.writer.Write(data)
}

// 设置一个error中断写入
func (w *ipfsWriter) Abort(err error) {
w.writer.CloseWithError(err)
}

// Finish 结束写入,并获得返回值(文件哈希值)
func (w *ipfsWriter) Finish() (string, error) {
w.writer.CloseWithError(io.EOF)

<-w.finished

return w.fileHash, w.finishErr
}

func IPFSRemoteDeamonDetector() { //探测本地IPFS Deamon与目的地IPFS Deamon的连接状态

}

+ 8
- 0
utils/logger/config.go View File

@@ -0,0 +1,8 @@
package logger

type Config struct {
Output string `json:"output"` // 输出日志的方式。file:输出到文件,stdout:输出到标准输出
OutputFileName string `json:"outputFileName"` // 输出日志的文件名,只在Output字段为file时有意义
OutputDirectory string `json:"outputDirectory"` // 输出日志的目录,只在Output字段为file时有意义
Level string `json:"level"`
}

+ 126
- 0
utils/logger/logger.go View File

@@ -0,0 +1,126 @@
package logger

import (
"fmt"
"os"
"path/filepath"
"strings"

nested "github.com/antonfisher/nested-logrus-formatter"
"github.com/sirupsen/logrus"
)

const (
TRACE_LEVEL = "TRACE"
DEBUG_LEVEL = "DEBUG"
INFO_LEVEL = "INFO"
WARN_LEVEL = "WARN"
ERROR_LEVEL = "ERROR"
FATAL_LEVEL = "FATAL"
PANIC_LEVEL = "PANIC"

OUTPUT_FILE = "FILE"
OUTPUT_STDOUT = "STDOUT"
)

var loggerLevels = map[string]logrus.Level{
TRACE_LEVEL: logrus.TraceLevel,
DEBUG_LEVEL: logrus.DebugLevel,
INFO_LEVEL: logrus.InfoLevel,
WARN_LEVEL: logrus.WarnLevel,
ERROR_LEVEL: logrus.ErrorLevel,
FATAL_LEVEL: logrus.FatalLevel,
PANIC_LEVEL: logrus.PanicLevel,
}

// Init 初始化全局默认的日志器
func Init(cfg *Config) error {
logrus.SetFormatter(&nested.Formatter{
TimestampFormat: "2006-01-02 15:04:05",
NoColors: true,
NoFieldsColors: true,
})

level, ok := loggerLevels[strings.ToUpper(cfg.Level)]
if !ok {
return fmt.Errorf("invalid log level: %s", cfg.Level)
}

logrus.SetLevel(level)

output := strings.ToUpper(cfg.Output)

if output == OUTPUT_FILE {
logFilePath := filepath.Join(cfg.OutputDirectory, cfg.OutputFileName+".log")

if err := os.MkdirAll(cfg.OutputDirectory, 0644); err != nil {
return err
}

file, err := os.OpenFile(logFilePath, os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
return err
}
logrus.SetOutput(file)

} else if output == OUTPUT_STDOUT {
logrus.SetOutput(os.Stdout)
} else {
logrus.SetOutput(os.Stdout)
logrus.Warnf("unsupported output: %s, will output to stdout", output)
}

return nil
}

func Debug(args ...interface{}) {
logrus.Debug(args...)
}

func Debugf(format string, args ...interface{}) {
logrus.Debugf(format, args...)
}

func Info(args ...interface{}) {
logrus.Info(args...)
}

func Infof(format string, args ...interface{}) {
logrus.Infof(format, args...)
}

func Warn(args ...interface{}) {
logrus.Warn(args...)
}

func Warnf(format string, args ...interface{}) {
logrus.Warnf(format, args...)
}

func Error(args ...interface{}) {
logrus.Error(args...)
}

func Errorf(format string, args ...interface{}) {
logrus.Errorf(format, args...)
}

func Fatal(args ...interface{}) {
logrus.Fatal(args...)
}

func Fatalf(format string, args ...interface{}) {
logrus.Fatalf(format, args...)
}

func Panic(args ...interface{}) {
logrus.Panic(args...)
}

func Panicf(format string, args ...interface{}) {
logrus.Panicf(format, args...)
}

func WithField(key string, val any) *logrus.Entry {
return logrus.WithField(key, val)
}

+ 19
- 0
utils/math/math.go View File

@@ -0,0 +1,19 @@
package math

import "golang.org/x/exp/constraints"

func Max[T constraints.Ordered](v1, v2 T) T {
if v1 < v2 {
return v2
}

return v1
}

func Min[T constraints.Ordered](v1, v2 T) T {
if v1 < v2 {
return v1
}

return v2
}

+ 82
- 0
utils/ping.go View File

@@ -0,0 +1,82 @@
package utils

import (
//"fmt"
"github.com/go-ping/ping"
//"net"
"io/ioutil"
"net/http"
"strings"
"time"
)

type ConnStatus struct {
Addr string
IsReachable bool
Delay time.Duration
TTL int
}

// 获取本地主机 IP 地址
func getLocalIP() string {
resp, err := http.Get("https://api.ipify.org")
if err != nil {
panic(err)
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}

ip := strings.TrimSpace(string(body))
return ip
}

func GetConnStatus(remoteIP string) (*ConnStatus, error) {
// 本地主机 IP 地址
//localIP := getLocalIP()
//print("!@#@#!")
//print(localIP)
conn := ConnStatus{
Addr: remoteIP,
IsReachable: false,
}
pinger, err := ping.NewPinger(remoteIP)

if err != nil {
return nil, err
}
pinger.Count = 5 // 设置 ping 次数为 5
// pinger.Interval = 1 // 设置 ping 时间间隔为 1 秒
//pinger.Timeout = 2 // 设置 ping 超时时间为 2 秒
//pinger.SetPrivileged(true) // 设置使用特权模式以获取 TTL 值
pinger.OnRecv = func(pkt *ping.Packet) {
//fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v ttl=%v (DUP!)\n",
// pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt, pkt.Ttl)
conn.TTL = pkt.Ttl
}

/*pinger.OnDuplicateRecv = func(pkt *ping.Packet) {
fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v ttl=%v (DUP!)\n",
pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt, pkt.Ttl)
}*/

pinger.OnFinish = func(stats *ping.Statistics) {
//fmt.Printf("\n--- %s ping statistics ---\n", stats.Addr)
//fmt.Printf("%d packets transmitted, %d packets received, %v%% packet loss\n",
// stats.PacketsSent, stats.PacketsRecv, stats.PacketLoss)
//fmt.Printf("round-trip min/avg/max/stddev = %v/%v/%v/%v\n",
// stats.MinRtt, stats.AvgRtt, stats.MaxRtt, stats.StdDevRtt)
if stats.PacketLoss == 0.0 {
conn.IsReachable = true
}
conn.Delay = stats.AvgRtt
}
err = pinger.Run() // Blocks until finished.
if err != nil {
return nil, err
}
return &conn, nil
}

+ 13
- 0
utils/reflect/reflect.go View File

@@ -0,0 +1,13 @@
package reflect

import "reflect"

// GetGenericType 获得泛型的类型
func GetGenericType[T any]() reflect.Type {
return reflect.TypeOf([0]T{}).Elem()
}

// GetGenericElemType 获得泛型的类型。适用于数组、指针类型
func GetGenericElemType[T any]() reflect.Type {
return reflect.TypeOf([0]T{}).Elem().Elem()
}

+ 108
- 0
utils/serder/serder.go View File

@@ -0,0 +1,108 @@
package serder

import (
"encoding/json"
"fmt"
"reflect"

mp "github.com/mitchellh/mapstructure"
)

func ObjectToJSON(obj any) ([]byte, error) {
return json.Marshal(obj)
}

func JSONToObject(data []byte, obj any) error {
return json.Unmarshal(data, obj)
}

type TypeResolver interface {
TypeToString(typ reflect.Type) (string, error)
StringToType(typeStr string) (reflect.Type, error)
}

type TypedSerderOption struct {
TypeResolver TypeResolver
TypeFieldName string
}

func MapToObject(m map[string]any, obj any) error {
config := &mp.DecoderConfig{
TagName: "json",
Squash: true,
WeaklyTypedInput: true,
Result: obj,
}

decoder, err := mp.NewDecoder(config)
if err != nil {
return err
}

return decoder.Decode(m)
}

func ObjectToMap(obj any) (map[string]any, error) {
var retMap map[string]any
config := &mp.DecoderConfig{
TagName: "json",
Squash: true,
WeaklyTypedInput: true,
Result: &retMap,
}

decoder, err := mp.NewDecoder(config)
if err != nil {
return nil, err
}

err = decoder.Decode(obj)
return retMap, err
}

func TypedMapToObject(m map[string]any, opt TypedSerderOption) (any, error) {

typeVal, ok := m[opt.TypeFieldName]
if !ok {
return nil, fmt.Errorf("no type field in the map")
}

typeStr, ok := typeVal.(string)
if !ok {
return nil, fmt.Errorf("type is not a string")
}

typ, err := opt.TypeResolver.StringToType(typeStr)
if err != nil {
return nil, fmt.Errorf("get type from string failed, err: %w", err)
}

val := reflect.New(typ)

valPtr := val.Interface()
err = MapToObject(m, valPtr)
if err != nil {
return nil, err
}

return val.Elem().Interface(), nil
}

func ObjectToTypedMap(obj any, opt TypedSerderOption) (map[string]any, error) {
mp, err := ObjectToMap(obj)
if err != nil {
return nil, err
}

_, ok := mp[opt.TypeFieldName]
if ok {
return nil, fmt.Errorf("object has the same field as the type field")
}

mp[opt.TypeFieldName], err = opt.TypeResolver.TypeToString(reflect.TypeOf(obj))
if err != nil {
return nil, fmt.Errorf("get string from type failed, err: %w", err)
}

return mp, nil
}

+ 72
- 0
utils/serder/serder_test.go View File

@@ -0,0 +1,72 @@
package serder

import (
"testing"

. "github.com/smartystreets/goconvey/convey"
myreflect "gitlink.org.cn/cloudream/common/utils/reflect"
)

func Test_MapToObject(t *testing.T) {
Convey("包含用字符串保存的int数据", t, func() {
type Struct struct {
A string `json:"a"`
B int `json:"b"`
C int64 `json:"c,string"`
}

mp := map[string]any{
"a": "a",
"b": 1,
"c": "1234",
}

var st Struct

err := MapToObject(mp, &st)
So(err, ShouldBeNil)

So(st.A, ShouldEqual, "a")
So(st.B, ShouldEqual, 1)
So(st.C, ShouldEqual, 1234)
})

}

func Test_TypedMapToObject(t *testing.T) {
type Struct struct {
A string `json:"a"`
B int `json:"b"`
C int64 `json:"c,string"`
}

nameResovler := NewTypeNameResolver(true)
nameResovler.Register(myreflect.GetGenericType[Struct]())

Convey("结构体", t, func() {
st := Struct{
A: "a",
B: 1,
C: 2,
}

mp, err := ObjectToTypedMap(st, TypedSerderOption{
TypeResolver: &nameResovler,
TypeFieldName: "@type",
})

So(err, ShouldBeNil)

st2Ptr, err := TypedMapToObject(mp, TypedSerderOption{
TypeResolver: &nameResovler,
TypeFieldName: "@type",
})
So(err, ShouldBeNil)

st2, ok := st2Ptr.(Struct)
So(ok, ShouldBeTrue)
So(st2, ShouldHaveSameTypeAs, st)
So(st2, ShouldResemble, st)
})

}

+ 43
- 0
utils/serder/type_name_resolver.go View File

@@ -0,0 +1,43 @@
package serder

import (
"fmt"
"reflect"
)

type TypeNameResolver struct {
includePackagePath bool
types map[string]reflect.Type
}

func NewTypeNameResolver(includePackagePath bool) TypeNameResolver {
return TypeNameResolver{
includePackagePath: includePackagePath,
types: make(map[string]reflect.Type),
}
}

func (r *TypeNameResolver) Register(typ reflect.Type) {
r.types[makeTypeString(typ, r.includePackagePath)] = typ
}

func (r *TypeNameResolver) TypeToString(typ reflect.Type) (string, error) {
return makeTypeString(typ, r.includePackagePath), nil
}

func (r *TypeNameResolver) StringToType(typeStr string) (reflect.Type, error) {
typ, ok := r.types[typeStr]
if !ok {
return nil, fmt.Errorf("unknow type name %s", typeStr)
}

return typ, nil
}

func makeTypeString(typ reflect.Type, includePkgPath bool) string {
if includePkgPath {
return fmt.Sprintf("%s.%s", typ.PkgPath(), typ.Name())
}

return typ.Name()
}

+ 62
- 0
utils/sort/sort.go View File

@@ -0,0 +1,62 @@
package sort

import (
"sort"

"golang.org/x/exp/constraints"
)

type Comparer[T any] func(left T, right T) int

type sorter[T any] struct {
arr []T
cmp Comparer[T]
}

func (s sorter[T]) Len() int {
return len(s.arr)
}

func (s sorter[T]) Less(i int, j int) bool {
ret := s.cmp(s.arr[i], s.arr[j])
return ret < 0
}

func (s sorter[T]) Swap(i int, j int) {
s.arr[i], s.arr[j] = s.arr[j], s.arr[i]
}

func Sort[T any](arr []T, cmp Comparer[T]) {
st := sorter[T]{
arr: arr,
cmp: cmp,
}

sort.Sort(st)
}

func CmpBool(left, right bool) int {
leftVal := 0
if left {
leftVal = 1
}

rightVal := 0
if right {
rightVal = 1
}

return leftVal - rightVal
}

func Cmp[T constraints.Ordered](left, right T) int {
if left == right {
return 0
}

if left < right {
return -1
}

return 1
}

+ 8
- 0
utils/utils.go View File

@@ -0,0 +1,8 @@
package utils

import "fmt"

// MakeMoveOperationFileName Move操作时,写入的文件的名称
func MakeMoveOperationFileName(objectID int, userID int) string {
return fmt.Sprintf("%d-%d", objectID, userID)
}

Loading…
Cancel
Save