Skip to content

Commit

Permalink
cli: Add .weight command to update a node's weight
Browse files Browse the repository at this point in the history
  • Loading branch information
freeekanayaka committed Jul 3, 2020
1 parent b47e468 commit 5fac55a
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions internal/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"database/sql"
"encoding/json"
"fmt"
"strconv"
"strings"

"github.com/canonical/go-dqlite/client"
Expand Down Expand Up @@ -71,6 +72,9 @@ func (s *Shell) Process(ctx context.Context, line string) (string, error) {
if strings.HasPrefix(strings.ToLower(strings.TrimLeft(line, " ")), ".describe") {
return s.processDescribe(ctx, line)
}
if strings.HasPrefix(strings.ToLower(strings.TrimLeft(line, " ")), ".weight") {
return s.processWeight(ctx, line)
}
if strings.HasPrefix(strings.ToUpper(strings.TrimLeft(line, " ")), "SELECT") {
return s.processSelect(ctx, line)
} else {
Expand Down Expand Up @@ -183,6 +187,28 @@ func (s *Shell) processDescribe(ctx context.Context, line string) (string, error
return result, nil
}

func (s *Shell) processWeight(ctx context.Context, line string) (string, error) {
parts := strings.Split(line, " ")
if len(parts) != 3 {
return "", fmt.Errorf("bad command format, should be: .weight <address> <n>")
}
address := parts[1]
weight, err := strconv.Atoi(parts[2])
if err != nil || weight < 0 {
return "", fmt.Errorf("bad weight %q", parts[2])
}

cli, err := client.New(ctx, address, client.WithDialFunc(s.dial))
if err != nil {
return "", err
}
if err := cli.Weight(ctx, uint64(weight)); err != nil {
return "", err
}

return "", nil
}

func (s *Shell) processSelect(ctx context.Context, line string) (string, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
Expand Down

0 comments on commit 5fac55a

Please sign in to comment.