package configure

import (

	"github.com/spf13/cobra"
	sunhpcconf "gitea.sunhpc.com/kelvin/sunhpc/internal/pkg/config"
	"gitea.sunhpc.com/kelvin/sunhpc/internal/pkg/configure"
	"gitea.sunhpc.com/kelvin/sunhpc/internal/pkg/splog"
)

func CobraRunE(cmd *cobra.Command, args []string) error {
	var err error

	conf := sunhpcconf.Get()
	if conf.Autodetected() && conf.InitializedFromFile() {
		if err = conf.PersistToFile(conf.GetSunhpcConf()); err != nil {
			splog.Warn("error when persisting auto-detected settings: %s", err)
		}
	}

	if allFunctions {
		if _, err = configure.TLS(false); err != nil {
			return err
		}

		err = configure.SUNHPCD()
		if err != nil {
			return err
		}
	}
	return nil
}