package cmd

/*
Copyright 2022 Absa Group Limited

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic
*/

import (
	"github.com/AbsaOSS/golic/utils/guard"
	"github.com/enescakir/emoji"
)

type Service interface {
	Run() error
	String() string
	ExitCode() int
}

type ServiceRunner struct {
	service Service
}

func Command(service Service) *ServiceRunner {
	return &ServiceRunner{
		service,
	}
}

//Run service once and panics if service is broken
func (r *ServiceRunner) MustRun() int {
	logger.Info().Msgf("%s command %s started", emoji.Tractor, r.service)
	err := r.service.Run()
	guard.FailOnError(err, "command %s failed", r.service)
	return r.service.ExitCode()
}
