Skip to content

Commit

Permalink
plugin: Allow routes content with @ prefix
Browse files Browse the repository at this point in the history
This makes the command compatible with rpaasv1 plugin.
  • Loading branch information
cezarsa committed Oct 8, 2020
1 parent d33d2fa commit 6e94db7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
25 changes: 19 additions & 6 deletions cmd/plugin/rpaasv2/cmd/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"fmt"
"io"
"io/ioutil"
"os"
"strings"

"github.com/olekukonko/tablewriter"
rpaasclient "github.com/tsuru/rpaas-operator/pkg/rpaas/client"
Expand Down Expand Up @@ -205,12 +207,9 @@ func runUpdateRoute(c *cli.Context) error {
return err
}

var content []byte
if contentFile := c.Path("content"); contentFile != "" {
content, err = ioutil.ReadFile(contentFile)
if err != nil {
return err
}
content, err := fetchContentFile(c)
if err != nil {
return err
}

args := rpaasclient.UpdateRouteArgs{
Expand All @@ -228,3 +227,17 @@ func runUpdateRoute(c *cli.Context) error {
fmt.Fprintf(c.App.Writer, "Route %q updated.\n", args.Path)
return nil
}

func fetchContentFile(c *cli.Context) ([]byte, error) {
contentFile := c.Path("content")
if contentFile == "" {
return nil, nil
}
content, err := ioutil.ReadFile(contentFile)
if os.IsNotExist(err) &&
strings.HasPrefix(contentFile, "@") {
return ioutil.ReadFile(strings.TrimPrefix(contentFile, "@"))
}

return content, err
}
16 changes: 16 additions & 0 deletions cmd/plugin/rpaasv2/cmd/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,22 @@ func TestUpdateRoute(t *testing.T) {
},
},
},
{
name: "when using a custom NGINX config with @ prefixed file path",
args: []string{"./rpaasv2", "routes", "update", "-i", "my-instance", "-p", "/custom/path", "-c", "@" + configFile.Name()},
expected: "Route \"/custom/path\" updated.\n",
client: &fake.FakeClient{
FakeUpdateRoute: func(args rpaasclient.UpdateRouteArgs) (*http.Response, error) {
expected := rpaasclient.UpdateRouteArgs{
Instance: "my-instance",
Path: "/custom/path",
Content: nginxConfig,
}
assert.Equal(t, expected, args)
return nil, nil
},
},
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 6e94db7

Please sign in to comment.