Skip to content

Commit

Permalink
Display managedBy on tsuru env get
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Jun 12, 2024
1 parent 6edf43c commit 99a7826
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
32 changes: 21 additions & 11 deletions tsuru/client/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/tsuru/go-tsuruclient/pkg/config"
"github.com/tsuru/tsuru-client/tsuru/formatter"
tsuruHTTP "github.com/tsuru/tsuru-client/tsuru/http"
tsuruAPIApp "github.com/tsuru/tsuru/app"
"github.com/tsuru/tsuru/cmd"
apiTypes "github.com/tsuru/tsuru/types/api"
)
Expand Down Expand Up @@ -96,10 +95,18 @@ func (c *EnvGet) Run(context *cmd.Context) error {

formatted := make([]string, 0, len(variables))
for _, v := range variables {
value := tsuruAPIApp.SuppressedEnv
if v["public"].(bool) {
value = v["value"].(string)
value := v["value"].(string)
public := v["public"].(bool)
managedBy, _ := v["managedBy"].(string)

if public && managedBy != "" {
value = fmt.Sprintf("%s (managed by %s)", value, managedBy)
} else if !public && managedBy != "" {
value = fmt.Sprintf("*** (private variable managed by %s)", managedBy)
} else if !public {
value = "*** (private variable)"
}

formatted = append(formatted, fmt.Sprintf("%s=%s", v["name"], value))
}
sort.Strings(formatted)
Expand All @@ -109,24 +116,27 @@ func (c *EnvGet) Run(context *cmd.Context) error {

func (c *EnvGet) renderJSON(context *cmd.Context, variables []map[string]interface{}) error {
type envJSON struct {
Name string `json:"name"`
Value string `json:"value"`
Private bool `json:"private"`
Name string `json:"name"`
Value string `json:"value"`
Private bool `json:"private"`
ManagedBy string `json:"managedBy,omitempty"`
}

data := make([]envJSON, 0, len(variables))

for _, v := range variables {
private := true
value := tsuruAPIApp.SuppressedEnv
value := "*** (private variable)"
if v["public"].(bool) {
value = v["value"].(string)
private = false
}
managedBy, _ := v["managedBy"].(string)
data = append(data, envJSON{
Name: v["name"].(string),
Value: value,
Private: private,
Name: v["name"].(string),
Value: value,
Private: private,
ManagedBy: managedBy,
})
}

Expand Down
18 changes: 18 additions & 0 deletions tsuru/client/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,24 @@ func (s *S) TestEnvGetPrivateVariables(c *check.C) {
c.Assert(stdout.String(), check.Equals, result)
}

func (s *S) TestEnvGetManagedByVariables(c *check.C) {
var stdout, stderr bytes.Buffer
jsonResult := `[{"name": "DATABASE_USER", "value": "someuser", "public": false, "managedBy": "my-service/instance"}, {"name": "DATABASE_HOST", "value": "somehost", "public": true, "managedBy": "my-service/instance"}]`
result := "DATABASE_HOST=somehost (managed by my-service/instance)\nDATABASE_USER=*** (private variable managed by my-service/instance)\n"
params := []string{"DATABASE_HOST", "DATABASE_USER"}
context := cmd.Context{
Args: params,
Stdout: &stdout,
Stderr: &stderr,
}
s.setupFakeTransport(&cmdtest.Transport{Message: jsonResult, Status: http.StatusOK})
command := EnvGet{}
command.Flags().Parse(true, []string{"-a", "someapp"})
err := command.Run(&context)
c.Assert(err, check.IsNil)
c.Assert(stdout.String(), check.Equals, result)
}

func (s *S) TestEnvGetWithoutTheFlag(c *check.C) {
var stdout, stderr bytes.Buffer
jsonResult := `[{"name": "DATABASE_HOST", "value": "somehost", "public": true}, {"name": "DATABASE_USER", "value": "someuser", "public": true}]`
Expand Down

0 comments on commit 99a7826

Please sign in to comment.