2016-08-13 19:11:52 -04:00
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
2016-12-21 07:13:17 -05:00
|
|
|
// Copyright 2016 The Gitea Authors. All rights reserved.
|
2016-08-13 19:11:52 -04: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"
|
|
|
|
|
2016-08-30 07:57:58 -04:00
|
|
|
"github.com/urfave/cli"
|
2016-08-13 19:11:52 -04:00
|
|
|
|
2016-11-10 11:24:48 -05:00
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2016-08-13 19:11:52 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-11-04 07:42:18 -04:00
|
|
|
// CmdAdmin represents the available admin sub-command.
|
2016-08-13 19:11:52 -04:00
|
|
|
CmdAdmin = cli.Command{
|
|
|
|
Name: "admin",
|
|
|
|
Usage: "Preform admin operations on command line",
|
2016-12-21 07:13:17 -05:00
|
|
|
Description: `Allow using internal logic of Gitea without hacking into the source code
|
2016-08-13 19:11:52 -04:00
|
|
|
to make automatic initialization process more smoothly`,
|
|
|
|
Subcommands: []cli.Command{
|
|
|
|
subcmdCreateUser,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
subcmdCreateUser = cli.Command{
|
|
|
|
Name: "create-user",
|
|
|
|
Usage: "Create a new user in database",
|
|
|
|
Action: runCreateUser,
|
|
|
|
Flags: []cli.Flag{
|
2016-11-09 17:18:22 -05:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "name",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Username",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "password",
|
2016-11-09 17:32:24 -05:00
|
|
|
Value: "",
|
|
|
|
Usage: "User password",
|
2016-11-09 17:18:22 -05:00
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "email",
|
2016-11-09 17:32:24 -05:00
|
|
|
Value: "",
|
|
|
|
Usage: "User email address",
|
2016-11-09 17:18:22 -05: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-13 19:11:52 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func runCreateUser(c *cli.Context) error {
|
|
|
|
if !c.IsSet("name") {
|
|
|
|
return fmt.Errorf("Username is not specified")
|
|
|
|
} else if !c.IsSet("password") {
|
|
|
|
return fmt.Errorf("Password is not specified")
|
|
|
|
} else if !c.IsSet("email") {
|
|
|
|
return fmt.Errorf("Email is not specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.IsSet("config") {
|
|
|
|
setting.CustomConf = c.String("config")
|
|
|
|
}
|
|
|
|
|
|
|
|
setting.NewContext()
|
|
|
|
models.LoadConfigs()
|
|
|
|
models.SetEngine()
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|