Skip to content

Commit

Permalink
update 'zrok admin create account' CLI to use the new API endpoint (#734
Browse files Browse the repository at this point in the history
)
  • Loading branch information
michaelquigley committed Aug 19, 2024
1 parent 60b248c commit e9b2a27
Showing 1 changed file with 15 additions and 31 deletions.
46 changes: 15 additions & 31 deletions cmd/zrok/adminCreateAccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package main

import (
"fmt"
"github.com/openziti/zrok/controller"
"github.com/openziti/zrok/controller/config"
"github.com/openziti/zrok/controller/store"
"github.com/openziti/zrok/environment"
"github.com/openziti/zrok/rest_client_zrok/admin"
"github.com/spf13/cobra"
)

Expand All @@ -18,49 +17,34 @@ type adminCreateAccount struct {

func newAdminCreateAccount() *adminCreateAccount {
cmd := &cobra.Command{
Use: "account <configPath}> <email> <password>",
Use: "account <email> <password>",
Short: "Pre-populate an account in the database; returns an enable token for the account",
Args: cobra.ExactArgs(3),
Args: cobra.ExactArgs(2),
}
command := &adminCreateAccount{cmd: cmd}
cmd.Run = command.run
return command
}

func (cmd *adminCreateAccount) run(_ *cobra.Command, args []string) {
cfg, err := config.LoadConfig(args[0])
env, err := environment.LoadRoot()
if err != nil {
panic(err)
}
str, err := store.Open(cfg.Store)
if err != nil {
panic(err)
}
token, err := controller.CreateToken()
if err != nil {
panic(err)
}
hpwd, err := controller.HashPassword(args[2])

zrok, err := env.Client()
if err != nil {
panic(err)
}
trx, err := str.Begin()

req := admin.NewCreateAccountParams()
req.Body.Email = args[0]
req.Body.Password = args[1]

resp, err := zrok.Admin.CreateAccount(req, mustGetAdminAuth())
if err != nil {
panic(err)
}
defer func() {
if err := trx.Commit(); err != nil {
panic(err)
}
}()
a := &store.Account{
Email: args[1],
Salt: hpwd.Salt,
Password: hpwd.Password,
Token: token,
}
if _, err := str.CreateAccount(a, trx); err != nil {
panic(err)
}
fmt.Println(token)

fmt.Println(resp.GetPayload().Token)
}

0 comments on commit e9b2a27

Please sign in to comment.