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) 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(" 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 server --config %v &\n\n", configPath) fmt.Printf(" To run as daemon:\n\nnohup ./semaphore runner --config %v &\n\n", resultConfigPath)
return 0 return 0
} }

View File

@ -31,9 +31,9 @@ func doSetup() int {
config.GenerateSecrets() config.GenerateSecrets()
setup.InteractiveSetup(config) 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..") fmt.Println(" Pinging db..")
@ -74,8 +74,8 @@ func doSetup() int {
fmt.Printf("\n You are all setup %v!\n", user.Name) 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(" 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", configPath) 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) fmt.Printf(" You can login with %v or %v.\n", user.Email, user.Username)
return 0 return 0

View File

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

View File

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