Skip to content

Commit 0f33b04

Browse files
committed
Add subcommand to create new user in CLI
1 parent fd3b9ca commit 0f33b04

File tree

4 files changed

+74
-3
lines changed

4 files changed

+74
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Gogs - Go Git Service [![Build Status](https://travis-ci.org/gogits/gogs.svg?bra
33

44
![](https://github.com/gogits/gogs/blob/master/public/img/gogs-large-resize.png?raw=true)
55

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)
77

88
| Web | UI | Preview |
99
|:-------------:|:-------:|:-------:|

cmd/admin.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

gogs.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"github.com/gogits/gogs/modules/setting"
1818
)
1919

20-
const APP_VER = "0.9.74.0811"
20+
const APP_VER = "0.9.75.0813"
2121

2222
func init() {
2323
runtime.GOMAXPROCS(runtime.NumCPU())
@@ -35,6 +35,7 @@ func main() {
3535
cmd.CmdUpdate,
3636
cmd.CmdDump,
3737
cmd.CmdCert,
38+
cmd.CmdAdmin,
3839
}
3940
app.Flags = append(app.Flags, []cli.Flag{}...)
4041
app.Run(os.Args)

templates/.VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.9.74.0811
1+
0.9.75.0813

0 commit comments

Comments
 (0)