mirror of
https://github.com/komodorio/helm-dashboard.git
synced 2026-03-24 11:48:04 +00:00
Force namespace via cmdline parameter (#53)
* Force name via cmdline parameter * Use a library to parse CLI flags * Use less env vars * Document it
This commit is contained in:
63
main.go
63
main.go
@@ -1,7 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jessevdk/go-flags"
|
||||
"github.com/komodorio/helm-dashboard/pkg/dashboard"
|
||||
"github.com/pkg/browser"
|
||||
log "github.com/sirupsen/logrus"
|
||||
@@ -14,37 +16,70 @@ var (
|
||||
date = "unknown"
|
||||
)
|
||||
|
||||
type options struct {
|
||||
Verbose bool `short:"v" long:"verbose" description:"Show verbose debug information"`
|
||||
NoBrowser bool `short:"b" long:"no-browser" description:"Do not attempt to open Web browser upon start"`
|
||||
Version bool `long:"version" description:"Show tool version"`
|
||||
|
||||
Port uint `short:"p" long:"port" description:"Port to start server on" default:"8080"` // TODO: better default port to clash less?
|
||||
|
||||
Namespace string `short:"n" long:"namespace" description:"Limit operations to a specific namespace"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
setupLogging()
|
||||
opts := parseFlags()
|
||||
|
||||
// TODO: proper command-line parsing
|
||||
if len(os.Args) > 1 { // dirty thing to allow --help to work
|
||||
os.Exit(0)
|
||||
}
|
||||
setupLogging(opts.Verbose)
|
||||
|
||||
address, webServerDone := dashboard.StartServer(version)
|
||||
address, webServerDone := dashboard.StartServer(version, int(opts.Port), opts.Namespace, opts.Verbose)
|
||||
|
||||
if os.Getenv("HD_NOBROWSER") == "" {
|
||||
if opts.NoBrowser {
|
||||
log.Infof("Access web UI at: %s", address)
|
||||
} else {
|
||||
log.Infof("Opening web UI: %s", address)
|
||||
err := browser.OpenURL(address)
|
||||
if err != nil {
|
||||
log.Warnf("Failed to open Web browser for URL: %s", err)
|
||||
}
|
||||
} else {
|
||||
log.Infof("Access web UI at: %s", address)
|
||||
}
|
||||
|
||||
<-webServerDone
|
||||
log.Infof("Done.")
|
||||
}
|
||||
|
||||
func setupLogging() {
|
||||
if os.Getenv("DEBUG") == "" {
|
||||
log.SetLevel(log.InfoLevel)
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
} else {
|
||||
func parseFlags() options {
|
||||
opts := options{}
|
||||
args, err := flags.Parse(&opts)
|
||||
if err != nil {
|
||||
if e, ok := err.(*flags.Error); ok {
|
||||
if e.Type == flags.ErrHelp {
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
|
||||
// we rely on default behavior to print the problem inside `flags` library
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if opts.Version {
|
||||
fmt.Print(version)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if len(args) > 0 {
|
||||
panic("The program does not take argumants, see --help for usage")
|
||||
}
|
||||
return opts
|
||||
}
|
||||
|
||||
func setupLogging(verbose bool) {
|
||||
if verbose {
|
||||
log.SetLevel(log.DebugLevel)
|
||||
gin.SetMode(gin.DebugMode)
|
||||
log.Debugf("Debug logging is enabled")
|
||||
} else {
|
||||
log.SetLevel(log.InfoLevel)
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
log.Infof("Helm Dashboard by Komodor, version %s (%s @ %s)", version, commit, date)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user