main.go (3408B)
1 // GoToSocial 2 // Copyright (C) GoToSocial Authors admin@gotosocial.org 3 // SPDX-License-Identifier: AGPL-3.0-or-later 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package main 19 20 import ( 21 "log" 22 "os" 23 godebug "runtime/debug" 24 "strings" 25 26 "codeberg.org/gruf/go-debug" 27 "github.com/spf13/cobra" 28 29 _ "github.com/superseriousbusiness/gotosocial/docs" 30 "github.com/superseriousbusiness/gotosocial/internal/config" 31 ) 32 33 // Version is the version of GoToSocial being used. 34 // It's injected into the binary by the build script. 35 var Version string 36 37 //go:generate swagger generate spec 38 func main() { 39 // Load version string 40 version := version() 41 42 // override version in config store 43 config.SetSoftwareVersion(version) 44 45 // instantiate the root command 46 rootCmd := &cobra.Command{ 47 Use: "gotosocial", 48 Short: "GoToSocial - a fediverse social media server", 49 Long: "GoToSocial - a fediverse social media server\n\nFor help, see: https://docs.gotosocial.org.\n\nCode: https://github.com/superseriousbusiness/gotosocial", 50 Version: version, 51 PersistentPreRunE: func(cmd *cobra.Command, args []string) error { 52 // before running any other cmd funcs, we must load config-path 53 return config.LoadEarlyFlags(cmd) 54 }, 55 SilenceErrors: true, 56 SilenceUsage: true, 57 } 58 59 // attach global flags to the root command so that they can be accessed from any subcommand 60 config.AddGlobalFlags(rootCmd) 61 62 // add subcommands 63 rootCmd.AddCommand(serverCommands()) 64 rootCmd.AddCommand(debugCommands()) 65 rootCmd.AddCommand(adminCommands()) 66 if debug.DEBUG { 67 // only add testrig if debug enabled. 68 rootCmd.AddCommand(testrigCommands()) 69 } else if len(os.Args) > 1 && os.Args[1] == "testrig" { 70 log.Fatalln("gotosocial must be built and run with the DEBUG enviroment variable set to enable and access testrig") 71 } 72 73 // run 74 if err := rootCmd.Execute(); err != nil { 75 log.Fatalf("error executing command: %s", err) 76 } 77 } 78 79 // version will build a version string from binary's stored build information. 80 func version() string { 81 // Read build information from binary 82 build, ok := godebug.ReadBuildInfo() 83 if !ok { 84 return "" 85 } 86 87 // Define easy getter to fetch build settings 88 getSetting := func(key string) string { 89 for i := 0; i < len(build.Settings); i++ { 90 if build.Settings[i].Key == key { 91 return build.Settings[i].Value 92 } 93 } 94 return "" 95 } 96 97 var info []string 98 99 if Version != "" { 100 // Append version if set 101 info = append(info, Version) 102 } 103 104 if vcs := getSetting("vcs"); vcs != "" { 105 // A VCS type was set (99.9% probably git) 106 107 if commit := getSetting("vcs.revision"); commit != "" { 108 if len(commit) > 7 { 109 // Truncate commit 110 commit = commit[:7] 111 } 112 113 // Append VCS + commit if set 114 info = append(info, vcs+"-"+commit) 115 } 116 } 117 118 return strings.Join(info, " ") 119 }