2023-03-12 16:00:57 +01:00
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2021-02-28 15:17:18 +01:00
2021-02-27 22:57:50 +01:00
package main
import (
2022-07-19 10:47:55 +02:00
"log"
2023-02-25 23:26:48 +01:00
"os"
2023-02-13 19:40:48 +01:00
godebug "runtime/debug"
2022-05-30 14:41:24 +02:00
"strings"
2022-04-02 15:40:09 +02:00
2023-02-13 19:40:48 +01:00
"codeberg.org/gruf/go-debug"
2021-12-07 13:31:39 +01:00
"github.com/spf13/cobra"
2021-03-03 11:28:28 +01:00
2021-07-31 17:49:59 +02:00
_ "github.com/superseriousbusiness/gotosocial/docs"
2021-12-07 13:31:39 +01:00
"github.com/superseriousbusiness/gotosocial/internal/config"
2021-02-27 22:57:50 +01:00
)
2022-04-02 15:40:09 +02:00
// Version is the version of GoToSocial being used.
// It's injected into the binary by the build script.
2021-06-28 12:17:20 +02:00
var Version string
2021-07-31 17:49:59 +02:00
//go:generate swagger generate spec
2021-02-27 22:57:50 +01:00
func main ( ) {
2022-05-30 14:41:24 +02:00
// Load version string
version := version ( )
2021-05-09 11:25:13 +02:00
2022-05-30 14:41:24 +02:00
// override version in config store
config . SetSoftwareVersion ( version )
2021-12-07 13:31:39 +01:00
// instantiate the root command
rootCmd := & cobra . Command {
2022-05-30 14:41:24 +02:00
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 )
} ,
2021-12-07 13:31:39 +01:00
SilenceErrors : true ,
SilenceUsage : true ,
}
// attach global flags to the root command so that they can be accessed from any subcommand
2022-05-30 14:41:24 +02:00
config . AddGlobalFlags ( rootCmd )
2021-03-02 22:52:31 +01:00
2021-12-07 13:31:39 +01:00
// add subcommands
rootCmd . AddCommand ( serverCommands ( ) )
rootCmd . AddCommand ( debugCommands ( ) )
rootCmd . AddCommand ( adminCommands ( ) )
2023-02-13 19:40:48 +01:00
if debug . DEBUG {
// only add testrig if debug enabled.
rootCmd . AddCommand ( testrigCommands ( ) )
2023-02-25 23:26:48 +01:00
} else if len ( os . Args ) > 1 && os . Args [ 1 ] == "testrig" {
log . Fatalln ( "gotosocial must be built and run with the DEBUG enviroment variable set to enable and access testrig" )
2023-02-13 19:40:48 +01:00
}
2021-12-07 13:31:39 +01:00
// run
if err := rootCmd . Execute ( ) ; err != nil {
2022-07-19 10:47:55 +02:00
log . Fatalf ( "error executing command: %s" , err )
2021-03-02 22:52:31 +01:00
}
2021-02-27 22:57:50 +01:00
}
2022-05-30 14:41:24 +02:00
// version will build a version string from binary's stored build information.
2024-02-12 12:03:56 +01:00
// It is SemVer-compatible so long as Version is SemVer-compatible.
2022-05-30 14:41:24 +02:00
func version ( ) string {
// Read build information from binary
2023-02-13 19:40:48 +01:00
build , ok := godebug . ReadBuildInfo ( )
2022-05-30 14:41:24 +02:00
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 )
}
}
2024-02-12 12:03:56 +01:00
return strings . Join ( info , "+" )
2022-05-30 14:41:24 +02:00
}