Skip to content

Commit

Permalink
create server command create-super-user
Browse files Browse the repository at this point in the history
  • Loading branch information
drgarcia1986 committed May 24, 2017
1 parent ba2df12 commit d09c8a5
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions pkg/server/cmd/user.go
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")
}

0 comments on commit d09c8a5

Please sign in to comment.