package cmii_operator

import (
	"bytes"
	v1 "k8s.io/api/apps/v1"
	appsv1 "k8s.io/client-go/applyconfigurations/apps/v1"
	"sigs.k8s.io/yaml"
	"text/template"
	"wdd.io/agent-go/utils"
)

type CmiiBackendDeploymentConfig struct {
	Namespace  string
	AppName    string
	ImageTag   string
	TagVersion string
	Replicas   string
}

func (backend CmiiBackendDeploymentConfig) ParseToApplyConf() *appsv1.DeploymentApplyConfiguration {

	// 解析模板
	tmpl, err := template.New("cmiiBackendDeploymentTemplate").Parse(cmiiBackendDeploymentTemplate)
	if err != nil {
		panic(err)
	}

	// 应用数据并打印结果
	var result bytes.Buffer
	err = tmpl.Execute(&result, backend)
	if err != nil {
		panic(err)
	}

	// 创建Deployment对象
	deployment := v1.Deployment{}
	err = yaml.Unmarshal(result.Bytes(), &deployment)
	if err != nil {
		panic(err)
	}

	utils.BeautifulPrint(&deployment)

	return nil
}
