Skip to content

Commit

Permalink
Add UpdateBridge CLI command
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryNguyen5 committed Sep 19, 2024
1 parent 7b324ca commit f24a153
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
23 changes: 23 additions & 0 deletions core/cmd/bridge_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,29 @@ func (s *Shell) CreateBridge(c *cli.Context) (err error) {
return s.renderAPIResponse(resp, &BridgePresenter{})
}

func (s *Shell) UpdateBridge(c *cli.Context) (err error) {
if !c.Args().Present() {
return s.errorOut(errors.New("must pass the name of the bridge to be updated"))
}
bridgeName := c.Args().First()
buf, err := getBufferFromJSON(c.Args().Get(1))
if err != nil {
return s.errorOut(err)
}

resp, err := s.HTTP.Patch(s.ctx(), "/v2/bridge_types/"+bridgeName, buf)
if err != nil {
return s.errorOut(err)
}
defer func() {
if cerr := resp.Body.Close(); cerr != nil {
err = multierr.Append(err, cerr)
}
}()

return s.renderAPIResponse(resp, &BridgePresenter{})
}

// RemoveBridge removes a specific Bridge by name.
func (s *Shell) RemoveBridge(c *cli.Context) (err error) {
if !c.Args().Present() {
Expand Down
42 changes: 42 additions & 0 deletions core/cmd/bridge_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd_test
import (
"bytes"
"flag"
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -191,3 +192,44 @@ func TestShell_RemoveBridge(t *testing.T) {
assert.Equal(t, bt.URL.String(), p.URL)
assert.Equal(t, bt.Confirmations, p.Confirmations)
}
func TestShell_UpdateBridge(t *testing.T) {
t.Parallel()

app := startNewApplicationV2(t, nil)
client, _ := app.NewShellAndRenderer()
name := testutils.RandomizeName("updatebridge")

bt := &bridges.BridgeType{
Name: bridges.MustParseBridgeName(name),
URL: cltest.WebURL(t, "https://testing.com/bridges"),
Confirmations: 0,
}
require.NoError(t, app.BridgeORM().CreateBridgeType(testutils.Context(t), bt))
tests := []struct {
name string
args []string
errored bool
}{
{"NoArgs", []string{}, true},
{"OnlyName", []string{name}, true},
{"ValidUpdate", []string{name, fmt.Sprintf(`{ "name": "%s", "url": "http://localhost:3000/updated" }`, name)}, false},
{"InvalidJSON", []string{name, `{ "url": "http://localhost:3000/updated"`}, true},
}

for _, tt := range tests {
test := tt
t.Run(test.name, func(t *testing.T) {
set := flag.NewFlagSet("bridge", 0)
flagSetApplyFromAction(client.UpdateBridge, set, "")

require.NoError(t, set.Parse(test.args))

c := cli.NewContext(nil, set, nil)
if test.errored {
assert.Error(t, client.UpdateBridge(c))
} else {
assert.Nil(t, client.UpdateBridge(c))
}
})
}
}

0 comments on commit f24a153

Please sign in to comment.