]> source.dussan.org Git - gitea.git/commitdiff
Add subcommand to create new user in CLI
authorUnknwon <u@gogs.io>
Sat, 13 Aug 2016 23:11:52 +0000 (16:11 -0700)
committerUnknwon <u@gogs.io>
Sat, 13 Aug 2016 23:11:52 +0000 (16:11 -0700)
README.md
cmd/admin.go [new file with mode: 0644]
gogs.go
templates/.VERSION

index e565d29a7ff0bdec08e89470aa7561ed43181486..08af17f949dca5e08a1a2d83c0bfb33034fab99f 100644 (file)
--- a/README.md
+++ b/README.md
@@ -3,7 +3,7 @@ Gogs - Go Git Service [![Build Status](https://travis-ci.org/gogits/gogs.svg?bra
 
 ![](https://github.com/gogits/gogs/blob/master/public/img/gogs-large-resize.png?raw=true)
 
-##### Current tip version: 0.9.74 (see [Releases](https://github.com/gogits/gogs/releases) for binary versions)
+##### Current tip version: 0.9.75 (see [Releases](https://github.com/gogits/gogs/releases) for binary versions)
 
 | Web | UI  | Preview  |
 |:-------------:|:-------:|:-------:|
diff --git a/cmd/admin.go b/cmd/admin.go
new file mode 100644 (file)
index 0000000..5202301
--- /dev/null
@@ -0,0 +1,70 @@
+// Copyright 2016 The Gogs Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package cmd
+
+import (
+       "fmt"
+
+       "github.com/codegangsta/cli"
+
+       "github.com/gogits/gogs/models"
+       "github.com/gogits/gogs/modules/setting"
+)
+
+var (
+       CmdAdmin = cli.Command{
+               Name:  "admin",
+               Usage: "Preform admin operations on command line",
+               Description: `Allow using internal logic of Gogs without hacking into the source code
+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{
+                       stringFlag("name", "", "Username"),
+                       stringFlag("password", "", "User password"),
+                       stringFlag("email", "", "User email address"),
+                       boolFlag("admin", "User is an admin"),
+                       stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"),
+               },
+       }
+)
+
+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
+}
diff --git a/gogs.go b/gogs.go
index 7f9dc60492a1106f24ddf249ed18c202a4e990b9..00498ed9aabb7e0d86779b59c69b22d0b91b7994 100644 (file)
--- a/gogs.go
+++ b/gogs.go
@@ -17,7 +17,7 @@ import (
        "github.com/gogits/gogs/modules/setting"
 )
 
-const APP_VER = "0.9.74.0811"
+const APP_VER = "0.9.75.0813"
 
 func init() {
        runtime.GOMAXPROCS(runtime.NumCPU())
@@ -35,6 +35,7 @@ func main() {
                cmd.CmdUpdate,
                cmd.CmdDump,
                cmd.CmdCert,
+               cmd.CmdAdmin,
        }
        app.Flags = append(app.Flags, []cli.Flag{}...)
        app.Run(os.Args)
index 267ba8d5500e301b0be692b124ac62adbff32297..56e398c5f35ba3071149b96be00e57757b3b0373 100644 (file)
@@ -1 +1 @@
-0.9.74.0811
\ No newline at end of file
+0.9.75.0813
\ No newline at end of file