add version flag

This commit is contained in:
Jakob Schrettenbrunner 2020-07-05 00:18:29 +02:00
parent a110d5768f
commit 5ef58cadee

View File

@ -3,15 +3,16 @@ package cmd
import ( import (
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"github.com/apex/log"
"github.com/mitchellh/colorstring"
"github.com/pterodactyl/wings/loggers/cli"
"golang.org/x/crypto/acme/autocert"
"net/http" "net/http"
"os" "os"
"path" "path"
"strings" "strings"
"github.com/apex/log"
"github.com/mitchellh/colorstring"
"github.com/pterodactyl/wings/loggers/cli"
"golang.org/x/crypto/acme/autocert"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/pkg/profile" "github.com/pkg/profile"
"github.com/pterodactyl/wings/config" "github.com/pterodactyl/wings/config"
@ -30,6 +31,7 @@ var debug = false
var shouldRunProfiler = false var shouldRunProfiler = false
var useAutomaticTls = false var useAutomaticTls = false
var tlsHostname = "" var tlsHostname = ""
var showVersion = false
var root = &cobra.Command{ var root = &cobra.Command{
Use: "wings", Use: "wings",
@ -41,10 +43,11 @@ var root = &cobra.Command{
os.Exit(1) os.Exit(1)
} }
}, },
Run: rootCmdRun, Run: rootCmdRun,
} }
func init() { func init() {
root.PersistentFlags().BoolVar(&showVersion, "version", false, "show the version and exit")
root.PersistentFlags().StringVar(&configPath, "config", config.DefaultLocation, "set the location for the configuration file") root.PersistentFlags().StringVar(&configPath, "config", config.DefaultLocation, "set the location for the configuration file")
root.PersistentFlags().BoolVar(&debug, "debug", false, "pass in order to run wings in debug mode") root.PersistentFlags().BoolVar(&debug, "debug", false, "pass in order to run wings in debug mode")
root.PersistentFlags().BoolVar(&shouldRunProfiler, "profile", false, "pass in order to profile wings") root.PersistentFlags().BoolVar(&shouldRunProfiler, "profile", false, "pass in order to profile wings")
@ -76,6 +79,11 @@ func readConfiguration() (*config.Configuration, error) {
} }
func rootCmdRun(*cobra.Command, []string) { func rootCmdRun(*cobra.Command, []string) {
if showVersion {
fmt.Println(system.Version)
os.Exit(0)
}
if shouldRunProfiler { if shouldRunProfiler {
defer profile.Start().Stop() defer profile.Start().Stop()
} }
@ -229,10 +237,10 @@ func rootCmdRun(*cobra.Command, []string) {
} }
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"use_ssl": c.Api.Ssl.Enabled, "use_ssl": c.Api.Ssl.Enabled,
"use_auto_tls": useAutomaticTls && len(tlsHostname) > 0, "use_auto_tls": useAutomaticTls && len(tlsHostname) > 0,
"host_address": c.Api.Host, "host_address": c.Api.Host,
"host_port": c.Api.Port, "host_port": c.Api.Port,
}).Info("configuring internal webserver") }).Info("configuring internal webserver")
r := router.Configure() r := router.Configure()
@ -240,9 +248,9 @@ func rootCmdRun(*cobra.Command, []string) {
if useAutomaticTls && len(tlsHostname) > 0 { if useAutomaticTls && len(tlsHostname) > 0 {
m := autocert.Manager{ m := autocert.Manager{
Prompt: autocert.AcceptTOS, Prompt: autocert.AcceptTOS,
Cache: autocert.DirCache(path.Join(c.System.RootDirectory, "/.tls-cache")), Cache: autocert.DirCache(path.Join(c.System.RootDirectory, "/.tls-cache")),
HostPolicy: autocert.HostWhitelist(tlsHostname), HostPolicy: autocert.HostWhitelist(tlsHostname),
} }
log.WithField("hostname", tlsHostname). log.WithField("hostname", tlsHostname).