diff options
Diffstat (limited to 'cmd/gotosocial')
-rw-r--r-- | cmd/gotosocial/action/admin/account/account.go | 19 | ||||
-rw-r--r-- | cmd/gotosocial/action/admin/trans/export.go | 3 | ||||
-rw-r--r-- | cmd/gotosocial/action/admin/trans/import.go | 3 | ||||
-rw-r--r-- | cmd/gotosocial/action/debug/config/config.go | 22 | ||||
-rw-r--r-- | cmd/gotosocial/action/server/server.go | 8 | ||||
-rw-r--r-- | cmd/gotosocial/admin.go | 22 | ||||
-rw-r--r-- | cmd/gotosocial/common.go | 8 | ||||
-rw-r--r-- | cmd/gotosocial/debug.go | 4 | ||||
-rw-r--r-- | cmd/gotosocial/flag/admin.go | 62 | ||||
-rw-r--r-- | cmd/gotosocial/flag/global.go | 46 | ||||
-rw-r--r-- | cmd/gotosocial/flag/server.go | 117 | ||||
-rw-r--r-- | cmd/gotosocial/flag/usage.go | 82 | ||||
-rw-r--r-- | cmd/gotosocial/main.go | 92 | ||||
-rw-r--r-- | cmd/gotosocial/server.go | 5 |
14 files changed, 104 insertions, 389 deletions
diff --git a/cmd/gotosocial/action/admin/account/account.go b/cmd/gotosocial/action/admin/account/account.go index cdbe70c15..05f5a776b 100644 --- a/cmd/gotosocial/action/admin/account/account.go +++ b/cmd/gotosocial/action/admin/account/account.go @@ -24,7 +24,6 @@ import ( "fmt" "time" - "github.com/spf13/viper" "github.com/superseriousbusiness/gotosocial/cmd/gotosocial/action" "github.com/superseriousbusiness/gotosocial/internal/config" "github.com/superseriousbusiness/gotosocial/internal/db" @@ -41,7 +40,7 @@ var Create action.GTSAction = func(ctx context.Context) error { return fmt.Errorf("error creating dbservice: %s", err) } - username := viper.GetString(config.Keys.AdminAccountUsername) + username := config.GetAdminAccountUsername() if username == "" { return errors.New("no username set") } @@ -49,7 +48,7 @@ var Create action.GTSAction = func(ctx context.Context) error { return err } - email := viper.GetString(config.Keys.AdminAccountEmail) + email := config.GetAdminAccountEmail() if email == "" { return errors.New("no email set") } @@ -57,7 +56,7 @@ var Create action.GTSAction = func(ctx context.Context) error { return err } - password := viper.GetString(config.Keys.AdminAccountPassword) + password := config.GetAdminAccountPassword() if password == "" { return errors.New("no password set") } @@ -80,7 +79,7 @@ var Confirm action.GTSAction = func(ctx context.Context) error { return fmt.Errorf("error creating dbservice: %s", err) } - username := viper.GetString(config.Keys.AdminAccountUsername) + username := config.GetAdminAccountUsername() if username == "" { return errors.New("no username set") } @@ -115,7 +114,7 @@ var Promote action.GTSAction = func(ctx context.Context) error { return fmt.Errorf("error creating dbservice: %s", err) } - username := viper.GetString(config.Keys.AdminAccountUsername) + username := config.GetAdminAccountUsername() if username == "" { return errors.New("no username set") } @@ -147,7 +146,7 @@ var Demote action.GTSAction = func(ctx context.Context) error { return fmt.Errorf("error creating dbservice: %s", err) } - username := viper.GetString(config.Keys.AdminAccountUsername) + username := config.GetAdminAccountUsername() if username == "" { return errors.New("no username set") } @@ -179,7 +178,7 @@ var Disable action.GTSAction = func(ctx context.Context) error { return fmt.Errorf("error creating dbservice: %s", err) } - username := viper.GetString(config.Keys.AdminAccountUsername) + username := config.GetAdminAccountUsername() if username == "" { return errors.New("no username set") } @@ -217,7 +216,7 @@ var Password action.GTSAction = func(ctx context.Context) error { return fmt.Errorf("error creating dbservice: %s", err) } - username := viper.GetString(config.Keys.AdminAccountUsername) + username := config.GetAdminAccountUsername() if username == "" { return errors.New("no username set") } @@ -225,7 +224,7 @@ var Password action.GTSAction = func(ctx context.Context) error { return err } - password := viper.GetString(config.Keys.AdminAccountPassword) + password := config.GetAdminAccountPassword() if password == "" { return errors.New("no password set") } diff --git a/cmd/gotosocial/action/admin/trans/export.go b/cmd/gotosocial/action/admin/trans/export.go index a6eb3f73b..2b5f82ee8 100644 --- a/cmd/gotosocial/action/admin/trans/export.go +++ b/cmd/gotosocial/action/admin/trans/export.go @@ -23,7 +23,6 @@ import ( "errors" "fmt" - "github.com/spf13/viper" "github.com/superseriousbusiness/gotosocial/cmd/gotosocial/action" "github.com/superseriousbusiness/gotosocial/internal/config" "github.com/superseriousbusiness/gotosocial/internal/db/bundb" @@ -39,7 +38,7 @@ var Export action.GTSAction = func(ctx context.Context) error { exporter := trans.NewExporter(dbConn) - path := viper.GetString(config.Keys.AdminTransPath) + path := config.GetAdminTransPath() if path == "" { return errors.New("no path set") } diff --git a/cmd/gotosocial/action/admin/trans/import.go b/cmd/gotosocial/action/admin/trans/import.go index df7d32b69..75f2d6a5e 100644 --- a/cmd/gotosocial/action/admin/trans/import.go +++ b/cmd/gotosocial/action/admin/trans/import.go @@ -23,7 +23,6 @@ import ( "errors" "fmt" - "github.com/spf13/viper" "github.com/superseriousbusiness/gotosocial/cmd/gotosocial/action" "github.com/superseriousbusiness/gotosocial/internal/config" "github.com/superseriousbusiness/gotosocial/internal/db/bundb" @@ -39,7 +38,7 @@ var Import action.GTSAction = func(ctx context.Context) error { importer := trans.NewImporter(dbConn) - path := viper.GetString(config.Keys.AdminTransPath) + path := config.GetAdminTransPath() if path == "" { return errors.New("no path set") } diff --git a/cmd/gotosocial/action/debug/config/config.go b/cmd/gotosocial/action/debug/config/config.go index 92f18bdfd..010907eab 100644 --- a/cmd/gotosocial/action/debug/config/config.go +++ b/cmd/gotosocial/action/debug/config/config.go @@ -23,17 +23,29 @@ import ( "encoding/json" "fmt" - "github.com/spf13/viper" "github.com/superseriousbusiness/gotosocial/cmd/gotosocial/action" + "github.com/superseriousbusiness/gotosocial/internal/config" ) // Config just prints the collated config out to stdout as json. -var Config action.GTSAction = func(ctx context.Context) error { - allSettings := viper.AllSettings() - b, err := json.Marshal(&allSettings) +var Config action.GTSAction = func(ctx context.Context) (err error) { + var raw map[string]interface{} + + // Marshal configuration to a raw JSON map + config.Config(func(cfg *config.Configuration) { + raw, err = cfg.MarshalMap() + }) + if err != nil { + return err + } + + // Marshal map to JSON + b, err := json.Marshal(raw) if err != nil { return err } - fmt.Println(string(b)) + + // Print to stdout + fmt.Printf("%s\n", b) return nil } diff --git a/cmd/gotosocial/action/server/server.go b/cmd/gotosocial/action/server/server.go index 709b3d481..f266331f2 100644 --- a/cmd/gotosocial/action/server/server.go +++ b/cmd/gotosocial/action/server/server.go @@ -29,7 +29,6 @@ import ( "codeberg.org/gruf/go-store/kv" "codeberg.org/gruf/go-store/storage" "github.com/sirupsen/logrus" - "github.com/spf13/viper" "github.com/superseriousbusiness/gotosocial/cmd/gotosocial/action" "github.com/superseriousbusiness/gotosocial/internal/api" "github.com/superseriousbusiness/gotosocial/internal/api/client/account" @@ -107,7 +106,7 @@ var Start action.GTSAction = func(ctx context.Context) error { typeConverter := typeutils.NewConverter(dbService) // Open the storage backend - storageBasePath := viper.GetString(config.Keys.StorageLocalBasePath) + storageBasePath := config.GetStorageLocalBasePath() storage, err := kv.OpenFile(storageBasePath, &storage.DiskConfig{ // Put the store lockfile in the storage dir itself. // Normally this would not be safe, since we could end up @@ -134,8 +133,7 @@ var Start action.GTSAction = func(ctx context.Context) error { // decide whether to create a noop email sender (won't send emails) or a real one var emailSender email.Sender - smtpHost := viper.GetString(config.Keys.SMTPHost) - if smtpHost != "" { + if smtpHost := config.GetSMTPHost(); smtpHost != "" { // host is defined so create a proper sender emailSender, err = email.NewSender() if err != nil { @@ -239,7 +237,7 @@ var Start action.GTSAction = func(ctx context.Context) error { } // perform initial media prune in case value of MediaRemoteCacheDays changed - if err := processor.AdminMediaPrune(ctx, viper.GetInt(config.Keys.MediaRemoteCacheDays)); err != nil { + if err := processor.AdminMediaPrune(ctx, config.GetMediaRemoteCacheDays()); err != nil { return fmt.Errorf("error during initial media prune: %s", err) } diff --git a/cmd/gotosocial/admin.go b/cmd/gotosocial/admin.go index 2e4336d7a..cddc79f40 100644 --- a/cmd/gotosocial/admin.go +++ b/cmd/gotosocial/admin.go @@ -22,7 +22,6 @@ import ( "github.com/spf13/cobra" "github.com/superseriousbusiness/gotosocial/cmd/gotosocial/action/admin/account" "github.com/superseriousbusiness/gotosocial/cmd/gotosocial/action/admin/trans" - "github.com/superseriousbusiness/gotosocial/cmd/gotosocial/flag" "github.com/superseriousbusiness/gotosocial/internal/config" ) @@ -40,7 +39,7 @@ func adminCommands() *cobra.Command { Use: "account", Short: "admin commands related to accounts", } - flag.AdminAccount(adminAccountCmd, config.Defaults) + config.AddAdminAccount(adminAccountCmd) adminAccountCreateCmd := &cobra.Command{ Use: "create", @@ -52,7 +51,7 @@ func adminCommands() *cobra.Command { return run(cmd.Context(), account.Create) }, } - flag.AdminAccountCreate(adminAccountCreateCmd, config.Defaults) + config.AddAdminAccountCreate(adminAccountCreateCmd) adminAccountCmd.AddCommand(adminAccountCreateCmd) adminAccountConfirmCmd := &cobra.Command{ @@ -65,7 +64,7 @@ func adminCommands() *cobra.Command { return run(cmd.Context(), account.Confirm) }, } - flag.AdminAccount(adminAccountConfirmCmd, config.Defaults) + config.AddAdminAccount(adminAccountConfirmCmd) adminAccountCmd.AddCommand(adminAccountConfirmCmd) adminAccountPromoteCmd := &cobra.Command{ @@ -78,7 +77,7 @@ func adminCommands() *cobra.Command { return run(cmd.Context(), account.Promote) }, } - flag.AdminAccount(adminAccountPromoteCmd, config.Defaults) + config.AddAdminAccount(adminAccountPromoteCmd) adminAccountCmd.AddCommand(adminAccountPromoteCmd) adminAccountDemoteCmd := &cobra.Command{ @@ -91,7 +90,7 @@ func adminCommands() *cobra.Command { return run(cmd.Context(), account.Demote) }, } - flag.AdminAccount(adminAccountDemoteCmd, config.Defaults) + config.AddAdminAccount(adminAccountDemoteCmd) adminAccountCmd.AddCommand(adminAccountDemoteCmd) adminAccountDisableCmd := &cobra.Command{ @@ -104,7 +103,7 @@ func adminCommands() *cobra.Command { return run(cmd.Context(), account.Disable) }, } - flag.AdminAccount(adminAccountDisableCmd, config.Defaults) + config.AddAdminAccount(adminAccountDisableCmd) adminAccountCmd.AddCommand(adminAccountDisableCmd) adminAccountSuspendCmd := &cobra.Command{ @@ -117,7 +116,7 @@ func adminCommands() *cobra.Command { return run(cmd.Context(), account.Suspend) }, } - flag.AdminAccount(adminAccountSuspendCmd, config.Defaults) + config.AddAdminAccount(adminAccountSuspendCmd) adminAccountCmd.AddCommand(adminAccountSuspendCmd) adminAccountPasswordCmd := &cobra.Command{ @@ -130,7 +129,8 @@ func adminCommands() *cobra.Command { return run(cmd.Context(), account.Password) }, } - flag.AdminAccountPassword(adminAccountPasswordCmd, config.Defaults) + config.AddAdminAccount(adminAccountPasswordCmd) + config.AddAdminAccountPassword(adminAccountPasswordCmd) adminAccountCmd.AddCommand(adminAccountPasswordCmd) adminCmd.AddCommand(adminAccountCmd) @@ -149,7 +149,7 @@ func adminCommands() *cobra.Command { return run(cmd.Context(), trans.Export) }, } - flag.AdminTrans(adminExportCmd, config.Defaults) + config.AddAdminTrans(adminExportCmd) adminCmd.AddCommand(adminExportCmd) adminImportCmd := &cobra.Command{ @@ -162,7 +162,7 @@ func adminCommands() *cobra.Command { return run(cmd.Context(), trans.Import) }, } - flag.AdminTrans(adminImportCmd, config.Defaults) + config.AddAdminTrans(adminImportCmd) adminCmd.AddCommand(adminImportCmd) return adminCmd diff --git a/cmd/gotosocial/common.go b/cmd/gotosocial/common.go index 48d56a7a2..43e2f16ad 100644 --- a/cmd/gotosocial/common.go +++ b/cmd/gotosocial/common.go @@ -43,12 +43,12 @@ type preRunArgs struct { // of the config file from the viper store so that it can be picked up by either // env vars or cli flag. func preRun(a preRunArgs) error { - if err := config.InitViper(a.cmd.Flags()); err != nil { - return fmt.Errorf("error initializing viper: %s", err) + if err := config.BindFlags(a.cmd); err != nil { + return fmt.Errorf("error binding flags: %s", err) } - if err := config.ReadFromFile(); err != nil { - return fmt.Errorf("error initializing config: %s", err) + if err := config.Reload(); err != nil { + return fmt.Errorf("error reloading config: %s", err) } if !a.skipValidation { diff --git a/cmd/gotosocial/debug.go b/cmd/gotosocial/debug.go index b1a1560ec..b32a73920 100644 --- a/cmd/gotosocial/debug.go +++ b/cmd/gotosocial/debug.go @@ -21,7 +21,6 @@ package main import ( "github.com/spf13/cobra" configaction "github.com/superseriousbusiness/gotosocial/cmd/gotosocial/action/debug/config" - "github.com/superseriousbusiness/gotosocial/cmd/gotosocial/flag" "github.com/superseriousbusiness/gotosocial/internal/config" ) @@ -41,8 +40,7 @@ func debugCommands() *cobra.Command { return run(cmd.Context(), configaction.Config) }, } - flag.Server(debugConfigCmd, config.Defaults) - + config.AddServerFlags(debugConfigCmd) debugCmd.AddCommand(debugConfigCmd) return debugCmd } diff --git a/cmd/gotosocial/flag/admin.go b/cmd/gotosocial/flag/admin.go deleted file mode 100644 index e3829954f..000000000 --- a/cmd/gotosocial/flag/admin.go +++ /dev/null @@ -1,62 +0,0 @@ -/* - GoToSocial - Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org - - 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/>. -*/ - -package flag - -import ( - "github.com/spf13/cobra" - "github.com/superseriousbusiness/gotosocial/internal/config" -) - -// AdminAccount attaches flags pertaining to admin account actions. -func AdminAccount(cmd *cobra.Command, values config.Values) { - cmd.Flags().String(config.Keys.AdminAccountUsername, "", usage.AdminAccountUsername) // REQUIRED - if err := cmd.MarkFlagRequired(config.Keys.AdminAccountUsername); err != nil { - panic(err) - } -} - -// AdminAccountPassword attaches flags pertaining to admin account password reset. -func AdminAccountPassword(cmd *cobra.Command, values config.Values) { - AdminAccount(cmd, values) - cmd.Flags().String(config.Keys.AdminAccountPassword, "", usage.AdminAccountPassword) // REQUIRED - if err := cmd.MarkFlagRequired(config.Keys.AdminAccountPassword); err != nil { - panic(err) - } -} - -// AdminAccountCreate attaches flags pertaining to admin account creation. -func AdminAccountCreate(cmd *cobra.Command, values config.Values) { - AdminAccount(cmd, values) - cmd.Flags().String(config.Keys.AdminAccountPassword, "", usage.AdminAccountPassword) // REQUIRED - if err := cmd.MarkFlagRequired(config.Keys.AdminAccountPassword); err != nil { - panic(err) - } - cmd.Flags().String(config.Keys.AdminAccountEmail, "", usage.AdminAccountEmail) // REQUIRED - if err := cmd.MarkFlagRequired(config.Keys.AdminAccountEmail); err != nil { - panic(err) - } -} - -// AdminTrans attaches flags pertaining to import/export commands. -func AdminTrans(cmd *cobra.Command, values config.Values) { - cmd.Flags().String(config.Keys.AdminTransPath, "", usage.AdminTransPath) // REQUIRED - if err := cmd.MarkFlagRequired(config.Keys.AdminTransPath); err != nil { - panic(err) - } -} diff --git a/cmd/gotosocial/flag/global.go b/cmd/gotosocial/flag/global.go deleted file mode 100644 index 173180024..000000000 --- a/cmd/gotosocial/flag/global.go +++ /dev/null @@ -1,46 +0,0 @@ -/* - GoToSocial - Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org - - 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/>. -*/ - -package flag - -import ( - "github.com/spf13/cobra" - "github.com/superseriousbusiness/gotosocial/internal/config" -) - -// Global attaches flags that are common to all commands, aka persistent commands. -func Global(cmd *cobra.Command, values config.Values) { - // general stuff - cmd.PersistentFlags().String(config.Keys.ApplicationName, values.ApplicationName, usage.ApplicationName) - cmd.PersistentFlags().String(config.Keys.Host, values.Host, usage.Host) - cmd.PersistentFlags().String(config.Keys.AccountDomain, values.AccountDomain, usage.AccountDomain) - cmd.PersistentFlags().String(config.Keys.Protocol, values.Protocol, usage.Protocol) - cmd.PersistentFlags().String(config.Keys.LogLevel, values.LogLevel, usage.LogLevel) - cmd.PersistentFlags().Bool(config.Keys.LogDbQueries, values.LogDbQueries, usage.LogDbQueries) - cmd.PersistentFlags().String(config.Keys.ConfigPath, values.ConfigPath, usage.ConfigPath) - - // database stuff - cmd.PersistentFlags().String(config.Keys.DbType, values.DbType, usage.DbType) - cmd.PersistentFlags().String(config.Keys.DbAddress, values.DbAddress, usage.DbAddress) - cmd.PersistentFlags().Int(config.Keys.DbPort, values.DbPort, usage.DbPort) - cmd.PersistentFlags().String(config.Keys.DbUser, values.DbUser, usage.DbUser) - cmd.PersistentFlags().String(config.Keys.DbPassword, values.DbPassword, usage.DbPassword) - cmd.PersistentFlags().String(config.Keys.DbDatabase, values.DbDatabase, usage.DbDatabase) - cmd.PersistentFlags().String(config.Keys.DbTLSMode, values.DbTLSMode, usage.DbTLSMode) - cmd.PersistentFlags().String(config.Keys.DbTLSCACert, values.DbTLSCACert, usage.DbTLSCACert) -} diff --git a/cmd/gotosocial/flag/server.go b/cmd/gotosocial/flag/server.go deleted file mode 100644 index 418e48af6..000000000 --- a/cmd/gotosocial/flag/server.go +++ /dev/null @@ -1,117 +0,0 @@ -/* - GoToSocial - Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org - - 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/>. -*/ - -package flag - -import ( - "github.com/spf13/cobra" - "github.com/superseriousbusiness/gotosocial/internal/config" -) - -// Server attaches all flags pertaining to running the GtS server or testrig. -func Server(cmd *cobra.Command, values config.Values) { - Template(cmd, values) - Accounts(cmd, values) - Media(cmd, values) - Storage(cmd, values) - Statuses(cmd, values) - LetsEncrypt(cmd, values) - OIDC(cmd, values) - SMTP(cmd, values) - Router(cmd, values) - Syslog(cmd, values) -} - -// Router attaches flags pertaining to the gin router. -func Router(cmd *cobra.Command, values config.Values) { - cmd.PersistentFlags().String(config.Keys.BindAddress, values.BindAddress, usage.BindAddress) - cmd.PersistentFlags().Int(config.Keys.Port, values.Port, usage.Port) - cmd.PersistentFlags().StringSlice(config.Keys.TrustedProxies, values.TrustedProxies, usage.TrustedProxies) -} - -// Template attaches flags pertaining to templating config. -func Template(cmd *cobra.Command, values config.Values) { - cmd.Flags().String(config.Keys.WebTemplateBaseDir, values.WebTemplateBaseDir, usage.WebTemplateBaseDir) - cmd.Flags().String(config.Keys.WebAssetBaseDir, values.WebAssetBaseDir, usage.WebAssetBaseDir) -} - -// Accounts attaches flags pertaining to account config. -func Accounts(cmd *cobra.Command, values config.Values) { - cmd.Flags().Bool(config.Keys.AccountsRegistrationOpen, values.AccountsRegistrationOpen, usage.AccountsRegistrationOpen) - cmd.Flags().Bool(config.Keys.AccountsApprovalRequired, values.AccountsApprovalRequired, usage.AccountsApprovalRequired) - cmd.Flags().Bool(config.Keys.AccountsReasonRequired, values.AccountsReasonRequired, usage.AccountsReasonRequired) -} - -// Media attaches flags pertaining to media config. -func Media(cmd *cobra.Command, values config.Values) { - cmd.Flags().Int(config.Keys.MediaImageMaxSize, values.MediaImageMaxSize, usage.MediaImageMaxSize) - cmd.Flags().Int(config.Keys.MediaVideoMaxSize, values.MediaVideoMaxSize, usage.MediaVideoMaxSize) - cmd.Flags().Int(config.Keys.MediaDescriptionMinChars, values.MediaDescriptionMinChars, usage.MediaDescriptionMinChars) - cmd.Flags().Int(config.Keys.MediaDescriptionMaxChars, values.MediaDescriptionMaxChars, usage.MediaDescriptionMaxChars) - cmd.Flags().Int(config.Keys.MediaRemoteCacheDays, values.MediaRemoteCacheDays, usage.MediaRemoteCacheDays) -} - -// Storage attaches flags pertaining to storage config. -func Storage(cmd *cobra.Command, values config.Values) { - cmd.Flags().String(config.Keys.StorageBackend, values.StorageBackend, usage.StorageBackend) - cmd.Flags().String(config.Keys.StorageLocalBasePath, values.StorageLocalBasePath, usage.StorageLocalBasePath) -} - -// Statuses attaches flags pertaining to statuses config. -func Statuses(cmd *cobra.Command, values config.Values) { - cmd.Flags().Int(config.Keys.StatusesMaxChars, values.StatusesMaxChars, usage.StatusesMaxChars) - cmd.Flags().Int(config.Keys.StatusesCWMaxChars, values.StatusesCWMaxChars, usage.StatusesCWMaxChars) - cmd.Flags().Int(config.Keys.StatusesPollMaxOptions, values.StatusesPollMaxOptions, usage.StatusesPollMaxOptions) - cmd.Flags().Int(config.Keys.StatusesPollOptionMaxChars, values.StatusesPollOptionMaxChars, usage.StatusesPollOptionMaxChars) - cmd.Flags().Int(config.Keys.StatusesMediaMaxFiles, values.StatusesMediaMaxFiles, usage.StatusesMediaMaxFiles) -} - -// LetsEncrypt attaches flags pertaining to letsencrypt config. -func LetsEncrypt(cmd *cobra.Command, values config.Values) { - cmd.Flags().Bool(config.Keys.LetsEncryptEnabled, values.LetsEncryptEnabled, usage.LetsEncryptEnabled) - cmd.Flags().Int(config.Keys.LetsEncryptPort, values.LetsEncryptPort, usage.LetsEncryptPort) - cmd.Flags().String(config.Keys.LetsEncryptCertDir, values.LetsEncryptCertDir, usage.LetsEncryptCertDir) - cmd.Flags().String(config.Keys.LetsEncryptEmailAddress, values.LetsEncryptEmailAddress, usage.LetsEncryptEmailAddress) -} - -// OIDC attaches flags pertaining to oidc config. -func OIDC(cmd *cobra.Command, values config.Values) { - cmd.Flags().Bool(config.Keys.OIDCEnabled, values.OIDCEnabled, usage.OIDCEnabled) - cmd.Flags().String(config.Keys.OIDCIdpName, values.OIDCIdpName, usage.OIDCIdpName) - cmd.Flags().Bool(config.Keys.OIDCSkipVerification, values.OIDCSkipVerification, usage.OIDCSkipVerification) - cmd.Flags().String(config.Keys.OIDCIssuer, values.OIDCIssuer, usage.OIDCIssuer) - cmd.Flags().String(config.Keys.OIDCClientID, values.OIDCClientID, usage.OIDCClientID) - cmd.Flags().String(config.Keys.OIDCClientSecret, values.OIDCClientSecret, usage.OIDCClientSecret) - cmd.Flags().StringSlice(config.Keys.OIDCScopes, values.OIDCScopes, usage.OIDCScopes) -} - -// SMTP attaches flags pertaining to smtp/email config. -func SMTP(cmd *cobra.Command, values config.Values) { - cmd.Flags().String(config.Keys.SMTPHost, values.SMTPHost, usage.SMTPHost) - cmd.Flags().Int(config.Keys.SMTPPort, values.SMTPPort, usage.SMTPPort) - cmd.Flags().String(config.Keys.SMTPUsername, values.SMTPUsername, usage.SMTPUsername) - cmd.Flags().String(config.Keys.SMTPPassword, values.SMTPPassword, usage.SMTPPassword) - cmd.Flags().String(config.Keys.SMTPFrom, values.SMTPFrom, usage.SMTPFrom) -} - -// Syslog attaches flags pertaining to syslog config. -func Syslog(cmd *cobra.Command, values config.Values) { - cmd.Flags().Bool(config.Keys.SyslogEnabled, values.SyslogEnabled, usage.SyslogEnabled) - cmd.Flags().String(config.Keys.SyslogProtocol, values.SyslogProtocol, usage.SyslogProtocol) - cmd.Flags().String(config.Keys.SyslogAddress, values.SyslogAddress, usage.SyslogAddress) -} diff --git a/cmd/gotosocial/flag/usage.go b/cmd/gotosocial/flag/usage.go deleted file mode 100644 index 3393f4675..000000000 --- a/cmd/gotosocial/flag/usage.go +++ /dev/null @@ -1,82 +0,0 @@ -/* - GoToSocial - Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org - - 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/>. -*/ - -package flag - -import "github.com/superseriousbusiness/gotosocial/internal/config" - -var usage = config.KeyNames{ - LogLevel: "Log level to run at: [trace, debug, info, warn, fatal]", - LogDbQueries: "Log database queries verbosely when log-level is trace or debug", - ApplicationName: "Name of the application, used in various places internally", - ConfigPath: "Path to a file containing gotosocial configuration. Values set in this file will be overwritten by values set as env vars or arguments", - Host: "Hostname to use for the server (eg., example.org, gotosocial.whatever.com). This value must be set. DO NOT change this on a server that's already run!", - AccountDomain: "Domain to use in account names (eg., example.org, whatever.com). If not set, will default to the setting for host. DO NOT change this on a server that's already run!", - Protocol: "Protocol to use for the REST api of the server. This value must be set to one of http or https; only use http for debugging and tests!", - BindAddress: "Bind address to use for the GoToSocial server (eg., 0.0.0.0, 172.138.0.9, [::], localhost). For ipv6, enclose the address in square brackets, eg [2001:db8::fed1]. Default binds to all interfaces.", - Port: "Port to use for GoToSocial. Change this to 443 if you're running the binary directly on the host machine.", - TrustedProxies: "Proxies to trust when parsing x-forwarded headers into real IPs.", - DbType: "Database type: eg., postgres", - DbAddress: "Database ipv4 address, hostname, or filename", - DbPort: "Database port", - DbUser: "Database username", - DbPassword: "Database password", - DbDatabase: "Database name", - DbTLSMode: "Database tls mode", - DbTLSCACert: "Path to CA cert for db tls connection", - WebTemplateBaseDir: "Basedir for html templating files for rendering pages and composing emails.", - WebAssetBaseDir: "Directory to serve static assets from, accessible at example.org/assets/", - AccountsRegistrationOpen: "Allow anyone to submit an account signup request. If false, server will be invite-only.", - AccountsApprovalRequired: "Do account signups require approval by an admin or moderator before user can log in? If false, new registrations will be automatically approved.", - AccountsReasonRequired: "Do new account signups require a reason to be submitted on registration?", - MediaImageMaxSize: "Max size of accepted images in bytes", - MediaVideoMaxSize: "Max size of accepted videos in bytes", - MediaDescriptionMinChars: "Min required chars for an image description", - MediaDescriptionMaxChars: "Max permitted chars for an image description", - MediaRemoteCacheDays: "Number of days to locally cache media from remote instances. If set to 0, remote media will be kept indefinitely.", - StorageBackend: "Storage backend to use for media attachments", - StorageLocalBasePath: "Full path to an already-created directory where gts should store/retrieve media files. Subfolders will be created within this dir.", - StatusesMaxChars: "Max permitted characters for posted statuses", - StatusesCWMaxChars: "Max permitted characters for content/spoiler warnings on statuses", - StatusesPollMaxOptions: "Max amount of options permitted on a poll", - StatusesPollOptionMaxChars: "Max amount of characters for a poll option", - StatusesMediaMaxFiles: "Maximum number of media files/attachments per status", - LetsEncryptEnabled: "Enable letsencrypt TLS certs for this server. If set to true, then cert dir also needs to be set (or take the default).", - LetsEncryptPort: "Port to listen on for letsencrypt certificate challenges. Must not be the same as the GtS webserver/API port.", - LetsEncryptCertDir: "Directory to store acquired letsencrypt certificates.", - LetsEncryptEmailAddress: "Email address to use when requesting letsencrypt certs. Will receive updates on cert expiry etc.", - OIDCEnabled: "Enabled OIDC authorization for this instance. If set to true, then the other OIDC flags must also be set.", - OIDCIdpName: "Name of the OIDC identity provider. Will be shown to the user when logging in.", - OIDCSkipVerification: "Skip verification of tokens returned by the OIDC provider. Should only be set to 'true' for testing purposes, never in a production environment!", - OIDCIssuer: "Address of the OIDC issuer. Should be the web address, including protocol, at which the issuer can be reached. Eg., 'https://example.org/auth'", - OIDCClientID: "ClientID of GoToSocial, as registered with the OIDC provider.", - OIDCClientSecret: "ClientSecret of GoToSocial, as registered with the OIDC provider.", - OIDCScopes: "OIDC scopes.", - SMTPHost: "Host of the smtp server. Eg., 'smtp.eu.mailgun.org'", - SMTPPort: "Port of the smtp server. Eg., 587", - SMTPUsername: "Username to authenticate with the smtp server as. Eg., 'postmaster@mail.example.org'", - SMTPPassword: "Password to pass to the smtp server.", - SMTPFrom: "Address to use as the 'from' field of the email. Eg., 'gotosocial@example.org'", - SyslogEnabled: "Enable the syslog logging hook. Logs will be mirrored to the configured destination.", - SyslogProtocol: "Protocol to use when directing logs to syslog. Leave empty to connect to local syslog.", - SyslogAddress: "Address:port to send syslog logs to. Leave empty to connect to local syslog.", - AdminAccountUsername: "the username to create/delete/etc", - AdminAccountEmail: "the email address of this account", - AdminAccountPassword: "the password to set for this account", - AdminTransPath: "the path of the file to import from/export to", -} 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, " ") +} diff --git a/cmd/gotosocial/server.go b/cmd/gotosocial/server.go index fda5bad90..42d5543ea 100644 --- a/cmd/gotosocial/server.go +++ b/cmd/gotosocial/server.go @@ -21,7 +21,6 @@ package main import ( "github.com/spf13/cobra" "github.com/superseriousbusiness/gotosocial/cmd/gotosocial/action/server" - "github.com/superseriousbusiness/gotosocial/cmd/gotosocial/flag" "github.com/superseriousbusiness/gotosocial/internal/config" ) @@ -31,7 +30,6 @@ func serverCommands() *cobra.Command { Use: "server", Short: "gotosocial server-related tasks", } - serverStartCmd := &cobra.Command{ Use: "start", Short: "start the gotosocial server", @@ -42,8 +40,7 @@ func serverCommands() *cobra.Command { return run(cmd.Context(), server.Start) }, } - flag.Server(serverStartCmd, config.Defaults) - + config.AddServerFlags(serverStartCmd) serverCmd.AddCommand(serverStartCmd) return serverCmd } |