package maker

import (
	"context"
	"fmt"
	"os"
	"path/filepath"

	"gitea.loveuer.com/yizhisec/pkg3/logger"
	"yizhisec.com/hsv2/forge/internal/opt"
	"yizhisec.com/hsv2/forge/pkg/resource"
)

func (m *maker) Flannel(ctx context.Context, mode string) error {
	var (
		err      error
		location = filepath.Join(opt.Cfg.Make.Dir, "dependency", "flannel")
	)

	logger.Info("☑️ 开始构建 flannel 资源...")

	logger.Debug("MakeFlannel location: %s", location)
	if err = os.Mkdir(location, 0755); err != nil {
		logger.Error("MakeFlannel: 创建目录 %s 失败", location)
		logger.Debug("MakeFlannel: 创建目录 %s 失败: %v", location, err)
		return err
	}

	bs := []byte(fmt.Sprintf(resource.YAMLFlannel, mode))

	if err = os.WriteFile(filepath.Join(location, "flannel.yaml"), bs, 0644); err != nil {
		logger.Error("MakeFlannel: 创建文件 %s 失败", filepath.Join(location, "flannel.yaml"))
		logger.Debug("MakeFlannel: 创建文件 %s 失败: %v", filepath.Join(location, "flannel.yaml"), err)
		return err
	}

	logger.Info("✅ 构建 flannel 成功!!!")

	return nil
}
