Browse Source

Add subcommand to create new user in CLI

Unknwon 3 years ago
parent
commit
0f33b04c87
4 changed files with 74 additions and 3 deletions
  1. 1 1
      README.md
  2. 70 0
      cmd/admin.go
  3. 2 1
      gogs.go
  4. 1 1
      templates/.VERSION

+ 1 - 1
README.md

@@ -3,7 +3,7 @@ Gogs - Go Git Service [![Build Status](https://travis-ci.org/gogits/gogs.svg?bra
3 3
 
4 4
 ![](https://github.com/gogits/gogs/blob/master/public/img/gogs-large-resize.png?raw=true)
5 5
 
6
-##### Current tip version: 0.9.74 (see [Releases](https://github.com/gogits/gogs/releases) for binary versions)
6
+##### Current tip version: 0.9.75 (see [Releases](https://github.com/gogits/gogs/releases) for binary versions)
7 7
 
8 8
 | Web | UI  | Preview  |
9 9
 |:-------------:|:-------:|:-------:|

+ 70 - 0
cmd/admin.go

@@ -0,0 +1,70 @@
1
+// Copyright 2016 The Gogs Authors. All rights reserved.
2
+// Use of this source code is governed by a MIT-style
3
+// license that can be found in the LICENSE file.
4
+
5
+package cmd
6
+
7
+import (
8
+	"fmt"
9
+
10
+	"github.com/codegangsta/cli"
11
+
12
+	"github.com/gogits/gogs/models"
13
+	"github.com/gogits/gogs/modules/setting"
14
+)
15
+
16
+var (
17
+	CmdAdmin = cli.Command{
18
+		Name:  "admin",
19
+		Usage: "Preform admin operations on command line",
20
+		Description: `Allow using internal logic of Gogs without hacking into the source code
21
+to make automatic initialization process more smoothly`,
22
+		Subcommands: []cli.Command{
23
+			subcmdCreateUser,
24
+		},
25
+	}
26
+
27
+	subcmdCreateUser = cli.Command{
28
+		Name:   "create-user",
29
+		Usage:  "Create a new user in database",
30
+		Action: runCreateUser,
31
+		Flags: []cli.Flag{
32
+			stringFlag("name", "", "Username"),
33
+			stringFlag("password", "", "User password"),
34
+			stringFlag("email", "", "User email address"),
35
+			boolFlag("admin", "User is an admin"),
36
+			stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"),
37
+		},
38
+	}
39
+)
40
+
41
+func runCreateUser(c *cli.Context) error {
42
+	if !c.IsSet("name") {
43
+		return fmt.Errorf("Username is not specified")
44
+	} else if !c.IsSet("password") {
45
+		return fmt.Errorf("Password is not specified")
46
+	} else if !c.IsSet("email") {
47
+		return fmt.Errorf("Email is not specified")
48
+	}
49
+
50
+	if c.IsSet("config") {
51
+		setting.CustomConf = c.String("config")
52
+	}
53
+
54
+	setting.NewContext()
55
+	models.LoadConfigs()
56
+	models.SetEngine()
57
+
58
+	if err := models.CreateUser(&models.User{
59
+		Name:     c.String("name"),
60
+		Email:    c.String("email"),
61
+		Passwd:   c.String("password"),
62
+		IsActive: true,
63
+		IsAdmin:  c.Bool("admin"),
64
+	}); err != nil {
65
+		return fmt.Errorf("CreateUser: %v", err)
66
+	}
67
+
68
+	fmt.Printf("New user '%s' has been successfully created!\n", c.String("name"))
69
+	return nil
70
+}

+ 2 - 1
gogs.go

@@ -17,7 +17,7 @@ import (
17 17
 	"github.com/gogits/gogs/modules/setting"
18 18
 )
19 19
 
20
-const APP_VER = "0.9.74.0811"
20
+const APP_VER = "0.9.75.0813"
21 21
 
22 22
 func init() {
23 23
 	runtime.GOMAXPROCS(runtime.NumCPU())
@@ -35,6 +35,7 @@ func main() {
35 35
 		cmd.CmdUpdate,
36 36
 		cmd.CmdDump,
37 37
 		cmd.CmdCert,
38
+		cmd.CmdAdmin,
38 39
 	}
39 40
 	app.Flags = append(app.Flags, []cli.Flag{}...)
40 41
 	app.Run(os.Args)

+ 1 - 1
templates/.VERSION

@@ -1 +1 @@
1
-0.9.74.0811
1
+0.9.75.0813