Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions internal/cmd/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ var (
to make automatic initialization process more smoothly`,
Subcommands: []cli.Command{
subcmdCreateUser,
subcmdCreateRepo,

subcmdDeleteInactivateUsers,
subcmdDeleteRepositoryArchives,
subcmdDeleteMissingRepositories,
Expand All @@ -47,6 +49,20 @@ to make automatic initialization process more smoothly`,
stringFlag("config, c", "", "Custom configuration file path"),
},
}
subcmdCreateRepo = cli.Command{
Name: "create-repository",
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",
Expand Down Expand Up @@ -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 repository %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"))
Expand Down