Skip to content

Commit

Permalink
Minor improvements for env subcommand group (#689)
Browse files Browse the repository at this point in the history
1. The snapshot listing now matches the extension.
2. Proper error handling when session uses simple map env store.
  • Loading branch information
sourishkrout authored Oct 21, 2024
1 parent 171fd75 commit 6f18ca0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
33 changes: 19 additions & 14 deletions internal/cmd/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"
"time"

"github.com/cli/cli/v2/pkg/iostreams"
"github.com/cli/go-gh/pkg/tableprinter"
"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand All @@ -18,6 +17,7 @@ import (
"github.com/stateful/runme/v3/internal/command"
"github.com/stateful/runme/v3/internal/owl"
"github.com/stateful/runme/v3/internal/runner/client"
"github.com/stateful/runme/v3/internal/term"
runmetls "github.com/stateful/runme/v3/internal/tls"
runnerv1 "github.com/stateful/runme/v3/pkg/api/gen/proto/go/runme/runner/v1"
)
Expand Down Expand Up @@ -117,7 +117,7 @@ func storeSnapshotCmd() *cobra.Command {
}

if msgData, ok := msg.Data.(*runnerv1.MonitorEnvStoreResponse_Snapshot); ok {
return errors.Wrap(printStore(msgData, limit, all), "failed to render")
return errors.Wrap(printStore(cmd, msgData, limit, all), "failed to render")
}

return nil
Expand Down Expand Up @@ -223,14 +223,18 @@ func environmentDumpCmd() *cobra.Command {
return &cmd
}

func printStore(msgData *runnerv1.MonitorEnvStoreResponse_Snapshot, lines int, all bool) error {
io := iostreams.System()
func printStore(cmd *cobra.Command, msgData *runnerv1.MonitorEnvStoreResponse_Snapshot, lines int, all bool) error {
term := term.FromIO(cmd.InOrStdin(), cmd.OutOrStdout(), cmd.ErrOrStderr())

// TODO: Clear terminal screen
width, _, err := term.Size()
if err != nil {
width = 80
}

table := tableprinter.New(io.Out, io.IsStdoutTTY(), io.TerminalWidth())
table := tableprinter.New(term.Out(), term.IsTTY(), width)
table.AddField(strings.ToUpper("Name"))
table.AddField(strings.ToUpper("Value"))
table.AddField(strings.ToUpper("Description"))
table.AddField(strings.ToUpper("Spec"))
table.AddField(strings.ToUpper("Origin"))
table.AddField(strings.ToUpper("Updated"))
Expand All @@ -246,9 +250,9 @@ func printStore(msgData *runnerv1.MonitorEnvStoreResponse_Snapshot, lines int, a
break
}

value := env.ResolvedValue
value := env.GetResolvedValue()

switch env.Status {
switch env.GetStatus() {
case runnerv1.MonitorEnvStoreResponseSnapshot_STATUS_UNSPECIFIED:
value = "[unset]"
case runnerv1.MonitorEnvStoreResponseSnapshot_STATUS_MASKED:
Expand All @@ -257,13 +261,14 @@ func printStore(msgData *runnerv1.MonitorEnvStoreResponse_Snapshot, lines int, a
value = "******"
}

table.AddField(env.Name)
stripped := strings.ReplaceAll(strings.ReplaceAll(value, "\n", " "), "\r", "")
table.AddField(stripped)
table.AddField(env.Spec)
table.AddField(env.Origin)
table.AddField(env.GetName())
table.AddField(env.GetDescription())
strippedVal := strings.ReplaceAll(strings.ReplaceAll(value, "\n", " "), "\r", "")
table.AddField(strippedVal)
table.AddField(env.GetSpec())
table.AddField(env.GetOrigin())

t, err := time.Parse(time.RFC3339, env.UpdateTime)
t, err := time.Parse(time.RFC3339, env.GetUpdateTime())
if err == nil {
table.AddField(t.Format(time.DateTime))
} else {
Expand Down
14 changes: 11 additions & 3 deletions internal/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/spf13/cobra"

"github.com/stateful/runme/v3/internal/shell"
"github.com/stateful/runme/v3/internal/term"
"github.com/stateful/runme/v3/pkg/project"
)

Expand Down Expand Up @@ -71,7 +72,7 @@ func listCmd() *cobra.Command {
rows = append(rows, r)
}
if !formatJSON {
err := displayTable(io, rows)
err := displayTable(cmd, rows)

if !io.IsStderrTTY() {
return err
Expand All @@ -91,8 +92,15 @@ func listCmd() *cobra.Command {
return &cmd
}

func displayTable(io *iostreams.IOStreams, rows []row) error {
table := tableprinter.New(io.Out, io.IsStdoutTTY(), io.TerminalWidth())
func displayTable(cmd *cobra.Command, rows []row) error {
term := term.FromIO(cmd.InOrStdin(), cmd.OutOrStdout(), cmd.ErrOrStderr())

// Detect width. For non-TTY, use a default width of 80.
width, _, err := term.Size()
if err != nil {
width = 80
}
table := tableprinter.New(term.Out(), term.IsTTY(), width)

// table header
table.AddField(strings.ToUpper("Name"))
Expand Down
3 changes: 2 additions & 1 deletion internal/runner/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -862,9 +862,10 @@ func (r *runnerService) MonitorEnvStore(req *runnerv1.MonitorEnvStoreRequest, sr
}
}()

err := <-errc
wg.Wait()

return <-errc
return err
}

func convertToMonitorEnvStoreResponse(msg *runnerv1.MonitorEnvStoreResponse, snapshot owl.SetVarItems) error {
Expand Down

0 comments on commit 6f18ca0

Please sign in to comment.