Skip to content
Open
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
1 change: 1 addition & 0 deletions manage/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ package manage
// AccountCommand is the main command for account management
type AccountCommand struct {
Cache AccountCacheCommand `command:"cache" alias:"c" description:"Cache the account assertions from the store in the database"`
Add AccountAddCommand `command:"add" alias:"a" description:"Add a new account"`
}
15 changes: 12 additions & 3 deletions manage/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,22 @@ func (s *AccountSuite) TestAccount(c *check.C) {
tests := []manTest{
{
Args: []string{"serial-vault-admin", "account"},
ErrorMessage: "Please specify the cache command"},
ErrorMessage: "Please specify one command of: add or cache"},
{
Args: []string{"serial-vault-admin", "account", "invalid"},
ErrorMessage: "Unknown command `invalid'. You should use the cache command"},
ErrorMessage: "Unknown command `invalid'. Please specify one command of: add or cache"},
{
Args: []string{"serial-vault-admin", "account", "cache"},
ErrorMessage: ""},
ErrorMessage: "",
},
{
Args: []string{"serial-vault-admin", "account", "add", "acc123"},
ErrorMessage: "",
},
{
Args: []string{"serial-vault-admin", "account", "add", "acc123", "-r"},
ErrorMessage: "",
},
}

for _, t := range tests {
Expand Down
43 changes: 43 additions & 0 deletions manage/accountadd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package manage

import (
"fmt"
"github.com/CanonicalLtd/serial-vault/datastore"
)

// AccountAddCommand handles adding a new account for the serial-vault-admin command
type AccountAddCommand struct {
ResellerAPI bool `short:"r" long:"reseller" description:"Enable the reseller API"`
}

// Execute the adding of an account
func (cmd AccountAddCommand) Execute(args []string) error {
err := checkAccountIDArg(args, "Add")
if err != nil {
return err
}

// Open the database and create the account
openDatabase()
account := datastore.Account{
AuthorityID: args[0],
ResellerAPI: cmd.ResellerAPI,
}
if err := datastore.Environ.DB.CreateAccount(account); err != nil {
return fmt.Errorf("error creating the account: %v", err)
}

fmt.Printf("Account '%s' created successfully\n", account.AuthorityID)
return nil
}

func checkAccountIDArg(args []string, action string) error {
switch len(args) {
case 0:
return fmt.Errorf("%s account expects an 'account ID' argument", action)
case 1:
return nil
default:
return fmt.Errorf("%s account expects a single 'account ID' argument", action)
}
}