package command

import (
	"path/filepath"
	"strings"
)

type DmCmd struct{ ExecCmd }

const Dmctl = "/dmctl --master-addr=127.0.0.1:8261"

func (d *DmCmd) Dmctl() *DmCmd {
	return &DmCmd{ExecCmd{Command: Dmctl, Options: nil, ParentCmd: d}}
}

func (d *DmCmd) Operate(operation string) *DmCmd {
	return &DmCmd{ExecCmd{Command: operation, Options: nil, ParentCmd: d}}
}

func (d *DmCmd) WriteCfgToFile(content, filePath string) *DmCmd {
	cmd := "mkdir -p " + filepath.Dir(filePath)
	cmd += " && touch " + filePath
	cmd += " && echo \"" + strings.ReplaceAll(content, "\"", "\\\"") + "\" >" + filePath
	return &DmCmd{ExecCmd{Command: cmd, Options: nil, ParentCmd: d}}
}

type TiupDmCmd struct{ ExecCmd }

const TiupDmctl = "/dmctl --master-addr=tiup-peer:8261"

func (d *TiupDmCmd) TiupDmctl() *TiupDmCmd {
	return &TiupDmCmd{ExecCmd{Command: TiupDmctl, Options: nil, ParentCmd: d}}
}

func (d *TiupDmCmd) WriteCfgToFile(content, filePath string) *TiupDmCmd {
	cmd := "mkdir -p " + filepath.Dir(filePath)
	cmd += " && touch " + filePath
	cmd += " && echo \"" + strings.ReplaceAll(content, "\"", "\\\"") + "\" >" + filePath
	return &TiupDmCmd{ExecCmd{Command: cmd, Options: nil, ParentCmd: d}}
}
