qbtrade/pkg/cmd/build.go
2024-06-27 22:42:38 +08:00

60 lines
1.1 KiB
Go

package cmd
import (
"context"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"git.qtrade.icu/lychiyu/qbtrade/pkg/qbtrade"
)
func init() {
RootCmd.AddCommand(BuildCmd)
}
var BuildCmd = &cobra.Command{
Use: "build",
Short: "build cross-platform binary",
// SilenceUsage is an option to silence usage when an error occurs.
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
configFile, err := cmd.Flags().GetString("config")
if err != nil {
return err
}
if len(configFile) == 0 {
return errors.New("--config option is required")
}
userConfig, err := qbtrade.LoadBuildConfig(configFile)
if err != nil {
return err
}
if userConfig.Build == nil {
return errors.New("build config is not defined")
}
for _, target := range userConfig.Build.Targets {
log.Infof("building %s ...", target.Name)
binary, err := qbtrade.BuildTarget(ctx, userConfig, target)
if err != nil {
return err
}
log.Infof("build succeeded: %s", binary)
}
return nil
},
}