diff options
author | Unknwon <u@gogs.io> | 2016-08-13 16:11:52 -0700 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2016-08-13 16:11:52 -0700 |
commit | 0f33b04c876593e592887450302774654fef2787 (patch) | |
tree | 3cc1fc91131f3abcb526e70b2a3ff60acdb35def /cmd/admin.go | |
parent | fd3b9ca3aa4fe5cdb5b4cf08b663c28fb731112f (diff) | |
download | gitea-0f33b04c876593e592887450302774654fef2787.tar.gz gitea-0f33b04c876593e592887450302774654fef2787.zip |
Add subcommand to create new user in CLI
Diffstat (limited to 'cmd/admin.go')
-rw-r--r-- | cmd/admin.go | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/cmd/admin.go b/cmd/admin.go new file mode 100644 index 0000000000..5202301d26 --- /dev/null +++ b/cmd/admin.go @@ -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 +} |