From 23dcbc813dbda179f3d5d15cd2e539c8955d0326 Mon Sep 17 00:00:00 2001 From: Sydonian <794346190@qq.com> Date: Tue, 29 Aug 2023 15:37:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=88=E5=B9=B6=E4=BB=93=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- magefiles/go.mod | 10 +++ magefiles/go.sum | 7 +++ magefiles/main.go | 152 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 169 insertions(+) create mode 100644 magefiles/go.mod create mode 100644 magefiles/go.sum create mode 100644 magefiles/main.go diff --git a/magefiles/go.mod b/magefiles/go.mod new file mode 100644 index 0000000..58f574b --- /dev/null +++ b/magefiles/go.mod @@ -0,0 +1,10 @@ +module magefiles + +go 1.20 + +require ( + github.com/magefile/mage v1.15.0 + github.com/otiai10/copy v1.12.0 +) + +require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect diff --git a/magefiles/go.sum b/magefiles/go.sum new file mode 100644 index 0000000..b3d523d --- /dev/null +++ b/magefiles/go.sum @@ -0,0 +1,7 @@ +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/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= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/magefiles/main.go b/magefiles/main.go new file mode 100644 index 0000000..c81d02d --- /dev/null +++ b/magefiles/main.go @@ -0,0 +1,152 @@ +//go:build mage + +package main + +import ( + "errors" + "fmt" + "os" + "path/filepath" + + "github.com/magefile/mage/sh" + cp "github.com/otiai10/copy" +) + +const ( + BuildDir = "./build" +) + +var Global = struct { + OS string + Arch string +}{} + +// [配置项]设置编译平台为windows +func Win() { + Global.OS = "win" +} + +// [配置项]设置编译平台为linux +func Linux() { + Global.OS = "linux" +} + +// [配置项]设置编译架构为amd64 +func AMD64() { + Global.Arch = "amd64" +} + +func All() error { + if err := Bin(); err != nil { + return err + } + + if err := Scripts(); err != nil { + return err + } + + if err := Confs(); err != nil { + return err + } + + return nil +} + +func Bin() error { + if err := Agent(); err != nil { + return err + } + if err := Client(); err != nil { + return err + } + if err := Coordinator(); err != nil { + return err + } + if err := Scanner(); err != nil { + return err + } + + return nil +} + +func Scripts() error { + scriptsDir := "./storage-common/assets/scripts" + + info, err := os.Stat(scriptsDir) + if errors.Is(err, os.ErrNotExist) || !info.IsDir() { + return fmt.Errorf("script directory not exists or is not a directory") + } + + fullDirPath, err := filepath.Abs(filepath.Join(BuildDir, "scripts")) + if err != nil { + return err + } + + fmt.Printf("copying scripts to %s\n", fullDirPath) + + return cp.Copy(scriptsDir, fullDirPath) +} + +func Confs() error { + confDir := "./storage-common/assets/confs" + + info, err := os.Stat(confDir) + if errors.Is(err, os.ErrNotExist) || !info.IsDir() { + return fmt.Errorf("conf directory not exists or is not a directory") + } + + fullDirPath, err := filepath.Abs(filepath.Join(BuildDir, "confs")) + if err != nil { + return err + } + + fmt.Printf("copying confs to %s\n", fullDirPath) + + return cp.Copy(confDir, fullDirPath) +} + +func Agent() error { + os.Chdir("./storage-agent") + defer os.Chdir("..") + + return sh.RunV("mage", makeBuildMageArgeuments()...) +} + +func Client() error { + os.Chdir("./storage-client") + defer os.Chdir("..") + + return sh.RunV("mage", makeBuildMageArgeuments()...) +} + +func Coordinator() error { + os.Chdir("./storage-coordinator") + defer os.Chdir("..") + + return sh.RunV("mage", makeBuildMageArgeuments()...) +} + +func Scanner() error { + os.Chdir("./storage-scanner") + defer os.Chdir("..") + + return sh.RunV("mage", makeBuildMageArgeuments()...) +} + +func makeBuildMageArgeuments() []string { + var args []string + + if Global.OS != "" { + args = append(args, Global.OS) + } + + if Global.Arch != "" { + args = append(args, Global.Arch) + } + + args = append(args, "buildroot", "../build") + + args = append(args, "build") + + return args +}