diff options
author | 2022-05-30 13:41:24 +0100 | |
---|---|---|
committer | 2022-05-30 14:41:24 +0200 | |
commit | 43ac0cdb9c4eea9d3c5eceb2c11b9e5b98b87b00 (patch) | |
tree | f0d5967d0ce639b6bc82aaf607f62e228fdf4559 /cmd/gotosocial/main.go | |
parent | [chore] Mastodon api fixups (#617) (diff) | |
download | gotosocial-43ac0cdb9c4eea9d3c5eceb2c11b9e5b98b87b00.tar.xz |
[chore] Global server configuration overhaul (#575)
* move config flag names and usage to config package, rewrite config package to use global Configuration{} struct
Signed-off-by: kim <grufwub@gmail.com>
* improved code comment
Signed-off-by: kim <grufwub@gmail.com>
* linter
Signed-off-by: kim <grufwub@gmail.com>
* fix unmarshaling
Signed-off-by: kim <grufwub@gmail.com>
* remove kim's custom go compiler changes
Signed-off-by: kim <grufwub@gmail.com>
* generate setter and flag-name functions, implement these in codebase
Signed-off-by: kim <grufwub@gmail.com>
* update deps
Signed-off-by: kim <grufwub@gmail.com>
* small change
Signed-off-by: kim <grufwub@gmail.com>
* appease the linter...
Signed-off-by: kim <grufwub@gmail.com>
* move configuration into ConfigState structure, ensure reloading to/from viper settings to keep in sync
Signed-off-by: kim <grufwub@gmail.com>
* lint
Signed-off-by: kim <grufwub@gmail.com>
* update code comments
Signed-off-by: kim <grufwub@gmail.com>
* fix merge issue
Signed-off-by: kim <grufwub@gmail.com>
* fix merge issue
Signed-off-by: kim <grufwub@gmail.com>
* improved version string (removes time + go version)
Signed-off-by: kim <grufwub@gmail.com>
* fix version string build to pass test script + consolidate logic in func
Signed-off-by: kim <grufwub@gmail.com>
* add license text, update config.Defaults comment
Signed-off-by: kim <grufwub@gmail.com>
* add license text to generated config helpers file
Signed-off-by: kim <grufwub@gmail.com>
* defer unlock on config.Set___(), to ensure unlocked on panic
Signed-off-by: kim <grufwub@gmail.com>
* make it more obvious which cmd flags are being attached
Signed-off-by: kim <grufwub@gmail.com>
Diffstat (limited to 'cmd/gotosocial/main.go')
-rw-r--r-- | cmd/gotosocial/main.go | 92 |
1 files changed, 56 insertions, 36 deletions
diff --git a/cmd/gotosocial/main.go b/cmd/gotosocial/main.go index 49560503f..1b815f6b7 100644 --- a/cmd/gotosocial/main.go +++ b/cmd/gotosocial/main.go @@ -19,14 +19,12 @@ package main import ( - "fmt" "runtime/debug" + "strings" "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "github.com/spf13/viper" - "github.com/superseriousbusiness/gotosocial/cmd/gotosocial/flag" _ "github.com/superseriousbusiness/gotosocial/docs" "github.com/superseriousbusiness/gotosocial/internal/config" ) @@ -37,48 +35,28 @@ var Version string //go:generate swagger generate spec func main() { - buildInfo, ok := debug.ReadBuildInfo() - if !ok { - panic("could not read buildinfo") - } - - goVersion := buildInfo.GoVersion - var commit string - var time string - for _, s := range buildInfo.Settings { - if s.Key == "vcs.revision" { - commit = s.Value[:7] - } - if s.Key == "vcs.time" { - time = s.Value - } - } - - var versionString string - if Version != "" { - versionString = fmt.Sprintf("%s %s %s [%s]", Version, commit, time, goVersion) - } + // Load version string + version := version() - // override software version in viper store - viper.Set(config.Keys.SoftwareVersion, versionString) + // override version in config store + config.SetSoftwareVersion(version) // instantiate the root command rootCmd := &cobra.Command{ - Use: "gotosocial", - Short: "GoToSocial - a fediverse social media server", - Long: "GoToSocial - a fediverse social media server\n\nFor help, see: https://docs.gotosocial.org.\n\nCode: https://github.com/superseriousbusiness/gotosocial", - Version: versionString, + Use: "gotosocial", + Short: "GoToSocial - a fediverse social media server", + Long: "GoToSocial - a fediverse social media server\n\nFor help, see: https://docs.gotosocial.org.\n\nCode: https://github.com/superseriousbusiness/gotosocial", + Version: version, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + // before running any other cmd funcs, we must load config-path + return config.LoadEarlyFlags(cmd) + }, SilenceErrors: true, SilenceUsage: true, } // attach global flags to the root command so that they can be accessed from any subcommand - flag.Global(rootCmd, config.Defaults) - - // bind the config-path flag to viper early so that we can call it in the pre-run of following commands - if err := viper.BindPFlag(config.Keys.ConfigPath, rootCmd.PersistentFlags().Lookup(config.Keys.ConfigPath)); err != nil { - logrus.Fatalf("error attaching config flag: %s", err) - } + config.AddGlobalFlags(rootCmd) // add subcommands rootCmd.AddCommand(serverCommands()) @@ -91,3 +69,45 @@ func main() { logrus.Fatalf("error executing command: %s", err) } } + +// version will build a version string from binary's stored build information. +func version() string { + // Read build information from binary + build, ok := debug.ReadBuildInfo() + if !ok { + return "" + } + + // Define easy getter to fetch build settings + getSetting := func(key string) string { + for i := 0; i < len(build.Settings); i++ { + if build.Settings[i].Key == key { + return build.Settings[i].Value + } + } + return "" + } + + var info []string + + if Version != "" { + // Append version if set + info = append(info, Version) + } + + if vcs := getSetting("vcs"); vcs != "" { + // A VCS type was set (99.9% probably git) + + if commit := getSetting("vcs.revision"); commit != "" { + if len(commit) > 7 { + // Truncate commit + commit = commit[:7] + } + + // Append VCS + commit if set + info = append(info, vcs+"-"+commit) + } + } + + return strings.Join(info, " ") +} |