2016-08-14 07:11:52 +08:00
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
2016-12-21 20:13:17 +08:00
|
|
|
// Copyright 2016 The Gitea Authors. All rights reserved.
|
2016-08-14 07:11:52 +08:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2017-12-31 22:45:46 +08:00
|
|
|
"code.gitea.io/git"
|
2016-11-11 00:24:48 +08:00
|
|
|
"code.gitea.io/gitea/models"
|
2017-12-31 22:45:46 +08:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2016-11-11 00:24:48 +08:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2017-04-12 15:44:54 +08:00
|
|
|
|
|
|
|
"github.com/urfave/cli"
|
2016-08-14 07:11:52 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-11-04 19:42:18 +08:00
|
|
|
// CmdAdmin represents the available admin sub-command.
|
2016-08-14 07:11:52 +08:00
|
|
|
CmdAdmin = cli.Command{
|
|
|
|
Name: "admin",
|
2018-01-13 06:16:49 +08:00
|
|
|
Usage: "Command line interface to perform common administrative operations",
|
2016-08-14 07:11:52 +08:00
|
|
|
Subcommands: []cli.Command{
|
|
|
|
subcmdCreateUser,
|
2017-03-20 16:23:38 +08:00
|
|
|
subcmdChangePassword,
|
2017-12-31 22:45:46 +08:00
|
|
|
subcmdRepoSyncReleases,
|
2016-08-14 07:11:52 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
subcmdCreateUser = cli.Command{
|
|
|
|
Name: "create-user",
|
|
|
|
Usage: "Create a new user in database",
|
|
|
|
Action: runCreateUser,
|
|
|
|
Flags: []cli.Flag{
|
2016-11-10 06:18:22 +08:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "name",
|
|
|
|
Usage: "Username",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "password",
|
2016-11-10 06:32:24 +08:00
|
|
|
Usage: "User password",
|
2016-11-10 06:18:22 +08:00
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "email",
|
2016-11-10 06:32:24 +08:00
|
|
|
Usage: "User email address",
|
2016-11-10 06:18:22 +08:00
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "admin",
|
|
|
|
Usage: "User is an admin",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "config, c",
|
|
|
|
Value: "custom/conf/app.ini",
|
|
|
|
Usage: "Custom configuration file path",
|
|
|
|
},
|
2016-08-14 07:11:52 +08:00
|
|
|
},
|
|
|
|
}
|
2017-03-20 16:23:38 +08:00
|
|
|
|
|
|
|
subcmdChangePassword = cli.Command{
|
|
|
|
Name: "change-password",
|
|
|
|
Usage: "Change a user's password",
|
|
|
|
Action: runChangePassword,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "username,u",
|
|
|
|
Value: "",
|
|
|
|
Usage: "The user to change password for",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "password,p",
|
|
|
|
Value: "",
|
|
|
|
Usage: "New password to set for user",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-12-31 22:45:46 +08:00
|
|
|
|
|
|
|
subcmdRepoSyncReleases = cli.Command{
|
|
|
|
Name: "repo-sync-releases",
|
|
|
|
Usage: "Synchronize repository releases with tags",
|
|
|
|
Action: runRepoSyncReleases,
|
|
|
|
}
|
2016-08-14 07:11:52 +08:00
|
|
|
)
|
|
|
|
|
2017-03-20 16:23:38 +08:00
|
|
|
func runChangePassword(c *cli.Context) error {
|
2018-01-13 06:16:49 +08:00
|
|
|
if err := argsSet(c, "username", "password"); err != nil {
|
|
|
|
return err
|
2017-03-20 16:23:38 +08:00
|
|
|
}
|
|
|
|
|
2018-01-13 06:16:49 +08:00
|
|
|
if err := initDB(); err != nil {
|
|
|
|
return err
|
2017-03-20 16:23:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uname := c.String("username")
|
|
|
|
user, err := models.GetUserByName(uname)
|
|
|
|
if err != nil {
|
2018-01-13 06:16:49 +08:00
|
|
|
return err
|
2017-03-20 16:23:38 +08:00
|
|
|
}
|
|
|
|
if user.Salt, err = models.GetUserSalt(); err != nil {
|
2018-01-13 06:16:49 +08:00
|
|
|
return err
|
2017-03-20 16:23:38 +08:00
|
|
|
}
|
2018-01-12 06:19:38 +08:00
|
|
|
user.HashPassword(c.String("password"))
|
2017-08-12 22:18:44 +08:00
|
|
|
if err := models.UpdateUserCols(user, "passwd", "salt"); err != nil {
|
2018-01-13 06:16:49 +08:00
|
|
|
return err
|
2017-03-20 16:23:38 +08:00
|
|
|
}
|
|
|
|
|
2018-01-13 06:16:49 +08:00
|
|
|
fmt.Printf("%s's password has been successfully updated!\n", user.Name)
|
2017-03-20 16:23:38 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-14 07:11:52 +08:00
|
|
|
func runCreateUser(c *cli.Context) error {
|
2018-01-13 06:16:49 +08:00
|
|
|
if err := argsSet(c, "name", "password", "email"); err != nil {
|
|
|
|
return err
|
2016-08-14 07:11:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.IsSet("config") {
|
|
|
|
setting.CustomConf = c.String("config")
|
|
|
|
}
|
|
|
|
|
2018-01-13 06:16:49 +08:00
|
|
|
if err := initDB(); err != nil {
|
|
|
|
return err
|
2017-01-05 16:31:07 +08:00
|
|
|
}
|
2016-08-14 07:11:52 +08:00
|
|
|
|
|
|
|
if err := models.CreateUser(&models.User{
|
|
|
|
Name: c.String("name"),
|
|
|
|
Email: c.String("email"),
|
|
|
|
Passwd: c.String("password"),
|
|
|
|
IsActive: true,
|
|
|
|
IsAdmin: c.Bool("admin"),
|
|
|
|
}); err != nil {
|
|
|
|
return fmt.Errorf("CreateUser: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("New user '%s' has been successfully created!\n", c.String("name"))
|
|
|
|
return nil
|
|
|
|
}
|
2017-12-31 22:45:46 +08:00
|
|
|
|
|
|
|
func runRepoSyncReleases(c *cli.Context) error {
|
2018-01-13 06:16:49 +08:00
|
|
|
if err := initDB(); err != nil {
|
|
|
|
return err
|
2017-12-31 22:45:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace("Synchronizing repository releases (this may take a while)")
|
|
|
|
for page := 1; ; page++ {
|
|
|
|
repos, count, err := models.SearchRepositoryByName(&models.SearchRepoOptions{
|
|
|
|
Page: page,
|
|
|
|
PageSize: models.RepositoryListDefaultPageSize,
|
|
|
|
Private: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2018-01-13 06:16:49 +08:00
|
|
|
return fmt.Errorf("SearchRepositoryByName: %v", err)
|
2017-12-31 22:45:46 +08:00
|
|
|
}
|
|
|
|
if len(repos) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
log.Trace("Processing next %d repos of %d", len(repos), count)
|
|
|
|
for _, repo := range repos {
|
|
|
|
log.Trace("Synchronizing repo %s with path %s", repo.FullName(), repo.RepoPath())
|
|
|
|
gitRepo, err := git.OpenRepository(repo.RepoPath())
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("OpenRepository: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-01-13 06:16:49 +08:00
|
|
|
oldnum, err := getReleaseCount(repo.ID)
|
2017-12-31 22:45:46 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Warn(" GetReleaseCountByRepoID: %v", err)
|
|
|
|
}
|
|
|
|
log.Trace(" currentNumReleases is %d, running SyncReleasesWithTags", oldnum)
|
|
|
|
|
|
|
|
if err = models.SyncReleasesWithTags(repo, gitRepo); err != nil {
|
|
|
|
log.Warn(" SyncReleasesWithTags: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-01-13 06:16:49 +08:00
|
|
|
count, err = getReleaseCount(repo.ID)
|
2017-12-31 22:45:46 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Warn(" GetReleaseCountByRepoID: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace(" repo %s releases synchronized to tags: from %d to %d",
|
|
|
|
repo.FullName(), oldnum, count)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-01-13 06:16:49 +08:00
|
|
|
|
|
|
|
func getReleaseCount(id int64) (int64, error) {
|
|
|
|
return models.GetReleaseCountByRepoID(
|
|
|
|
id,
|
|
|
|
models.FindReleasesOptions{
|
|
|
|
IncludeTags: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|