refactor(cli): add struct of global options

This commit is contained in:
Denis Gukov 2024-10-13 13:01:38 +00:00
parent 2bd3d864b4
commit 27c7c5565a
8 changed files with 34 additions and 19 deletions

6
.vscode/launch.json vendored
View File

@ -7,7 +7,7 @@
"request": "launch",
"mode": "auto",
"program": "./cli/main.go",
"args": ["server", "--config", ".devcontainer/config.json"],
"args": ["server", "--config", "${workspaceFolder}/.devcontainer/config.json"],
"cwd": "${workspaceFolder}",
"env": {
"PATH": "${workspaceFolder}/.venv/bin:${env:PATH}"
@ -19,7 +19,7 @@
"request": "launch",
"mode": "auto",
"program": "./cli/main.go",
"args": ["server", "--config", ".devcontainer/config.json"],
"args": ["server", "--config", "${workspaceFolder}/.devcontainer/config.json"],
"cwd": "${workspaceFolder}",
"env": {
"PATH": "${workspaceFolder}/.venv/bin:${env:PATH}",
@ -32,7 +32,7 @@
"request": "launch",
"mode": "auto",
"program": "./cli/main.go",
"args": ["runner", "--config", ".devcontainer/config-runner.json"],
"args": ["runner", "start", "--config", "${workspaceFolder}/.devcontainer/config-runner.json"],
"cwd": "${workspaceFolder}",
"env": {
"PATH": "${workspaceFolder}/.venv/bin:${env:PATH}"

View File

@ -2,6 +2,10 @@ package cmd
import (
"fmt"
"net/http"
"os"
"strings"
"github.com/ansible-semaphore/semaphore/api"
"github.com/ansible-semaphore/semaphore/api/sockets"
"github.com/ansible-semaphore/semaphore/db"
@ -13,13 +17,13 @@ import (
"github.com/gorilla/handlers"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"net/http"
"os"
"strings"
)
var configPath string
var noConfig bool
var persistentFlags struct {
configPath string
noConfig bool
logLevel string
}
var rootCmd = &cobra.Command{
Use: "semaphore",
@ -34,8 +38,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")
rootCmd.PersistentFlags().StringVar(&persistentFlags.logLevel, "log-level", "", "Log level: DEBUG, INFO, WARN, ERROR, FATAL, PANIC")
rootCmd.PersistentFlags().StringVar(&persistentFlags.configPath, "config", "", "Configuration file path")
rootCmd.PersistentFlags().BoolVar(&persistentFlags.noConfig, "no-config", false, "Don't use configuration file")
if err := rootCmd.Execute(); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
@ -98,7 +103,7 @@ func runService() {
}
func createStore(token string) db.Store {
util.ConfigInit(configPath, noConfig)
util.ConfigInit(persistentFlags.configPath, persistentFlags.noConfig)
store := factory.CreateStore()

View File

@ -20,7 +20,7 @@ func init() {
func registerRunner() {
util.ConfigInit(configPath, noConfig)
util.ConfigInit(persistentFlags.configPath, persistentFlags.noConfig)
if runnerRegisterArgs.stdinRegistrationToken {
tokenBytes, err := io.ReadAll(os.Stdin)

View File

@ -2,6 +2,7 @@ package cmd
import (
"fmt"
"github.com/ansible-semaphore/semaphore/cli/setup"
"github.com/ansible-semaphore/semaphore/util"
"github.com/spf13/cobra"
@ -26,7 +27,7 @@ func doRunnerSetup() int {
setup.InteractiveRunnerSetup(config)
resultConfigPath := setup.SaveConfig(config, "config-runner.json", configPath)
resultConfigPath := setup.SaveConfig(config, "config-runner.json", persistentFlags.configPath)
util.ConfigInit(resultConfigPath, false)

View File

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

View File

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

View File

@ -3,13 +3,14 @@ package cmd
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/ansible-semaphore/semaphore/cli/setup"
"github.com/ansible-semaphore/semaphore/db"
"github.com/ansible-semaphore/semaphore/db/factory"
"github.com/ansible-semaphore/semaphore/util"
"github.com/spf13/cobra"
"os"
"strings"
)
func init() {
@ -31,7 +32,7 @@ func doSetup() int {
config.GenerateSecrets()
setup.InteractiveSetup(config)
resultConfigPath := setup.SaveConfig(config, "config.json", configPath)
resultConfigPath := setup.SaveConfig(config, "config.json", persistentFlags.configPath)
util.ConfigInit(resultConfigPath, false)

View File

@ -44,6 +44,12 @@ func (e *ContextLogger) Panic(err error, action string, message string) {
}).Panic(message)
}
func (e *ContextLogger) Debug(message string) {
log.WithFields(log.Fields{
"context": e.Context,
}).Debug(message)
}
type JobPool struct {
// logger channel used to putting log records to database.
logger chan jobLogRecord
@ -147,6 +153,8 @@ func (p *JobPool) Run() {
select {
case <-queueTicker.C: // timer 5 seconds: get task from queue and run it
logger.Debug("Checking queue")
if len(p.queue) == 0 {
break
}
@ -155,7 +163,7 @@ func (p *JobPool) Run() {
if t.status == task_logger.TaskFailStatus {
//delete failed TaskRunner from queue
p.queue = p.queue[1:]
log.Info("Task " + strconv.Itoa(t.job.Task.ID) + " dequeued (failed)")
logger.Info("Task " + strconv.Itoa(t.job.Task.ID) + " dequeued (failed)")
break
}