don/cmd/cmds/root.go

66 lines
1.3 KiB
Go
Raw Normal View History

2021-06-14 14:48:39 +02:00
// nolint:gochecknoglobals,gochecknoinits
package cmds
import (
"fmt"
"time"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"go.xsfx.dev/don"
)
const (
defaultTimeout = 10 * time.Second
defaultRetry = time.Second
)
var (
2021-06-14 20:18:56 +02:00
command string
2021-06-14 14:48:39 +02:00
timeout time.Duration
retry time.Duration
)
var (
version = "dev"
commit = "none"
date = "unknown"
)
var rootCmd = &cobra.Command{
2021-06-14 20:18:56 +02:00
Use: "don [command]",
2021-06-14 14:48:39 +02:00
Run: func(cmd *cobra.Command, args []string) {
2021-06-15 10:48:12 +02:00
if err := don.Ready(don.Cmd(command), timeout, retry); err != nil {
2021-06-14 14:48:39 +02:00
log.Fatal().Err(err).Msg("received error")
}
log.Info().Msg("ready")
},
}
var versionCmd = &cobra.Command{
Use: "version",
Short: "Print version informations",
Run: func(cmd *cobra.Command, args []string) {
// nolint: forbidigo
fmt.Printf("don %s, commit %s, build on %s\n", version, commit, date)
},
}
func init() {
rootCmd.AddCommand(versionCmd)
2021-06-14 20:18:56 +02:00
rootCmd.Flags().StringVarP(&command, "command", "c", "", "command to run (required)")
if err := rootCmd.MarkFlagRequired("command"); err != nil {
log.Fatal().Err(err).Msg("needs command flag")
}
2021-06-14 14:48:39 +02:00
rootCmd.Flags().DurationVarP(&timeout, "timeout", "t", defaultTimeout, "timeout")
rootCmd.Flags().DurationVarP(&retry, "retry", "r", defaultRetry, "retry")
}
func Execute() error {
2021-06-14 15:11:14 +02:00
// nolint:wrapcheck
2021-06-14 14:48:39 +02:00
return rootCmd.Execute()
}