go-import-redirector/cmd/root.go

74 lines
1.9 KiB
Go
Raw Normal View History

2023-05-02 23:18:09 +00:00
// Copyright 2023 Michael Amann and contributors
// SPDX-License-Identifier: AGPL-3.0-only
package cmd
import (
"os"
"os/signal"
"github.com/spf13/cobra"
"git.kle.li/tools/go-import-redirector/pkg/configuration"
2023-05-06 21:34:13 +00:00
"git.kle.li/tools/go-import-redirector/pkg/meta"
2023-05-02 23:18:09 +00:00
"git.kle.li/tools/go-import-redirector/pkg/server"
)
2023-05-06 21:34:13 +00:00
var (
cfgFile string
bind string
hotReload bool
cfg *configuration.Configuration
srv *server.Server
startupDone bool
)
2023-05-02 23:18:09 +00:00
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
2023-05-06 21:34:13 +00:00
Use: "go-import-redirector",
Version: meta.GetVersion() + " (Commit: " + meta.GetShortCommit() + ")",
Short: "run a webserver serving html documents with the meta annotation for golang imports",
Long: ``,
Run: rootRun,
2023-05-02 23:18:09 +00:00
}
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
2023-12-26 21:21:27 +00:00
rootCmd.Flags().StringVarP(&cfgFile, "config", "c", "", "config file (default order is $HOME/go-import-redirector.yaml, /etc/go-import-redirector/go-import-redirector.yaml, ./go-import-redirector.yaml)")
2023-05-02 23:18:09 +00:00
rootCmd.Flags().StringVarP(&bind, "bind", "b", ":3000", "the address and port on which to serve the webserver (default :3000)")
rootCmd.Flags().BoolVarP(&hotReload, "hotreload", "r", true, "Change hot reloading (default true)")
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
startupDone = false
srv = server.New(bind)
cfg = configuration.New(cfgFile, hotReload, reloadConfig)
}
2023-05-06 21:34:13 +00:00
func rootRun(_ *cobra.Command, _ []string) {
2023-05-02 23:18:09 +00:00
// spew.Dump(cfg.GetPackageList())
startupDone = true
srv.Start(cfg.GetPackageList())
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
// Waiting for SIGINT (kill -2)
<-stop
}
func reloadConfig() {
if startupDone {
srv.Restart(cfg.GetPackageList())
}
}