Skip to content

Commit

Permalink
Add text output to CLI (#133)
Browse files Browse the repository at this point in the history
use text output if output is unspecified and either `CI` environment variable is set or command is being piped
  • Loading branch information
kitemongerer authored Nov 20, 2024
1 parent fac7858 commit 3fdbcde
Show file tree
Hide file tree
Showing 20 changed files with 275 additions and 104 deletions.
28 changes: 20 additions & 8 deletions cmd/deploycancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/spf13/cobra"

"github.com/renderinc/cli/pkg/command"
"github.com/renderinc/cli/pkg/text"
"github.com/renderinc/cli/pkg/tui/views"
)

Expand All @@ -29,15 +30,14 @@ func init() {
return err
}

if nonInteractive, err := command.NonInteractive(
nonInteractive, err := command.NonInteractiveWithConfirm(
cmd,
func() (any, error) {
return views.CancelDeploy(cmd.Context(), input)
},
func() (string, error) {
return views.RequireConfirmationForCancelDeploy(cmd.Context(), input)
},
); err != nil {
cancelDeploy(cmd.Context(), input),
text.FormatString,
confirmDeploy(cmd.Context(), input),
)

if err != nil {
return err
} else if nonInteractive {
return nil
Expand All @@ -47,3 +47,15 @@ func init() {
return nil
}
}

func cancelDeploy(ctx context.Context, input views.DeployCancelInput) func() (string, error) {
return func() (string, error) {
return views.CancelDeploy(ctx, input)
}
}

func confirmDeploy(ctx context.Context, input views.DeployCancelInput) func() (string, error) {
return func() (string, error) {
return views.RequireConfirmationForCancelDeploy(ctx, input)
}
}
13 changes: 6 additions & 7 deletions cmd/deploycreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/renderinc/cli/pkg/client"
"github.com/renderinc/cli/pkg/command"
"github.com/renderinc/cli/pkg/resource"
"github.com/renderinc/cli/pkg/text"
"github.com/renderinc/cli/pkg/tui/views"
"github.com/renderinc/cli/pkg/types"
)
Expand Down Expand Up @@ -45,13 +46,11 @@ func init() {
return fmt.Errorf("failed to parse command: %w", err)
}

