// 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" "git.kle.li/tools/go-import-redirector/pkg/meta" "git.kle.li/tools/go-import-redirector/pkg/server" ) var ( cfgFile string bind string hotReload bool cfg *configuration.Configuration srv *server.Server startupDone bool ) // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ 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, } func Execute() { err := rootCmd.Execute() if err != nil { os.Exit(1) } } func init() { cobra.OnInitialize(initConfig) 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)") 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) } func rootRun(_ *cobra.Command, _ []string) { // 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()) } }