configui/cmd/configui/main.go

123 lines
2.5 KiB
Go

package main
import (
"flag"
"fmt"
"net/http"
"os"
log "kumoly.io/lib/klog"
"kumoly.io/tools/configui"
)
var (
flagPath string
flagAction string
flagName string
flagConfigPath string
flagBind string
flagAddr string
flagNoReconfig bool
flagLogFile string
flagAllow string
flagVer bool
)
var Version = "0.0.0"
var Build = "alpha"
func init() {
// log.SetFlags(0)
flag.StringVar(&flagConfigPath, "f", "", "path to config file")
flag.StringVar(&flagPath, "p", "", "path to file, precedence over config")
flag.StringVar(&flagName, "n", "", "Name of file")
flag.StringVar(&flagAction, "c", "", "cmd to apply")
flag.StringVar(&flagLogFile, "log", "", "log to file")
flag.StringVar(&flagAllow, "allow", "", "IPs to allow, blank to allow all")
flag.StringVar(&flagBind, "bind", "0.0.0.0:8000", "address to bind, (deprecated, use -addr instead)")
flag.StringVar(&flagAddr, "addr", "0.0.0.0:8000", "address to bind")
flag.BoolVar(&flagNoReconfig, "static", false, "disable config api")
flag.BoolVar(&flagVer, "v", false, "show version")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: configui [options]\n")
flag.PrintDefaults()
}
}
func main() {
flag.Parse()
if flagVer {
fmt.Printf("%v - %v\n", Version, Build)
return
}
// deprecate fix
if flagBind != "0.0.0.0:8000" && flagAddr == "0.0.0.0:8000" {
flagAddr = flagBind
}
cui := configui.New()
// setup values
if flagPath != "" {
if flagName == "" {
flagName = flagPath
}
file := &configui.File{
Path: flagPath,
Name: flagName,
Action: flagAction,
}
if err := cui.AppendFile(file); err != nil {
log.Error(err)
os.Exit(1)
}
} else if flagConfigPath == "" {
log.Error("no config specified")
} else {
conf, err := os.ReadFile(flagConfigPath)
if err != nil {
log.Error(err)
os.Exit(1)
}
cui.LoadConfig(string(conf))
if err != nil {
log.Error(err)
os.Exit(1)
}
}
cui.ConfigPath = flagConfigPath
if flagNoReconfig {
cui.NoReconfig = flagNoReconfig
}
if flagLogFile != "" {
cui.LogPath = flagLogFile
}
if flagAllow != "" {
cui.AllowIP = flagAllow
}
// setup routes
server := &http.Server{
Addr: flagAddr,
// disable timeout due to cui controlling cmd timeouts
// WriteTimeout: time.Second * 30,
// ReadTimeout: time.Second * 30,
Handler: cui,
}
// start server
log.Info("Listening on ", flagAddr)
err := server.ListenAndServe()
if err != nil {
log.Error(err)
os.Exit(1)
}
}