From 4c3e2f438347dbabbd3506e825dff346ed39d6f4 Mon Sep 17 00:00:00 2001 From: Denis Gukov Date: Thu, 26 Aug 2021 01:36:04 +0500 Subject: [PATCH] feat(cli): support arg -config for back compatibility --- .circleci/config.yml | 2 +- CONTRIBUTING.md | 4 +- cli/cmd/root.go | 48 ++++++++++++++++++++++ cli/cmd/service.go | 40 +----------------- cli/cmd/setup.go | 4 +- deployment/docker/common/semaphore-wrapper | 2 +- deployment/docker/prod/goss.yaml | 2 +- deployment/systemd/semaphore.service | 2 +- util/config.go | 2 +- util/init/semaphore.service.debian | 2 +- 10 files changed, 59 insertions(+), 49 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index ec306379..c48f632c 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -184,7 +184,7 @@ jobs: - run: name: Wait for db command: dockerize -wait tcp://127.0.0.1:3306 -timeout 1m - - run: bin/semaphore --migrate -config config.json + - run: bin/semaphore migrate --config config.json test:docker: docker: diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 833c2a29..3326b537 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -50,8 +50,8 @@ echo "create database semaphore;" | mysql -uroot -p ``` task compile -go run cli/main.go -setup -go run cli/main.go -config ./config.json +go run cli/main.go setup +go run cli/main.go --config ./config.json ``` Open [localhost:3000](http://localhost:3000) diff --git a/cli/cmd/root.go b/cli/cmd/root.go index 706094b0..24f1dbe8 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -2,10 +2,17 @@ package cmd import ( "fmt" + log "github.com/Sirupsen/logrus" + "github.com/ansible-semaphore/semaphore/api" + "github.com/ansible-semaphore/semaphore/api/sockets" + "github.com/ansible-semaphore/semaphore/api/tasks" "github.com/ansible-semaphore/semaphore/db" "github.com/ansible-semaphore/semaphore/db/factory" "github.com/ansible-semaphore/semaphore/util" + "github.com/gorilla/context" + "github.com/gorilla/handlers" "github.com/spf13/cobra" + "net/http" "os" ) @@ -28,6 +35,13 @@ var rootCmd = &cobra.Command{ } func Execute() { + args := os.Args[1:] + if len(args) == 2 && args[0] == "-config" { + configPath = args[1] + runService() + return + } + rootCmd.PersistentFlags().StringVar(&configPath, "config", "", "Configuration file path") if err := rootCmd.Execute(); err != nil { fmt.Fprintln(os.Stderr, err) @@ -35,6 +49,40 @@ func Execute() { } } +func runService() { + store := createStore() + defer store.Close() + + fmt.Printf("Semaphore %v\n", util.Version) + fmt.Printf("Interface %v\n", util.Config.Interface) + fmt.Printf("Port %v\n", util.Config.Port) + + go sockets.StartWS() + go tasks.StartRunner() + + route := api.Route() + + route.Use(func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + context.Set(r, "store", store) + next.ServeHTTP(w, r) + }) + }) + + var router http.Handler = route + + router = handlers.ProxyHeaders(router) + http.Handle("/", router) + + fmt.Println("Server is running") + + err := http.ListenAndServe(util.Config.Interface+util.Config.Port, cropTrailingSlashMiddleware(router)) + + if err != nil { + log.Panic(err) + } +} + func createStore() db.Store { util.ConfigInit(configPath) diff --git a/cli/cmd/service.go b/cli/cmd/service.go index 453b9160..366c85e6 100644 --- a/cli/cmd/service.go +++ b/cli/cmd/service.go @@ -1,14 +1,6 @@ package cmd import ( - "fmt" - log "github.com/Sirupsen/logrus" - "github.com/ansible-semaphore/semaphore/api" - "github.com/ansible-semaphore/semaphore/api/sockets" - "github.com/ansible-semaphore/semaphore/api/tasks" - "github.com/ansible-semaphore/semaphore/util" - "github.com/gorilla/context" - "github.com/gorilla/handlers" "github.com/spf13/cobra" "net/http" "strings" @@ -24,37 +16,7 @@ var serviceCmd = &cobra.Command{ // Long: `All software has versions. This is Hugo's`, Run: func(cmd *cobra.Command, args []string) { - store := createStore() - defer store.Close() - - fmt.Printf("Semaphore %v\n", util.Version) - fmt.Printf("Interface %v\n", util.Config.Interface) - fmt.Printf("Port %v\n", util.Config.Port) - - go sockets.StartWS() - //go checkUpdates() - go tasks.StartRunner() - - route := api.Route() - - route.Use(func(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - context.Set(r, "store", store) - next.ServeHTTP(w, r) - }) - }) - - var router http.Handler = route - - router = handlers.ProxyHeaders(router) - http.Handle("/", router) - - fmt.Println("Server is running") - - err := http.ListenAndServe(util.Config.Interface+util.Config.Port, cropTrailingSlashMiddleware(router)) - if err != nil { - log.Panic(err) - } + runService() }, } diff --git a/cli/cmd/setup.go b/cli/cmd/setup.go index d4f81bf1..66dedc89 100644 --- a/cli/cmd/setup.go +++ b/cli/cmd/setup.go @@ -84,8 +84,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 -config %v\n\n", configPath) - fmt.Printf(" To run as daemon:\n\nnohup ./semaphore -config %v &\n\n", configPath) + fmt.Printf(" Re-launch this program pointing to the configuration file\n\n./semaphore --config %v\n\n", configPath) + fmt.Printf(" To run as daemon:\n\nnohup ./semaphore --config %v &\n\n", configPath) fmt.Printf(" You can login with %v or %v.\n", user.Email, user.Username) return 0 diff --git a/deployment/docker/common/semaphore-wrapper b/deployment/docker/common/semaphore-wrapper index 7539eac8..fbe978b2 100755 --- a/deployment/docker/common/semaphore-wrapper +++ b/deployment/docker/common/semaphore-wrapper @@ -100,7 +100,7 @@ EOF cat "${SEMAPHORE_TMP_PATH}/config.stdin" $1 -setup - < "${SEMAPHORE_TMP_PATH}/config.stdin" - echoerr "Run Semaphore with semaphore -config ${SEMAPHORE_CONFIG_PATH}/config.json" + echoerr "Run Semaphore with semaphore --config ${SEMAPHORE_CONFIG_PATH}/config.json" fi # run our command diff --git a/deployment/docker/prod/goss.yaml b/deployment/docker/prod/goss.yaml index ccb80e4b..252adbce 100644 --- a/deployment/docker/prod/goss.yaml +++ b/deployment/docker/prod/goss.yaml @@ -38,7 +38,7 @@ command: semaphore: exit-status: 1 stdout: - - Cannot Find configuration! Use -config parameter to point to a JSON file generated + - Cannot Find configuration! Use --config parameter to point to a JSON file generated by -setup. - 'Hint: have you run `-setup` ?' timeout: 10000 diff --git a/deployment/systemd/semaphore.service b/deployment/systemd/semaphore.service index 31d910a4..7cc226f5 100644 --- a/deployment/systemd/semaphore.service +++ b/deployment/systemd/semaphore.service @@ -4,7 +4,7 @@ Requires=network.target [Service] EnvironmentFile=/etc/semaphore/env -ExecStart=/usr/bin/semaphore -config ${SEMAPHORE_CONFIG} +ExecStart=/usr/bin/semaphore --config ${SEMAPHORE_CONFIG} User=semaphore Group=semaphore Restart=always diff --git a/util/config.go b/util/config.go index bf67605d..8909d058 100644 --- a/util/config.go +++ b/util/config.go @@ -178,7 +178,7 @@ func validatePort() { func exitOnConfigError(err error) { if err != nil { - fmt.Println("Cannot Find configuration! Use -config parameter to point to a JSON file generated by -setup.\n\n Hint: have you run `-setup` ?") + fmt.Println("Cannot Find configuration! Use --config parameter to point to a JSON file generated by -setup.\n\n Hint: have you run `-setup` ?") os.Exit(1) } } diff --git a/util/init/semaphore.service.debian b/util/init/semaphore.service.debian index 7e4db940..8f6b07b8 100644 --- a/util/init/semaphore.service.debian +++ b/util/init/semaphore.service.debian @@ -5,7 +5,7 @@ After=network.target [Service] User=www-data Group=www-data -ExecStart=/usr/bin/semaphore -config /etc/semaphore/config.json +ExecStart=/usr/bin/semaphore --config /etc/semaphore/config.json Type=simple [Install]