if nonInteractive, err := command.NonInteractive(
cmd,
func() (any, error) {
return views.CreateDeploy(cmd.Context(), input)
},
views.DeployCreateConfirm(cmd.Context(), input),
); err != nil {
if nonInteractive, err := command.NonInteractiveWithConfirm(cmd, func() (*client.Deploy, error) {
return views.CreateDeploy(cmd.Context(), input)
}, func(deploy *client.Deploy) string {
return text.FormatStringF("Created deploy %s for service %s", deploy.Id, input.ServiceID)
}, views.DeployCreateConfirm(cmd.Context(), input)); err != nil {
return err
} else if nonInteractive {
return nil
Expand Down
16 changes: 7 additions & 9 deletions cmd/deploylist.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"context"

tea "github.com/charmbracelet/bubbletea"
"github.com/spf13/cobra"

"github.com/renderinc/cli/pkg/client"
"github.com/renderinc/cli/pkg/deploy"
"github.com/renderinc/cli/pkg/pointers"
"github.com/spf13/cobra"
"github.com/renderinc/cli/pkg/text"

"github.com/renderinc/cli/pkg/command"
"github.com/renderinc/cli/pkg/resource"
Expand Down Expand Up @@ -52,7 +54,7 @@ func commandsForDeploy(dep *client.Deploy, serviceID string) []views.PaletteComm
ResourceIDs: []string{serviceID},
StartTime: startTime,
EndTime: endTime,
Direction: "forward",
Direction: "forward",
},
"Logs",
)
Expand Down Expand Up @@ -85,13 +87,9 @@ func init() {

input := views.DeployListInput{ServiceID: serviceID}

if nonInteractive, err := command.NonInteractive(
cmd,
func() (any, error) {
return views.LoadDeployList(cmd.Context(), input)
},
nil,
); err != nil {
if nonInteractive, err := command.NonInteractive(cmd, func() ([]*client.Deploy, error) {
return views.LoadDeployList(cmd.Context(), input)
}, text.DeployTable); err != nil {
return err
} else if nonInteractive {
return nil
Expand Down
11 changes: 4 additions & 7 deletions cmd/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/renderinc/cli/pkg/client"
"github.com/renderinc/cli/pkg/command"
"github.com/renderinc/cli/pkg/project"
"github.com/renderinc/cli/pkg/text"
"github.com/renderinc/cli/pkg/tui"
"github.com/renderinc/cli/pkg/tui/views"
)
Expand Down Expand Up @@ -45,13 +46,9 @@ func init() {
return err
}

if nonInteractive, err := command.NonInteractive(
cmd,
func() (any, error) {
return views.LoadEnvironments(cmd.Context(), input)
},
nil,
); err != nil {
if nonInteractive, err := command.NonInteractive(cmd, func() ([]*client.Environment, error) {
return views.LoadEnvironments(cmd.Context(), input)
}, text.EnvironmentTable); err != nil {
return err
} else if nonInteractive {
return nil
Expand Down
24 changes: 17 additions & 7 deletions cmd/jobcancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/spf13/cobra"

"github.com/renderinc/cli/pkg/command"
"github.com/renderinc/cli/pkg/text"
"github.com/renderinc/cli/pkg/tui/views"
)

Expand All @@ -28,14 +29,11 @@ func init() {
return err
}

if nonInteractive, err := command.NonInteractive(
if nonInteractive, err := command.NonInteractiveWithConfirm(
cmd,
func() (any, error) {
return views.CancelJob(cmd.Context(), input)
},
func() (string, error) {
return views.RequireConfirmationForCancelJob(cmd.Context(), input)
},
cancelJob(cmd, input),
text.FormatString,
confirmJobCancel(cmd, input),
); err != nil {
return err
} else if nonInteractive {
Expand All @@ -46,3 +44,15 @@ func init() {
return nil
}
}

func cancelJob(cmd *cobra.Command, input views.JobCancelInput) func() (string, error) {
return func() (string, error) {
return views.CancelJob(cmd.Context(), input)
}
}

func confirmJobCancel(cmd *cobra.Command, input views.JobCancelInput) func() (string, error) {
return func() (string, error) {
return views.RequireConfirmationForCancelJob(cmd.Context(), input)
}
}
13 changes: 6 additions & 7 deletions cmd/jobcreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
clientjob "github.com/renderinc/cli/pkg/client/jobs"
"github.com/renderinc/cli/pkg/command"
"github.com/renderinc/cli/pkg/resource"
"github.com/renderinc/cli/pkg/text"
"github.com/renderinc/cli/pkg/tui/views"
)

Expand Down Expand Up @@ -43,13 +44,11 @@ func init() {
return fmt.Errorf("failed to parse command: %w", err)
}

if nonInteractive, err := command.NonInteractive(
cmd,
func() (any, error) {
return views.CreateJob(cmd.Context(), input)
},
nil,
); err != nil {
if nonInteractive, err := command.NonInteractive(cmd, func() (*clientjob.Job, error) {
return views.CreateJob(cmd.Context(), input)
}, func(j *clientjob.Job) string {
return text.FormatStringF("Created job %s for %s", j.Id, input.ServiceID)
}); err != nil {
return err
} else if nonInteractive {
return nil
Expand Down
11 changes: 4 additions & 7 deletions cmd/joblist.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/renderinc/cli/pkg/job"
"github.com/renderinc/cli/pkg/pointers"
"github.com/renderinc/cli/pkg/resource"
"github.com/renderinc/cli/pkg/text"
"github.com/renderinc/cli/pkg/tui/views"
)

Expand Down Expand Up @@ -97,13 +98,9 @@ func init() {
return fmt.Errorf("failed to parse command: %w", err)
}

if nonInteractive, err := command.NonInteractive(
cmd,
func() (any, error) {
return views.LoadJobListData(cmd.Context(), input)
},
nil,
); err != nil {
if nonInteractive, err := command.NonInteractive(cmd, func() ([]*clientjob.Job, error) {
return views.LoadJobListData(cmd.Context(), input)
}, text.JobTable); err != nil {
return err
} else if nonInteractive {
return nil
Expand Down
6 changes: 5 additions & 1 deletion cmd/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package cmd
import (
"context"
"encoding/json"
"fmt"
"io"
"time"

tea "github.com/charmbracelet/bubbletea"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -40,6 +42,8 @@ func writeLog(format command.Output, out io.Writer, log *lclient.Log) error {
str, err = json.MarshalIndent(log, "", " ")
} else if format == command.YAML {
str, err = yaml.Marshal(log)
} else if format == command.TEXT {
str = []byte(fmt.Sprintf("%s %s\n", log.Timestamp.Format(time.DateTime), log.Message))
}

if err != nil {
Expand Down Expand Up @@ -121,7 +125,7 @@ func init() {
}

format := command.GetFormatFromContext(cmd.Context())
if format != nil && (*format == command.JSON || *format == command.YAML) {
if format != nil && (*format != command.Interactive) {
return nonInteractiveLogs(format, cmd, input)
}

Expand Down
11 changes: 4 additions & 7 deletions cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/renderinc/cli/pkg/client"
"github.com/renderinc/cli/pkg/command"
"github.com/renderinc/cli/pkg/text"
"github.com/renderinc/cli/pkg/tui"
"github.com/renderinc/cli/pkg/tui/views"
)
Expand Down Expand Up @@ -42,13 +43,9 @@ func init() {
rootCmd.AddCommand(projectCmd)

projectCmd.RunE = func(cmd *cobra.Command, args []string) error {
if nonInteractive, err := command.NonInteractive(
cmd,
func() (any, error) {
return views.LoadProjects(cmd.Context(), views.ProjectInput{})
},
nil,
); err != nil {
if nonInteractive, err := command.NonInteractive(cmd, func() ([]*client.Project, error) {
return views.LoadProjects(cmd.Context(), views.ProjectInput{})
}, text.ProjectTable); err != nil {
return err
} else if nonInteractive {
return nil
Expand Down
11 changes: 4 additions & 7 deletions cmd/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/renderinc/cli/pkg/command"
"github.com/renderinc/cli/pkg/resource"
"github.com/renderinc/cli/pkg/text"
"github.com/renderinc/cli/pkg/tui/views"
)

Expand All @@ -32,13 +33,9 @@ func init() {
return err
}

if nonInteractive, err := command.NonInteractive(
cmd,
func() (any, error) {
return views.RestartResource(cmd.Context(), input)
},
nil,
); err != nil {
if nonInteractive, err := command.NonInteractive(cmd, func() (string, error) {
return views.RestartResource(cmd.Context(), input)
}, text.FormatString); err != nil {
return err
} else if nonInteractive {
return nil
Expand Down
14 changes: 9 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ var rootCmd = &cobra.Command{
println(err.Error())
os.Exit(1)
}
if output == command.Interactive && (isPipe() || isCI()) {
output = command.TEXT
}
ctx = command.SetFormatInContext(ctx, &output)

if output == command.Interactive {
Expand All @@ -85,10 +88,6 @@ var rootCmd = &cobra.Command{

output := command.GetFormatFromContext(ctx)
if output == nil || *output == command.Interactive {
if isPipe() {
return errors.New("please specify `-o json` or `-o yaml` to pipe output")
}

stack := tui.GetStackFromContext(ctx)
if stack == nil {
return nil
Expand Down Expand Up @@ -128,6 +127,11 @@ func isPipe() bool {
return !isTerminal
}

func isCI() bool {
ci := os.Getenv("CI")
return ci == "true" || ci == "1"
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
Expand All @@ -145,7 +149,7 @@ func init() {

rootCmd.Version = cfg.Version
rootCmd.CompletionOptions.DisableDefaultCmd = true
rootCmd.PersistentFlags().StringP("output", "o", "interactive", "interactive, json, or yaml")
rootCmd.PersistentFlags().StringP("output", "o", "interactive", "interactive, json, yaml, or text")
rootCmd.PersistentFlags().Bool(command.ConfirmFlag, false, "set to skip confirmation prompts")

// Flags from the old CLI that we error with a helpful message
Expand Down
11 changes: 4 additions & 7 deletions cmd/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/renderinc/cli/pkg/redis"
"github.com/renderinc/cli/pkg/resource"
"github.com/renderinc/cli/pkg/service"
"github.com/renderinc/cli/pkg/text"
"github.com/renderinc/cli/pkg/tui"
"github.com/renderinc/cli/pkg/tui/views"
"github.com/renderinc/cli/pkg/types"
Expand Down Expand Up @@ -205,13 +206,9 @@ func init() {
return err
}

if nonInteractive, err := command.NonInteractive(
cmd,
func() (any, error) {
return views.LoadResourceData(cmd.Context(), in)
},
nil,
); err != nil {
if nonInteractive, err := command.NonInteractive(cmd, func() ([]resource.Resource, error) {
return views.LoadResourceData(cmd.Context(), in)
}, text.ResourceTable); err != nil {
return err
} else if nonInteractive {
return nil
Expand Down
Loading

0 comments on commit 3fdbcde

Please sign in to comment.