Skip to content

Commit f4eb49c

Browse files
Merge pull request #349 from MattAWright/colors
Colors
2 parents a5cebca + 3cd0572 commit f4eb49c

File tree

5 files changed

+37
-85
lines changed

5 files changed

+37
-85
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ _2017-12-22_
88

99
- Redesigned the output of `view` and removed the `--format` flag.
1010
- Updated formatting and flags of `envs list`, `services list`, `invites list`,
11-
`machines list`, and `machines view`.
11+
`machines list`, `machines view`, `policies list` and `policies view`.
1212
- Added styling, color and org/project prompts throughout commands. Colors can
1313
be disable by running `torus prefs set core.colors false`.
1414

cmd/list.go

Lines changed: 16 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"os"
7+
"strconv"
78
"sync"
89

910
"github.com/urfave/cli"
@@ -59,61 +60,14 @@ func listCmd(ctx *cli.Context) error {
5960

6061
tree := make(credentialTree)
6162

62-
// Retrieve org and project flag values
63-
orgName := ctx.String("org")
64-
var org *envelope.Org
65-
if orgName == "" {
66-
// Retrieve list of available orgs
67-
orgs, err := client.Orgs.List(c)
68-
if err != nil {
69-
return errs.NewExitError("Failed to retrieve orgs list.")
70-
}
71-
72-
// Prompt user to select from list of existing orgs
73-
idx, _, err := SelectExistingOrgPrompt(orgs)
74-
if err != nil {
75-
return errs.NewExitError("Failed to select org.")
76-
}
77-
78-
org = &orgs[idx]
79-
orgName = org.Body.Name
80-
81-
} else {
82-
// If org flag was used, identify the org supplied.
83-
org, err = client.Orgs.GetByName(c, orgName)
84-
if err != nil {
85-
return errs.NewErrorExitError("Failed to retrieve org " + orgName, err)
86-
}
87-
if org == nil {
88-
return errs.NewExitError("org " + orgName + " not found.")
89-
}
63+
org, err := getOrgWithPrompt(client, c, ctx.String("org"))
64+
if err != nil {
65+
return err
9066
}
9167

92-
projectName := ctx.String("project")
93-
var project *envelope.Project
94-
if projectName == "" {
95-
// Retrieve list of available projects
96-
projects, err := client.Projects.List(c, org.ID)
97-
if err != nil {
98-
return errs.NewErrorExitError("Failed to retrieve projects list.", err)
99-
}
100-
101-
// Prompt user to select from list of existing orgs
102-
idx, _, err := SelectExistingProjectPrompt(projects)
103-
if err != nil {
104-
return errs.NewErrorExitError("Failed to select project.", err)
105-
}
106-
107-
project = &projects[idx]
108-
projectName = project.Body.Name
109-
110-
} else {
111-
// Get Project for project name, confirm project exists
112-
project, err = getProject(c, client, org.ID, projectName)
113-
if err != nil {
114-
return errs.NewErrorExitError("Failed to get project info for "+
115-
orgName+"/"+projectName, err)
116-
}
68+
project, err := getProjectWithPrompt(client, c, org, ctx.String("project"))
69+
if err != nil {
70+
return err
11771
}
11872

11973
// Retrieve environment flag values
@@ -138,8 +92,8 @@ func listCmd(ctx *cli.Context) error {
13892
// Create a PathExp based on flags. This is the search space that
13993
// will be used to retrieve credentials.
14094
filterPathExp, err := pathexp.New(
141-
orgName,
142-
projectName,
95+
org.Body.Name,
96+
project.Body.Name,
14397
envFilters,
14498
serviceFilters,
14599
instanceFilters,
@@ -226,7 +180,8 @@ func listCmd(ctx *cli.Context) error {
226180
// Check for each env and service in the filtered list, add
227181
// any credentials along that path to that env/service branch
228182
// of the credentialsTree
229-
projectPath := "/" + orgName + "/" + projectName + "/"
183+
credCount := 0
184+
projectPath := "/" + org.Body.Name + "/" + project.Body.Name + "/"
230185
for _, e := range filteredEnvNames {
231186
for _, s := range filteredServiceNames {
232187
builtPathExp, err := pathexp.Parse(projectPath + e + "/" + s + "/*/*")
@@ -262,8 +217,8 @@ func listCmd(ctx *cli.Context) error {
262217
if(verbose){
263218
fmt.Println("")
264219
projW := ansiterm.NewTabWriter(os.Stdout, 0, 0, 4, ' ', 0)
265-
fmt.Fprintf(projW, "Org:\t" + ui.Faint(orgName) + "\t\n")
266-
fmt.Fprintf(projW, "Project:\t" + ui.Faint(projectName) + "\t\n")
220+
fmt.Fprintf(projW, "Org:\t" + ui.Bold(org.Body.Name) + "\t\n")
221+
fmt.Fprintf(projW, "Project:\t" + ui.Bold(project.Body.Name) + "\t\n")
267222
projW.Flush()
268223
}
269224

@@ -280,6 +235,7 @@ func listCmd(ctx *cli.Context) error {
280235
continue
281236
}
282237
for c, cred := range tree[e][s] {
238+
credCount += 1
283239
if verbose {
284240
credPath := (*cred.Body).GetPathExp().String() + "/"
285241
fmt.Fprintf(w, "\t\t%s\t(%s)\t\n", c, ui.Faint(credPath+c))
@@ -290,7 +246,8 @@ func listCmd(ctx *cli.Context) error {
290246
}
291247
}
292248
w.Flush()
293-
fmt.Println("")
249+
250+
fmt.Printf("\n(%s) secrets found\n.", ui.Faint(strconv.Itoa(credCount)))
294251

295252
return nil
296253
}

cmd/policies.go

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"text/tabwriter"
1212

1313
"github.com/urfave/cli"
14+
"github.com/juju/ansiterm"
1415

1516
"github.com/manifoldco/torus-cli/api"
1617
"github.com/manifoldco/torus-cli/apitypes"
@@ -20,6 +21,7 @@ import (
2021
"github.com/manifoldco/torus-cli/identity"
2122
"github.com/manifoldco/torus-cli/pathexp"
2223
"github.com/manifoldco/torus-cli/primitive"
24+
"github.com/manifoldco/torus-cli/ui"
2325
"github.com/manifoldco/torus-cli/validate"
2426
)
2527

@@ -33,7 +35,7 @@ func init() {
3335
Name: "list",
3436
Usage: "List all policies for an organization",
3537
Flags: []cli.Flag{
36-
orgFlag("The org to show policies for", true),
38+
orgFlag("The org to show policies for", false),
3739
},
3840
Action: chain(
3941
ensureDaemon, ensureSession, loadDirPrefs, loadPrefDefaults,
@@ -45,7 +47,7 @@ func init() {
4547
Usage: "Display the contents of a policy",
4648
ArgsUsage: "<policy>",
4749
Flags: []cli.Flag{
48-
orgFlag("The org the policy belongs to", true),
50+
orgFlag("The org the policy belongs to", false),
4951
},
5052
Action: chain(
5153
ensureDaemon, ensureSession, loadDirPrefs, loadPrefDefaults,
@@ -332,14 +334,9 @@ func listPoliciesCmd(ctx *cli.Context) error {
332334
client := api.NewClient(cfg)
333335
c := context.Background()
334336

335-
// Look up the target org
336-
var org *envelope.Org
337-
org, err = client.Orgs.GetByName(c, ctx.String("org"))
337+
org, err := getOrgWithPrompt(client, c, ctx.String("org"))
338338
if err != nil {
339-
return errs.NewErrorExitError(policyListFailed, err)
340-
}
341-
if org == nil {
342-
return errs.NewExitError("Org not found.")
339+
return err
343340
}
344341

345342
var getAttachments, display sync.WaitGroup
@@ -400,9 +397,8 @@ func listPoliciesCmd(ctx *cli.Context) error {
400397

401398
display.Wait()
402399
fmt.Println("")
403-
w := tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', 0)
404-
fmt.Fprintln(w, "POLICY NAME\tTYPE\tATTACHED TO")
405-
fmt.Fprintln(w, " \t \t ")
400+
w := ansiterm.NewTabWriter(os.Stdout, 0, 0, 4, ' ', 0)
401+
fmt.Fprintf(w, "%s\t%s\t%s\n", ui.Bold("Policy Name"), ui.Bold("Type"), ui.Bold("Attached To"))
406402
for _, name := range sortedNames {
407403
teamNames := ""
408404
policy := policiesByName[name]
@@ -441,12 +437,9 @@ func viewPolicyCmd(ctx *cli.Context) error {
441437
client := api.NewClient(cfg)
442438
c := context.Background()
443439

444-
org, err := client.Orgs.GetByName(c, ctx.String("org"))
440+
org, err := getOrgWithPrompt(client, c, ctx.String("org"))
445441
if err != nil {
446-
return errs.NewErrorExitError("Unable to lookup org.", err)
447-
}
448-
if org == nil {
449-
return errs.NewExitError("Org not found.")
442+
return err
450443
}
451444

452445
policies, err := client.Policies.List(c, org.ID, policyName)
@@ -463,8 +456,8 @@ func viewPolicyCmd(ctx *cli.Context) error {
463456

464457
w := tabwriter.NewWriter(os.Stdout, 2, 0, 1, ' ', 0)
465458

466-
fmt.Fprintf(w, "Name:\t%s\n", p.Name)
467-
fmt.Fprintf(w, "Description:\t%s\n", p.Description)
459+
fmt.Fprintf(w, "%s\t%s\n", ui.Bold("Name:"), p.Name)
460+
fmt.Fprintf(w, "%s\t%s\n", ui.Bold("Description:"), p.Description)
468461
fmt.Fprintln(w, "")
469462
w.Flush()
470463

cmd/projects.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ func getProjectWithPrompt(client *api.Client, c context.Context, org *envelope.O
236236
if err != nil {
237237
return nil, err
238238
}
239+
if len(projects) < 1 {
240+
return nil, errs.NewExitError("No projects found in org " + org.Body.Name)
241+
}
239242

240243
// Prompt user to select from list of existing projects
241244
idx, _, err := SelectExistingProjectPrompt(projects)

internal/qa.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@ If you have `torus` installed, start fresh `npm uninstall -g torus-cli`
1414
- [ ] `npm install -g torus-cli` installs `torus`
1515
- [ ] `torus help` displays the help prompt after an
1616
`npm install -g torus-cli`
17-
- [ ] `torus prefs list` displays the path to your `public_key_file`
17+
- [ ] `torus prefs list` displays your preferences
1818
- [ ] `torus signup` prompts you for an verification code username, name and
1919
email, before verifying and authenticating you
2020
- [ ] A user cannot perform any writes without verifying their account
21-
- [ ] `torus status` displays your current working context
2221
- [ ] `torus logout` logs you out
2322
- [ ] `torus login` prompts you for an email and password, before
2423
authenticating you
2524
- [ ] You can login using environment variables (`TORUS_EMAIL` and
26-
`TORUS_PASSWORD`)
25+
`TORUS_PASSWORD`) through `torus status`
2726

2827
### Account
2928

@@ -37,7 +36,7 @@ If you have `torus` installed, start fresh `npm uninstall -g torus-cli`
3736

3837
- [ ] `torus teams list --org [username]` displays `owner` `admin` and
3938
`member` teams, and you are a member of each
40-
- [ ] `torus teams create [name] --org [org-name]` creates an org.
39+
- [ ] `torus teams create [name] --org [org-name]` creates a team.
4140
- [ ] `torus invites send [email] —org [org-name]` generates an access code
4241
and sends it to the user
4342
- [ ] `torus invites accept —org [org] [email] [code]` prompts a user to
@@ -46,9 +45,9 @@ If you have `torus` installed, start fresh `npm uninstall -g torus-cli`
4645
- [ ] `torus invites list —org [org]` lists the outstanding invite
4746
- [ ] `torus invites approve [email] —org [org]` approves the users invite
4847
- [ ] The user is now a member of the members team
49-
- [ ] `torus teams add [name] [username] —org [org-name]` adds the user to a
48+
- [ ] `torus teams add [username] [name] —org [org-name]` adds the user to a
5049
team
51-
- [ ] `torus teams remove [name] [username] —org [org-name]` removes the user
50+
- [ ] `torus teams remove [username] [name] —org [org-name]` removes the user
5251
from a team
5352
- [ ] Users cannot remove themselves from system-teams
5453

0 commit comments

Comments
 (0)