-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create server command
create-super-user
- Loading branch information
1 parent
ba2df12
commit d09c8a5
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/luizalabs/teresa-api/pkg/client" | ||
"github.com/luizalabs/teresa-api/pkg/server/auth" | ||
"github.com/luizalabs/teresa-api/pkg/server/user" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var createSuperUserCmd = &cobra.Command{ | ||
Use: "create-super-user", | ||
Short: "Create an admin user", | ||
Run: createSuperUser, | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(createSuperUserCmd) | ||
createSuperUserCmd.Flags().String("name", "Admin", "super user name") | ||
createSuperUserCmd.Flags().String("email", "admin@teresa.io", "super user email") | ||
createSuperUserCmd.Flags().String("password", "", "super user password [required]") | ||
} | ||
|
||
func createSuperUser(cmd *cobra.Command, args []string) { | ||
name, _ := cmd.Flags().GetString("name") | ||
email, _ := cmd.Flags().GetString("email") | ||
pass, _ := cmd.Flags().GetString("password") | ||
if pass == "" { | ||
fmt.Fprintln(os.Stderr, "Password required") | ||
return | ||
} | ||
|
||
db, err := getDB() | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, "Error on connect to Database: ", err) | ||
return | ||
} | ||
|
||
uOps := user.NewDatabaseOperations(db, auth.NewFake()) | ||
if err := uOps.Create(name, email, pass, true); err != nil { | ||
fmt.Fprintln(os.Stderr, "Error on create super user:", client.GetErrorMsg(err)) | ||
return | ||
} | ||
fmt.Println("Super User created") | ||
} |