diff --git a/internal/cmd/admin.go b/internal/cmd/admin.go index 8f40bf1c0..8ee80a144 100644 --- a/internal/cmd/admin.go +++ b/internal/cmd/admin.go @@ -25,6 +25,8 @@ var ( to make automatic initialization process more smoothly`, Subcommands: []cli.Command{ subcmdCreateUser, + subcmdCreateRepo, + subcmdDeleteInactivateUsers, subcmdDeleteRepositoryArchives, subcmdDeleteMissingRepositories, @@ -47,6 +49,20 @@ to make automatic initialization process more smoothly`, stringFlag("config, c", "", "Custom configuration file path"), }, } + subcmdCreateRepo = cli.Command{ + Name: "create-repo", + Usage: "Create a new repo in database for a user", + Action: runCreateRepo, + Flags: []cli.Flag{ + stringFlag("username", "", "Username of repository's owner"), + stringFlag("repository_name", "", "Repository name"), + stringFlag("private", "false", "Private repository"), + stringFlag("unlisted", "false", "Listable repository"), + stringFlag("mirror", "false", "Whether the repository is a mirror"), + + stringFlag("config, c", "", "Custom configuration file path"), + }, + } subcmdDeleteInactivateUsers = cli.Command{ Name: "delete-inactive-users", @@ -170,6 +186,50 @@ func runCreateUser(c *cli.Context) error { return nil } +func runCreateRepo(c *cli.Context) error { + if !c.IsSet("username") { + return errors.New("Username is not specified") + } else if !c.IsSet("repository_name") { + return errors.New("Respository name is not specified") + } + + err := conf.Init(c.String("config")) + if err != nil { + return errors.Wrap(err, "init configuration") + } + conf.InitLogging(true) + + if _, err = db.SetEngine(); err != nil { + return errors.Wrap(err, "set engine") + } + // find user by username + user, err := db.Users.GetByUsername( + context.Background(), + c.String("username"), + ) + if err != nil { + return errors.Wrap(err, "No user was found with "+c.String("username")) + } + + repo, err := db.CreateRepository( + user, + user, + db.CreateRepoOptionsLegacy{ + Name: c.String("repository_name"), + Description: "", + IsPrivate: c.Bool("private") || conf.Repository.ForcePrivate, + IsUnlisted: c.Bool("unlisted"), + IsMirror: c.Bool("mirror"), + }) + + if err != nil { + return errors.Wrap(err, "Repo") + } + + fmt.Printf("New repo %q has been successfully created!\n", repo.Name) + return nil +} + func adminDashboardOperation(operation func() error, successMessage string) func(*cli.Context) error { return func(c *cli.Context) error { err := conf.Init(c.String("config"))