mirror of
https://github.com/semaphoreui/semaphore.git
synced 2025-01-20 07:19:20 +01:00
refactor(cli): add struct of global options
This commit is contained in:
parent
2bd3d864b4
commit
27c7c5565a
6
.vscode/launch.json
vendored
6
.vscode/launch.json
vendored
@ -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}"
|
||||
|
@ -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()
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
||||
|
@ -11,7 +11,7 @@ func init() {
|
||||
}
|
||||
|
||||
func runRunner() {
|
||||
util.ConfigInit(configPath, noConfig)
|
||||
util.ConfigInit(persistentFlags.configPath, persistentFlags.noConfig)
|
||||
|
||||
taskPool := runners.JobPool{}
|
||||
|
||||
|
@ -11,7 +11,7 @@ func init() {
|
||||
}
|
||||
|
||||
func unregisterRunner() {
|
||||
util.ConfigInit(configPath, noConfig)
|
||||
util.ConfigInit(persistentFlags.configPath, persistentFlags.noConfig)
|
||||
|
||||
taskPool := runners.JobPool{}
|
||||
err := taskPool.Unregister()
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user