Skip to content

Commit 8a8b50a

Browse files
Merge pull request #350 from manifoldco/colorize-machine-state
Colorize machine state
2 parents f4eb49c + cd91704 commit 8a8b50a

File tree

3 files changed

+45
-41
lines changed

3 files changed

+45
-41
lines changed

cmd/machines.go

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import (
1111
"text/tabwriter"
1212
"time"
1313

14+
"github.com/juju/ansiterm"
1415
"github.com/manifoldco/go-base32"
1516
"github.com/manifoldco/go-base64"
1617
"github.com/urfave/cli"
17-
"github.com/juju/ansiterm"
1818

1919
"github.com/manifoldco/torus-cli/api"
2020
"github.com/manifoldco/torus-cli/apitypes"
@@ -282,7 +282,7 @@ func viewMachineCmd(ctx *cli.Context) error {
282282

283283
// Created profile
284284
creator := profileMap[*machineBody.CreatedBy]
285-
createdBy := creator.Body.Username + " (" + creator.Body.Name + ")"
285+
createdBy := creator.Body.Name + " (" + ui.Faint(creator.Body.Username) + ")"
286286
createdOn := machineBody.Created.Format(time.RFC3339)
287287

288288
// Destroyed profile
@@ -291,7 +291,7 @@ func viewMachineCmd(ctx *cli.Context) error {
291291
if machineBody.State == primitive.MachineDestroyedState {
292292
destroyer := profileMap[*machineBody.DestroyedBy]
293293
destroyedOn = machineBody.Destroyed.Format(time.RFC3339)
294-
destroyedBy = destroyer.Body.Username + " (" + destroyer.Body.Name + ")"
294+
destroyedBy = destroyer.Body.Name + " (" + ui.Faint(destroyer.Body.Name) + ")"
295295
}
296296

297297
// Membership info
@@ -309,29 +309,24 @@ func viewMachineCmd(ctx *cli.Context) error {
309309

310310
fmt.Println("")
311311
w1 := tabwriter.NewWriter(os.Stdout, 0, 0, 8, ' ', 0)
312-
fmt.Fprintf(w1, "ID:\t%s\n", machine.ID)
313-
fmt.Fprintf(w1, "Name:\t%s\n", machineBody.Name)
314-
fmt.Fprintf(w1, "Role:\t%s\n", roleOutput)
315-
fmt.Fprintf(w1, "State:\t%s\n", machineBody.State)
316-
fmt.Fprintf(w1, "Created By:\t%s\n", createdBy)
317-
fmt.Fprintf(w1, "Created On:\t%s\n", createdOn)
318-
fmt.Fprintf(w1, "Destroyed By:\t%s\n", destroyedBy)
319-
fmt.Fprintf(w1, "Destroyed On:\t%s\n", destroyedOn)
312+
fmt.Fprintf(w1, "%s:\t%s\n", ui.Bold("ID"), machine.ID)
313+
fmt.Fprintf(w1, "%s:\t%s\n", ui.Bold("Name"), ui.Faint(machineBody.Name))
314+
fmt.Fprintf(w1, "%s:\t%s\n", ui.Bold("Role"), roleOutput)
315+
fmt.Fprintf(w1, "%s:\t%s\n", ui.Bold("State"), colorizeMachineState(machineBody.State))
316+
fmt.Fprintf(w1, "%s:\t%s\n", ui.Bold("Created By"), createdBy)
317+
fmt.Fprintf(w1, "%s:\t%s\n", ui.Bold("Created On"), createdOn)
318+
fmt.Fprintf(w1, "%s:\t%s\n", ui.Bold("Destroyed By"), destroyedBy)
319+
fmt.Fprintf(w1, "%s:\t%s\n", ui.Bold("Destroyed On"), destroyedOn)
320320
w1.Flush()
321321
fmt.Println("")
322322

323323
w2 := ansiterm.NewTabWriter(os.Stdout, 2, 0, 3, ' ', 0)
324324
fmt.Fprintf(w2, "%s\t%s\t%s\t%s\n", ui.Bold("Token ID"), ui.Bold("State"), ui.Bold("Created By"), ui.Bold("Created On"))
325325
for _, token := range machineSegment.Tokens {
326326
tokenID := token.Token.ID
327-
var state string
328-
if token.Token.Body.State == "active" {
329-
state = ui.Color(ui.Green, token.Token.Body.State)
330-
} else {
331-
state = ui.Color(ui.Red, token.Token.Body.State)
332-
}
327+
state := colorizeMachineState(token.Token.Body.State)
333328
creator := profileMap[*token.Token.Body.CreatedBy]
334-
createdBy := creator.Body.Username + " (" + creator.Body.Name + ")"
329+
createdBy := creator.Body.Name + " (" + ui.Faint(creator.Body.Username) + ")"
335330
createdOn := token.Token.Body.Created.Format(time.RFC3339)
336331
fmt.Fprintf(w2, "%s\t%s\t%s\t%s\n", tokenID, state, createdBy, createdOn)
337332
}
@@ -342,6 +337,17 @@ func viewMachineCmd(ctx *cli.Context) error {
342337
return nil
343338
}
344339

340+
func colorizeMachineState(state string) string {
341+
switch state {
342+
case "active":
343+
return ui.Color(ui.Green, state)
344+
case "destroyed":
345+
return ui.Color(ui.Red, state)
346+
default:
347+
return state
348+
}
349+
}
350+
345351
func listMachinesCmd(ctx *cli.Context) error {
346352
cfg, err := config.LoadConfig()
347353
if err != nil {
@@ -417,13 +423,9 @@ func listMachinesCmd(ctx *cli.Context) error {
417423
roleName = role.Name
418424
}
419425
}
420-
var state string
421-
if m.State == "active" {
422-
state = ui.Color(ui.Green, m.State)
423-
} else {
424-
state = ui.Color(ui.Red, m.State)
425-
}
426-
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", mID, m.Name, state, roleName, m.Created.Format(time.RFC3339))
426+
427+
state := colorizeMachineState(state)
428+
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", mID, ui.Faint(m.Name), state, roleName, m.Created.Format(time.RFC3339))
427429
}
428430
w.Flush()
429431
fmt.Println("")
@@ -650,9 +652,9 @@ func createMachine(ctx *cli.Context) error {
650652
w := tabwriter.NewWriter(os.Stdout, 2, 0, 1, ' ', 0)
651653

652654
tokenID := machine.Tokens[0].Token.ID
653-
fmt.Fprintf(w, "Machine ID:\t%s\n", machine.Machine.ID)
654-
fmt.Fprintf(w, "Machine Token ID:\t%s\n", tokenID)
655-
fmt.Fprintf(w, "Machine Token Secret:\t%s\n", tokenSecret)
655+
fmt.Fprintf(w, "%s:\t%s\n", ui.Bold("Machine ID"), machine.Machine.ID)
656+
fmt.Fprintf(w, "%s:\t%s\n", ui.Bold("Machine Token ID"), tokenID)
657+
fmt.Fprintf(w, "%s:\t%s\n", ui.Bold("Machine Token Secret"), tokenSecret)
656658

657659
w.Flush()
658660
hints.Display(hints.Allow, hints.Deny)

cmd/profile.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/manifoldco/torus-cli/apitypes"
1212
"github.com/manifoldco/torus-cli/config"
1313
"github.com/manifoldco/torus-cli/errs"
14+
"github.com/manifoldco/torus-cli/ui"
1415

1516
"github.com/urfave/cli"
1617
)
@@ -57,13 +58,13 @@ func profileView(ctx *cli.Context) error {
5758

5859
w := tabwriter.NewWriter(os.Stdout, 2, 0, 1, ' ', 0)
5960
if session.Type() == apitypes.MachineSession {
60-
fmt.Fprintf(w, "Machine ID:\t%s\n", session.ID())
61-
fmt.Fprintf(w, "Machine Token ID:\t%s\n", session.AuthID())
62-
fmt.Fprintf(w, "Machine Name:\t%s\n", session.Username())
61+
fmt.Fprintf(w, "%s:\t%s\n", ui.Bold("Machine ID"), session.ID())
62+
fmt.Fprintf(w, "%s:\t%s\n", ui.Bold("Machine Token ID"), session.AuthID())
63+
fmt.Fprintf(w, "%s:\t%s\n", ui.Bold("Machine Name"), ui.Faint(session.Username()))
6364
} else {
64-
fmt.Fprintf(w, "Name:\t%s\n", session.Name())
65-
fmt.Fprintf(w, "Email:\t%s\n", session.Email())
66-
fmt.Fprintf(w, "Username:\t%s\n", session.Username())
65+
fmt.Fprintf(w, "%s:\t%s\n", ui.Bold("Name"), session.Name())
66+
fmt.Fprintf(w, "%s:\t%s\n", ui.Bold("Username"), ui.Faint(session.Username()))
67+
fmt.Fprintf(w, "%s:\t%s\n", ui.Bold("Email"), session.Email())
6768
}
6869

6970
w.Flush()

cmd/status.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/manifoldco/torus-cli/config"
1212
"github.com/manifoldco/torus-cli/errs"
1313
"github.com/manifoldco/torus-cli/prefs"
14+
"github.com/manifoldco/torus-cli/ui"
1415

1516
"github.com/urfave/cli"
1617
)
@@ -93,17 +94,17 @@ func statusCmd(ctx *cli.Context) error {
9394
instance := ctx.String("instance")
9495

9596
w := tabwriter.NewWriter(os.Stdout, 2, 0, 1, ' ', 0)
96-
fmt.Fprintf(w, "Org:\t%s\n", org)
97-
fmt.Fprintf(w, "Project:\t%s\n", project)
98-
fmt.Fprintf(w, "Environment:\t%s\n", env)
99-
fmt.Fprintf(w, "Service:\t%s\n", service)
100-
fmt.Fprintf(w, "Identity:\t%s\n", identity)
101-
fmt.Fprintf(w, "Instance:\t%s\n", instance)
97+
fmt.Fprintf(w, "%s:\t%s\n", ui.Bold("Org"), org)
98+
fmt.Fprintf(w, "%s:\t%s\n", ui.Bold("Project"), project)
99+
fmt.Fprintf(w, "%s:\t%s\n", ui.Bold("Environment"), env)
100+
fmt.Fprintf(w, "%s:\t%s\n", ui.Bold("Service"), service)
101+
fmt.Fprintf(w, "%s:\t%s\n", ui.Bold("Identity"), ui.Faint(identity))
102+
fmt.Fprintf(w, "%s:\t%s\n", ui.Bold("Instance"), instance)
102103
w.Flush()
103104

104105
parts := []string{"", org, project, env, service, identity, instance}
105106
credPath := strings.Join(parts, "/")
106-
fmt.Printf("\nCredential path: %s\n", credPath)
107+
fmt.Printf("\n%s: %s\n", ui.Bold("Credential Path"), credPath)
107108

108109
return nil
109110
}

0 commit comments

Comments
 (0)