Pārlūkot izejas kodu

cmd/admin: add subcommands for all admin dashboard actions (#4813)

Josef Kemetmüller 6 gadi atpakaļ
vecāks
revīzija
b16c12f67b
1 mainītis faili ar 113 papildinājumiem un 0 dzēšanām
  1. 113 0
      cmd/admin.go

+ 113 - 0
cmd/admin.go

@@ -6,6 +6,8 @@ package cmd
 
 import (
 	"fmt"
+	"reflect"
+	"runtime"
 
 	"github.com/urfave/cli"
 
@@ -21,6 +23,13 @@ var (
 to make automatic initialization process more smoothly`,
 		Subcommands: []cli.Command{
 			subcmdCreateUser,
+			subcmdDeleteInactivateUsers,
+			subcmdDeleteRepositoryArchives,
+			subcmdDeleteMissingRepositories,
+			subcmdGitGcRepos,
+			subcmdRewriteAllPublicKeys,
+			subcmdSyncRepositoryHooks,
+			subcmdReinitMissingRepositories,
 		},
 	}
 
@@ -36,6 +45,90 @@ to make automatic initialization process more smoothly`,
 			stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"),
 		},
 	}
+
+	subcmdDeleteInactivateUsers = cli.Command{
+		Name:  "delete-inactive-users",
+		Usage: "Delete all inactive accounts",
+		Action: adminDashboardOperation(
+			models.DeleteInactivateUsers,
+			"All inactivate accounts have been deleted successfully",
+		),
+		Flags: []cli.Flag{
+			stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"),
+		},
+	}
+
+	subcmdDeleteRepositoryArchives = cli.Command{
+		Name:  "delete-repository-archives",
+		Usage: "Delete all repositories archives",
+		Action: adminDashboardOperation(
+			models.DeleteRepositoryArchives,
+			"All repositories archives have been deleted successfully",
+		),
+		Flags: []cli.Flag{
+			stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"),
+		},
+	}
+
+	subcmdDeleteMissingRepositories = cli.Command{
+		Name:  "delete-missing-repositories",
+		Usage: "Delete all repository records that lost Git files",
+		Action: adminDashboardOperation(
+			models.DeleteMissingRepositories,
+			"All repositories archives have been deleted successfully",
+		),
+		Flags: []cli.Flag{
+			stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"),
+		},
+	}
+
+	subcmdGitGcRepos = cli.Command{
+		Name:  "collect-garbage",
+		Usage: "Do garbage collection on repositories",
+		Action: adminDashboardOperation(
+			models.GitGcRepos,
+			"All repositories have done garbage collection successfully",
+		),
+		Flags: []cli.Flag{
+			stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"),
+		},
+	}
+
+	subcmdRewriteAllPublicKeys = cli.Command{
+		Name:  "rewrite-public-keys",
+		Usage: "Rewrite '.ssh/authorized_keys' file (caution: non-Gogs keys will be lost)",
+		Action: adminDashboardOperation(
+			models.RewriteAllPublicKeys,
+			"All public keys have been rewritten successfully",
+		),
+		Flags: []cli.Flag{
+			stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"),
+		},
+	}
+
+	subcmdSyncRepositoryHooks = cli.Command{
+		Name:  "resync-hooks",
+		Usage: "Resync pre-receive, update and post-receive hooks",
+		Action: adminDashboardOperation(
+			models.SyncRepositoryHooks,
+			"All repositories' pre-receive, update and post-receive hooks have been resynced successfully",
+		),
+		Flags: []cli.Flag{
+			stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"),
+		},
+	}
+
+	subcmdReinitMissingRepositories = cli.Command{
+		Name:  "reinit-missing-repositories",
+		Usage: "Reinitialize all repository records that lost Git files",
+		Action: adminDashboardOperation(
+			models.ReinitMissingRepositories,
+			"All repository records that lost Git files have been reinitialized successfully",
+		),
+		Flags: []cli.Flag{
+			stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"),
+		},
+	}
 )
 
 func runCreateUser(c *cli.Context) error {
@@ -68,3 +161,23 @@ func runCreateUser(c *cli.Context) error {
 	fmt.Printf("New user '%s' has been successfully created!\n", c.String("name"))
 	return nil
 }
+
+func adminDashboardOperation(operation func() error, successMessage string) func(*cli.Context) error {
+	return func(c *cli.Context) error {
+		if c.IsSet("config") {
+			setting.CustomConf = c.String("config")
+		}
+
+		setting.NewContext()
+		models.LoadConfigs()
+		models.SetEngine()
+
+		if err := operation(); err != nil {
+			functionName := runtime.FuncForPC(reflect.ValueOf(operation).Pointer()).Name()
+			return fmt.Errorf("%s: %v", functionName, err)
+		}
+
+		fmt.Printf("%s\n", successMessage)
+		return nil
+	}
+}