// Package cmd defines all runners
package cmd

import (
	"os"
	"os/signal"
	"syscall"

	"quantex.com/qfixdpl/src/app"
	"quantex.com/qfixdpl/src/client/config"
	"quantex.com/qfixdpl/src/common/tracerr"
)

func NewRunner(fn func(app.Config) error) func(globalCfg, serviceCfg string) error {
	return func(globalCfg, serviceCfg string) error {
		cfg, err := config.Read([]string{globalCfg, serviceCfg})
		if err != nil {
			return tracerr.Errorf("unable to read config running asyncRun: %s", err.Error())
		}

		err = fn(cfg)
		if err != nil {
			return tracerr.Errorf("error running traderRunner: %s", err.Error())
		}

		return nil
	}
}

func WaitForInterruptSignal(stopChan chan struct{}) {
	interrupt := make(chan os.Signal, 10)
	signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
	select {
	case <-interrupt:
		return
	case <-stopChan:
		return
	}
}
