feat(cli): add flag --no-config to do not read config file. All options can be read from env vars

This commit is contained in:
Denis Gukov 2024-09-26 00:28:22 +05:00
parent 43c2187433
commit 02631b2643
5 changed files with 12 additions and 8 deletions

View File

@ -19,6 +19,7 @@ import (
)
var configPath string
var noConfig bool
var rootCmd = &cobra.Command{
Use: "semaphore",
@ -34,8 +35,9 @@ Complete documentation is available at https://ansible-semaphore.com.`,
func Execute() {
rootCmd.PersistentFlags().StringVar(&configPath, "config", "", "Configuration file path")
rootCmd.PersistentFlags().BoolVar(&noConfig, "no-config", false, "Don't use configuration file")
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
@ -96,7 +98,7 @@ func runService() {
}
func createStore(token string) db.Store {
util.ConfigInit(configPath)
util.ConfigInit(configPath, noConfig)
store := factory.CreateStore()

View File

@ -11,7 +11,7 @@ func init() {
}
func runRunner() {
util.ConfigInit(configPath)
util.ConfigInit(configPath, noConfig)
taskPool := runners.JobPool{}

View File

@ -11,7 +11,7 @@ func init() {
}
func unregisterRunner() {
util.ConfigInit(configPath)
util.ConfigInit(configPath, noConfig)
taskPool := runners.JobPool{}
err := taskPool.Unregister()

View File

@ -33,7 +33,7 @@ func doSetup() int {
configPath := setup.SaveConfig(config)
util.ConfigInit(configPath)
util.ConfigInit(configPath, false)
fmt.Println(" Pinging db..")

View File

@ -107,7 +107,7 @@ type RunnerSettings struct {
// 1) User starts the task.
// 2) Semaphore found runner for task and calls runner's webhook if it provided.
// 3) Your server or lambda handling the call and starts the one-off runner.
// 4) The runner connects to the Semaphore server and handles tasks.
// 4) The runner connects to the Semaphore server and handles the enqueued task(s).
OneOff bool `json:"one_off" env:"SEMAPHORE_RUNNER_ONE_OFF"`
Webhook string `json:"webhook" env:"SEMAPHORE_RUNNER_WEBHOOK"`
@ -255,13 +255,15 @@ func LoadRunnerSettings(path string) (config RunnerConfig, err error) {
}
// ConfigInit reads in cli flags, and switches actions appropriately on them
func ConfigInit(configPath string) {
func ConfigInit(configPath string, noConfigFile bool) {
fmt.Println("Loading config")
Config = &ConfigType{}
Config.Apps = map[string]App{}
loadConfigFile(configPath)
if !noConfigFile {
loadConfigFile(configPath)
}
loadConfigEnvironment()
loadConfigDefaults()