package main

import (
	"context"
	"flag"

	"github.com/pingcap/log"
	"go.uber.org/zap"

	"github.com/pingcap/test-infra/caselib/dm/testcases"
	"github.com/pingcap/test-infra/caselib/pkg/model/dm"
	"github.com/pingcap/test-infra/caselib/pkg/spec"
	"github.com/pingcap/test-infra/caselib/pkg/steps"
	_ "github.com/pingcap/test-infra/sdk/resource/impl/k8s" // register k8s impl
)

var (
	workloadFilePath string
	pathPath         string
)

// spec will be used to initialize concrete testcase
// such as which one case to use, and some other parameters, eg: workload.
func init() {
	flag.StringVar(&workloadFilePath, "workload", "", "the workload of case")
	flag.StringVar(&pathPath, "path", "", "specification of the testcase")
	flag.Parse()

	log.Info("", zap.String("filePath", workloadFilePath), zap.String("pathPath", pathPath))

	if workloadFilePath == "" || pathPath == "" {
		log.Panic("filePath and path must be set")
	}

	if err := dm.ParseYaml(pathPath, testcases.Path); err != nil {
		log.Panic("parse path failed", zap.Error(err))
	}
	if err := spec.ParseYaml(workloadFilePath, testcases.Spec); err != nil {
		log.Panic("parse specification failed", zap.Error(err))
	}
}

func main() {
	ctx := steps.New(context.Background())
	err := testcases.GetTestCase().Execute(ctx)
	if err != nil {
		log.Panic("run case failed", zap.Error(err))
	}
	env := testcases.InitCommonEnv()
	s := env.Cleanup(string(testcases.Spec.CaseSpec.Name))
	if cerr := s.Execute(ctx); cerr != nil {
		log.Error("cleanup env failed", zap.Error(cerr))
	}
	log.Info("run case success")
}
