fix(cli): respect --config option in setup command

This commit is contained in:
Denis Gukov 2024-09-30 01:18:08 +05:00
parent c7b352dba3
commit 4afe568db6
4 changed files with 30 additions and 18 deletions

View File

@ -26,12 +26,12 @@ func doRunnerSetup() int {
setup.InteractiveRunnerSetup(config)
configPath := setup.SaveConfig(config)
resultConfigPath := setup.SaveConfig(config, "config-runner.json", configPath)
util.ConfigInit(configPath, false)
util.ConfigInit(resultConfigPath, false)
fmt.Printf(" Re-launch this program pointing to the configuration file\n\n./semaphore server --config %v\n\n", configPath)
fmt.Printf(" To run as daemon:\n\nnohup ./semaphore server --config %v &\n\n", configPath)
fmt.Printf(" Re-launch this program pointing to the configuration file\n\n./semaphore runner --config %v\n\n", resultConfigPath)
fmt.Printf(" To run as daemon:\n\nnohup ./semaphore runner --config %v &\n\n", resultConfigPath)
return 0
}

View File

@ -31,9 +31,9 @@ func doSetup() int {
config.GenerateSecrets()
setup.InteractiveSetup(config)
configPath := setup.SaveConfig(config)
resultConfigPath := setup.SaveConfig(config, "config.json", configPath)
util.ConfigInit(configPath, false)
util.ConfigInit(resultConfigPath, false)
fmt.Println(" Pinging db..")
@ -74,8 +74,8 @@ func doSetup() int {
fmt.Printf("\n You are all setup %v!\n", user.Name)
}
fmt.Printf(" Re-launch this program pointing to the configuration file\n\n./semaphore server --config %v\n\n", configPath)
fmt.Printf(" To run as daemon:\n\nnohup ./semaphore server --config %v &\n\n", configPath)
fmt.Printf(" Re-launch this program pointing to the configuration file\n\n./semaphore server --config %v\n\n", resultConfigPath)
fmt.Printf(" To run as daemon:\n\nnohup ./semaphore server --config %v &\n\n", resultConfigPath)
fmt.Printf(" You can login with %v or %v.\n", user.Email, user.Username)
return 0

View File

@ -25,6 +25,8 @@ func InteractiveRunnerSetup(conf *util.ConfigType) {
askValue("Semaphore server URL", "", &conf.WebHost)
conf.Runner = &util.RunnerConfig{}
askValue("Path to the file where runner token will be stored", "", &conf.Runner.TokenFile)
haveToken := false
@ -153,20 +155,31 @@ type IConfig interface {
ToJSON() ([]byte, error)
}
func SaveConfig(config IConfig) (configPath string) {
configDirectory, err := os.Getwd()
if err != nil {
configDirectory, err = os.UserConfigDir()
func SaveConfig(config IConfig, defaultFilename string, requiredConfigPath string) (configPath string) {
if requiredConfigPath == "" {
configDirectory, err := os.Getwd()
if err != nil {
// Final fallback
configDirectory = "/etc/semaphore"
configDirectory, err = os.UserConfigDir()
if err != nil {
// Final fallback
configDirectory = "/etc/semaphore"
}
configDirectory = filepath.Join(configDirectory, "semaphore")
}
configDirectory = filepath.Join(configDirectory, "semaphore")
configPath = filepath.Join(configDirectory, defaultFilename)
askValue("Config output file", configPath, &configPath)
} else {
configPath = requiredConfigPath
}
askValue("Config output directory", configDirectory, &configDirectory)
configDirectory := filepath.Dir(configPath)
fmt.Printf("Running: mkdir -p %v..\n", configDirectory)
var err error
if _, err = os.Stat(configDirectory); err != nil {
if os.IsNotExist(err) {
err = os.MkdirAll(configDirectory, 0755)
@ -183,7 +196,6 @@ func SaveConfig(config IConfig) (configPath string) {
panic(err)
}
configPath = filepath.Join(configDirectory, "config.json")
if err = os.WriteFile(configPath, bytes, 0644); err != nil {
panic(err)
}

View File

@ -241,7 +241,7 @@ func ConfigInit(configPath string, noConfigFile bool) {
if Config.Runner != nil && Config.Runner.TokenFile != "" {
runnerTokenBytes, err := os.ReadFile(Config.Runner.TokenFile)
if err == nil {
Config.Runner.Token = string(runnerTokenBytes)
Config.Runner.Token = strings.TrimSpace(string(runnerTokenBytes))
}
}
}