feat(cli): support arg -config for back compatibility

This commit is contained in:
Denis Gukov 2021-08-26 01:36:04 +05:00
parent f7f23f999f
commit 4c3e2f4383
10 changed files with 59 additions and 49 deletions

View File

@ -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:

View File

@ -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)

View File

@ -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)

View File

@ -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()
},
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)
}
}

View File

@ -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]