package main

import (
	"context"
	"flag"
	"time"

	"github.com/tidbcloud/account/test/providercli/cli"
)

func usage() {
	println("Usage: providercli [options] COMMAND [args]")
	println("  COMMANDs can be auto-completed.")
	flag.PrintDefaults()
	println()
	println("supported COMMANDs:")
	for _, command := range cli.SuppportedCommandList() {
		println("  - ", command)
	}
}

func main() {
	conf := &cli.Config{}

	flag.StringVar(&conf.Address, "address",
		"127.0.0.1:10000", "provider server address")
	flag.BoolVar(&conf.Benchmark, "benchmark",
		false, "benchmark")
	flag.StringVar(&conf.OutputFormat, "format",
		"text", "output format")
	flag.BoolVar(&conf.NoConflict, "no-conflict",
		false, "command is ensured no conflict, no need to check")
	timeout := flag.Duration("timeout", 1*time.Minute, "timeout")
	json := flag.Bool("json", false, "output in json format, equivalent to -format=json")
	flag.Usage = usage
	flag.Parse()

	rawArgs := flag.Args()
	if len(rawArgs) <= 0 {
		flag.Usage()
		return
	}

	ctx, cancel := context.WithTimeout(context.Background(), *timeout)
	defer cancel()
	conf.Context = ctx
	if *json {
		conf.OutputFormat = "json"
	}

	command, args := rawArgs[0], rawArgs[1:]
	cli.Main(conf, command, args)
}
