bbgo_origin/pkg/cmd/build.go

60 lines
1.1 KiB
Go
Raw Permalink Normal View History

2020-10-26 05:27:07 +00:00
package cmd
import (
"context"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
2020-10-26 13:45:02 +00:00
"github.com/c9s/bbgo/pkg/bbgo"
2020-10-26 05:27:07 +00:00
)
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")
}
2020-12-06 03:40:22 +00:00
userConfig, err := bbgo.LoadBuildConfig(configFile)
2020-10-26 05:27:07 +00:00
if err != nil {
return err
}
2021-01-21 04:06:03 +00:00
if userConfig.Build == nil {
return errors.New("build config is not defined")
2020-10-26 05:27:07 +00:00
}
2021-01-21 04:06:03 +00:00
for _, target := range userConfig.Build.Targets {
log.Infof("building %s ...", target.Name)
2020-10-26 05:27:07 +00:00
2021-01-21 04:06:03 +00:00
binary, err := bbgo.BuildTarget(ctx, userConfig, target)
if err != nil {
return err
}
2020-10-26 05:27:07 +00:00
2021-01-21 04:06:03 +00:00
log.Infof("build succeeded: %s", binary)
2020-10-26 05:27:07 +00:00
}
return nil
},
}