From 095a195130f46a966e9c7d50de79b4d447ad3add Mon Sep 17 00:00:00 2001 From: Sydonian <794346190@qq.com> Date: Thu, 10 Aug 2023 14:36:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E5=85=AC=E5=85=B1=E7=9A=84magefile?= =?UTF-8?q?=E8=84=9A=E6=9C=AC=E7=A7=BB=E5=8A=A8=E5=88=B0=E8=BF=99=E4=B8=AA?= =?UTF-8?q?=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 5 +- go.sum | 5 ++ magefiles/common.go | 101 +++++++++++++++++++++++++++++++++++ magefiles/targets/targets.go | 20 +++++++ utils/grpc/file_transport.go | 3 ++ 5 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 magefiles/common.go create mode 100644 magefiles/targets/targets.go diff --git a/go.mod b/go.mod index 883d95b..555dabc 100644 --- a/go.mod +++ b/go.mod @@ -8,12 +8,13 @@ require ( 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/magefile/mage v1.15.0 github.com/mitchellh/mapstructure v1.5.0 + github.com/otiai10/copy v1.12.0 github.com/samber/lo v1.36.0 github.com/sirupsen/logrus v1.9.2 github.com/smartystreets/goconvey v1.8.0 github.com/zyedidia/generic v1.2.1 - gitlink.org.cn/cloudream/proto v0.0.0 go.etcd.io/etcd/client/v3 v3.5.9 golang.org/x/exp v0.0.0-20230519143937-03e91628a987 ) @@ -66,4 +67,4 @@ require ( ) // 运行go mod tidy时需要将下面几行取消注释 -replace gitlink.org.cn/cloudream/proto => ../proto +//replace gitlink.org.cn/cloudream/proto => ../proto diff --git a/go.sum b/go.sum index a4b8906..4271f77 100644 --- a/go.sum +++ b/go.sum @@ -54,6 +54,8 @@ github.com/libp2p/go-flow-metrics v0.1.0 h1:0iPhMI8PskQwzh57jB9WxIuIOQ0r+15PChFG 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/magefile/mage v1.15.0 h1:BvGheCMAsG3bWUDbZ8AyXXpCNwU9u5CB6sM+HNb9HYg= +github.com/magefile/mage v1.15.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= 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= @@ -78,6 +80,9 @@ github.com/multiformats/go-multistream v0.4.1 h1:rFy0Iiyn3YT0asivDUIR05leAdwZq3d 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/otiai10/copy v1.12.0 h1:cLMgSQnXBs1eehF0Wy/FAGsgDTDmAqFR7rQylBb1nDY= +github.com/otiai10/copy v1.12.0/go.mod h1:rSaLseMUsZFFbsFGc7wCJnnkTAvdc5L6VWxPE4308Ww= +github.com/otiai10/mint v1.5.1 h1:XaPLeE+9vGbuyEHem1JNk3bYc7KKqyI/na0/mLd/Kks= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= diff --git a/magefiles/common.go b/magefiles/common.go new file mode 100644 index 0000000..03d76aa --- /dev/null +++ b/magefiles/common.go @@ -0,0 +1,101 @@ +package magefiles + +import ( + "errors" + "fmt" + "os" + "path/filepath" + "strings" + + "github.com/magefile/mage/sh" + cp "github.com/otiai10/copy" +) + +var Global = struct { + OS string + Arch string +}{ + Arch: "amd64", +} + +type BuildArgs struct { + OutputName string + OutputDir string + AssetsDir string +} + +type goBuildArgs struct { + Env map[string]string + OutputExt string +} + +func Build(args BuildArgs) error { + fullOutputDir, err := filepath.Abs(args.OutputDir) + if err != nil { + return err + } + + goBuildArgs, err := makeGoBuildArgs() + if err != nil { + return err + } + + binPath := filepath.Join(fullOutputDir, args.OutputName+goBuildArgs.OutputExt) + fmt.Printf("building to %s\n", binPath) + err = sh.RunWith(goBuildArgs.Env, "go", "build", "-o", binPath) + if err != nil { + return err + } + + if args.AssetsDir != "" { + outputAssetsPath := fullOutputDir + fmt.Printf("copying asset to %s\n", outputAssetsPath) + + return CopyAssets(args.AssetsDir, outputAssetsPath) + } + + return nil +} + +func makeGoBuildArgs() (goBuildArgs, error) { + args := goBuildArgs{ + Env: make(map[string]string), + } + + if Global.OS == "win" { + args.OutputExt = ".exe" + args.Env["CGO_ENABLE"] = "0" + args.Env["GOOS"] = "windows" + } else if Global.OS == "linux" { + args.OutputExt = "" + args.Env["CGO_ENABLE"] = "0" + args.Env["GOOS"] = "linux" + } else if Global.OS != "" { + return goBuildArgs{}, fmt.Errorf("unknow os type: %s", Global.OS) + } + + var pltParts []string + if Global.OS != "" { + pltParts = append(pltParts, Global.OS) + } + if Global.Arch != "" { + pltParts = append(pltParts, Global.Arch) + } + + if len(pltParts) == 0 { + fmt.Print("building platform is not set, will build for current machine.\n") + } else { + fmt.Printf("building for %s.\n", strings.Join(pltParts, "-")) + } + + return args, nil +} + +func CopyAssets(assrtDir string, targetDir string) error { + info, err := os.Stat(assrtDir) + if errors.Is(err, os.ErrNotExist) || !info.IsDir() { + return nil + } + + return cp.Copy(assrtDir, targetDir) +} diff --git a/magefiles/targets/targets.go b/magefiles/targets/targets.go new file mode 100644 index 0000000..1804ca1 --- /dev/null +++ b/magefiles/targets/targets.go @@ -0,0 +1,20 @@ +package targets + +import ( + "gitlink.org.cn/cloudream/common/magefiles" +) + +// [配置项]设置编译平台为windows +func Win() { + magefiles.Global.OS = "win" +} + +// [配置项]设置编译平台为linux +func Linux() { + magefiles.Global.OS = "linux" +} + +// [配置项]设置编译架构为amd64 +func AMD64() { + magefiles.Global.Arch = "amd64" +} diff --git a/utils/grpc/file_transport.go b/utils/grpc/file_transport.go index e4d876e..e43d986 100644 --- a/utils/grpc/file_transport.go +++ b/utils/grpc/file_transport.go @@ -1,5 +1,7 @@ package grpc +// TODO 拆分到存储服务的common包里去 +/* import ( "context" "fmt" @@ -118,3 +120,4 @@ func SendFileAsStream(client proto.FileTransportClient) (myio.PromiseWriteCloser stream: stream, }, nil } +*/