Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command create-super-user in server command line interface #198

Merged
merged 3 commits into from
May 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ COPY . /go/src/github.com/luizalabs/teresa-api

RUN go build -i -o teresa ./cmd/server/main.go

CMD ["./teresa", "--port", "8080"]
CMD ["./teresa", "run", "--port", "8080"]
EXPOSE 8080
2 changes: 1 addition & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func main() {
}
}()

if err := cmd.RunCmd.Execute(); err != nil {
if err := cmd.RootCmd.Execute(); err != nil {
log.Fatal(err)
}
}
15 changes: 15 additions & 0 deletions pkg/server/cmd/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cmd

import (
"github.com/jinzhu/gorm"
"github.com/kelseyhightower/envconfig"
"github.com/luizalabs/teresa-api/pkg/server/database"
)

func getDB() (*gorm.DB, error) {
dbConf := new(database.Config)
if err := envconfig.Process("teresadb", dbConf); err != nil {
return nil, err
}
return database.New(*dbConf)
}
9 changes: 9 additions & 0 deletions pkg/server/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package cmd

import "github.com/spf13/cobra"

var RootCmd = &cobra.Command{
Use: "teresa-server",
Short: "teresa-server",
Long: "Teresa server, use to start Teresa gRPC server and create super user",
}
23 changes: 6 additions & 17 deletions pkg/server/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,26 @@ package cmd
import (
"log"

"github.com/jinzhu/gorm"
"github.com/kelseyhightower/envconfig"
"github.com/luizalabs/teresa-api/pkg/server"
"github.com/luizalabs/teresa-api/pkg/server/auth"
"github.com/luizalabs/teresa-api/pkg/server/database"
"github.com/luizalabs/teresa-api/pkg/server/secrets"
"github.com/spf13/cobra"
)

var RunCmd = &cobra.Command{
Use: "",
Short: "",
Long: ``,
var runCmd = &cobra.Command{
Use: "run",
Short: "Start Teresa gRPC server",
Run: runServer,
}

var port string

func init() {
RunCmd.Flags().StringVarP(&port, "port", "p", "50051", "TCP port to create a listener")
RootCmd.AddCommand(runCmd)
runCmd.Flags().String("port", "50051", "TCP port to create a listener")
}

func runServer(cmd *cobra.Command, args []string) {
port, _ := cmd.Flags().GetString("port")
db, err := getDB()
if err != nil {
log.Fatal("Error on connect to Database: ", err)
Expand Down Expand Up @@ -77,11 +74,3 @@ func getAuth(s secrets.Secrets) (auth.Auth, error) {
}
return auth.New(private, public), nil
}

func getDB() (*gorm.DB, error) {
dbConf := new(database.Config)
if err := envconfig.Process("teresadb", dbConf); err != nil {
return nil, err
}
return database.New(*dbConf)
}
52 changes: 52 additions & 0 deletions pkg/server/cmd/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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", "", "super user email [required]")
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
}
if email == "" {
fmt.Fprintln(os.Stderr, "E-mail 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")
}