Skip to content

Commit

Permalink
fix: Check and fail if invalid output (#1406)
Browse files Browse the repository at this point in the history
Reviewed-by: Antoine Cotten <[email protected]>
Approved-by: Antoine Cotten <[email protected]>
  • Loading branch information
craciunoiuc authored Mar 13, 2024
2 parents 0838505 + 0cd4d36 commit 1879c67
Show file tree
Hide file tree
Showing 24 changed files with 114 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/cli/kraft/cloud/certificate/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ func (opts *GetOptions) Pre(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("could not populate metro and token: %w", err)
}

if !utils.IsValidOutputFormat(opts.Output) {
return fmt.Errorf("invalid output format: %s", opts.Output)
}

return nil
}

Expand Down
4 changes: 4 additions & 0 deletions internal/cli/kraft/cloud/certificate/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ func (opts *ListOptions) Pre(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("could not populate metro and token: %w", err)
}

if !utils.IsValidOutputFormat(opts.Output) {
return fmt.Errorf("invalid output format: %s", opts.Output)
}

return nil
}

Expand Down
4 changes: 4 additions & 0 deletions internal/cli/kraft/cloud/certificate/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ func (opts *RemoveOptions) Pre(cmd *cobra.Command, args []string) error {
return fmt.Errorf("could not populate metro and token: %w", err)
}

if !utils.IsValidOutputFormat(opts.Output) {
return fmt.Errorf("invalid output format: %s", opts.Output)
}

return nil
}

Expand Down
4 changes: 4 additions & 0 deletions internal/cli/kraft/cloud/img/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ func (opts *ListOptions) Pre(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("could not populate metro and token: %w", err)
}

if !utils.IsValidOutputFormat(opts.Output) {
return fmt.Errorf("invalid output format: %s", opts.Output)
}

return nil
}

Expand Down
4 changes: 4 additions & 0 deletions internal/cli/kraft/cloud/instance/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,10 @@ func (opts *CreateOptions) Pre(cmd *cobra.Command, _ []string) error {
return errors.New("cannot use --rollout with --replicas")
}

if !utils.IsValidOutputFormat(opts.Output) {
return fmt.Errorf("invalid output format: %s", opts.Output)
}

log.G(cmd.Context()).WithField("metro", opts.Metro).Debug("using")
return nil
}
Expand Down
4 changes: 4 additions & 0 deletions internal/cli/kraft/cloud/instance/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ func (opts *GetOptions) Pre(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("could not populate metro and token: %w", err)
}

if !utils.IsValidOutputFormat(opts.Output) {
return fmt.Errorf("invalid output format: %s", opts.Output)
}

return nil
}

Expand Down
4 changes: 4 additions & 0 deletions internal/cli/kraft/cloud/instance/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ func (opts *ListOptions) Pre(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("could not populate metro and token: %w", err)
}

if !utils.IsValidOutputFormat(opts.Output) {
return fmt.Errorf("invalid output format: %s", opts.Output)
}

return nil
}

Expand Down
4 changes: 4 additions & 0 deletions internal/cli/kraft/cloud/instance/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ func (opts *RemoveOptions) Pre(cmd *cobra.Command, args []string) error {
return fmt.Errorf("could not populate metro and token: %w", err)
}

if !utils.IsValidOutputFormat(opts.Output) {
return fmt.Errorf("invalid output format: %s", opts.Output)
}

return nil
}

Expand Down
4 changes: 4 additions & 0 deletions internal/cli/kraft/cloud/instance/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ func (opts *StartOptions) Pre(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("could not populate metro and token: %w", err)
}

if !utils.IsValidOutputFormat(opts.Output) {
return fmt.Errorf("invalid output format: %s", opts.Output)
}

return nil
}

Expand Down
4 changes: 4 additions & 0 deletions internal/cli/kraft/cloud/instance/stop/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ func (opts *StopOptions) Pre(cmd *cobra.Command, args []string) error {
return fmt.Errorf("could not populate metro and token: %w", err)
}

if !utils.IsValidOutputFormat(opts.Output) {
return fmt.Errorf("invalid output format: %s", opts.Output)
}

return nil
}

Expand Down
5 changes: 5 additions & 0 deletions internal/cli/kraft/cloud/metros/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
kraftcloud "sdk.kraft.cloud"

"kraftkit.sh/cmdfactory"
"kraftkit.sh/internal/cli/kraft/cloud/utils"
"kraftkit.sh/internal/tableprinter"
"kraftkit.sh/iostreams"
"kraftkit.sh/log"
Expand Down Expand Up @@ -57,6 +58,10 @@ func NewCmd() *cobra.Command {
}

func (opts *ListOptions) Pre(cmd *cobra.Command, _ []string) error {
if !utils.IsValidOutputFormat(opts.Output) {
return fmt.Errorf("invalid output format: %s", opts.Output)
}

return nil
}

Expand Down
4 changes: 4 additions & 0 deletions internal/cli/kraft/cloud/quotas/quotas.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ func (opts *QuotasOptions) Pre(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("could not populate metro and token: %w", err)
}

if !utils.IsValidOutputFormat(opts.Output) {
return fmt.Errorf("invalid output format: %s", opts.Output)
}

return nil
}

Expand Down
4 changes: 4 additions & 0 deletions internal/cli/kraft/cloud/service/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ func (opts *CreateOptions) Pre(cmd *cobra.Command, _ []string) error {
opts.FQDN = domain
}

if !utils.IsValidOutputFormat(opts.Output) {
return fmt.Errorf("invalid output format: %s", opts.Output)
}

return nil
}

Expand Down
4 changes: 4 additions & 0 deletions internal/cli/kraft/cloud/service/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ func (opts *GetOptions) Pre(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("could not populate metro and token: %w", err)
}

if !utils.IsValidOutputFormat(opts.Output) {
return fmt.Errorf("invalid output format: %s", opts.Output)
}

return nil
}

Expand Down
4 changes: 4 additions & 0 deletions internal/cli/kraft/cloud/service/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ func (opts *ListOptions) Pre(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("could not populate metro and token: %w", err)
}

if !utils.IsValidOutputFormat(opts.Output) {
return fmt.Errorf("invalid output format: %s", opts.Output)
}

return nil
}

Expand Down
8 changes: 8 additions & 0 deletions internal/cli/kraft/cloud/utils/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -800,3 +800,11 @@ func printJSON(ctx context.Context, data any) error {
fmt.Fprintln(iostreams.G(ctx).Out, string(b))
return nil
}

func IsValidOutputFormat(format string) bool {
return format == "json" ||
format == "table" ||
format == "yaml" ||
format == "list" ||
format == ""
}
4 changes: 4 additions & 0 deletions internal/cli/kraft/cloud/volume/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ func (opts *GetOptions) Pre(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("could not populate metro and token: %w", err)
}

if !utils.IsValidOutputFormat(opts.Output) {
return fmt.Errorf("invalid output format: %s", opts.Output)
}

return nil
}

Expand Down
4 changes: 4 additions & 0 deletions internal/cli/kraft/cloud/volume/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func (opts *ListOptions) Pre(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("could not populate metro and token: %w", err)
}

if !utils.IsValidOutputFormat(opts.Output) {
return fmt.Errorf("invalid output format: %s", opts.Output)
}

return nil
}

Expand Down
6 changes: 6 additions & 0 deletions internal/cli/kraft/net/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

networkapi "kraftkit.sh/api/network/v1alpha1"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/internal/cli/kraft/cloud/utils"
"kraftkit.sh/internal/tableprinter"
"kraftkit.sh/iostreams"
"kraftkit.sh/log"
Expand Down Expand Up @@ -58,6 +59,11 @@ func NewCmd() *cobra.Command {

func (opts *ListOptions) Pre(cmd *cobra.Command, _ []string) error {
opts.Driver = cmd.Flag("driver").Value.String()

if !utils.IsValidOutputFormat(opts.Output) {
return fmt.Errorf("invalid output format: %s", opts.Output)
}

return nil
}

Expand Down
9 changes: 9 additions & 0 deletions internal/cli/kraft/pkg/info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/spf13/cobra"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/config"
"kraftkit.sh/internal/cli/kraft/cloud/utils"
pkgutils "kraftkit.sh/internal/cli/kraft/pkg/utils"
"kraftkit.sh/iostreams"
"kraftkit.sh/log"
Expand Down Expand Up @@ -59,6 +60,14 @@ func New() *cobra.Command {
return cmd
}

func (opts *InfoOptions) Pre(cmd *cobra.Command, _ []string) error {
if !utils.IsValidOutputFormat(opts.Output) {
return fmt.Errorf("invalid output format: %s", opts.Output)
}

return nil
}

func (opts *InfoOptions) Run(ctx context.Context, args []string) error {
ctx, err := packmanager.WithDefaultUmbrellaManagerInContext(ctx)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions internal/cli/kraft/pkg/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"kraftkit.sh/unikraft/app"

"kraftkit.sh/cmdfactory"
"kraftkit.sh/internal/cli/kraft/cloud/utils"
pkgutils "kraftkit.sh/internal/cli/kraft/pkg/utils"
"kraftkit.sh/iostreams"
"kraftkit.sh/log"
Expand Down Expand Up @@ -83,6 +84,10 @@ func (opts *ListOptions) Pre(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("cannot use --local and --remote")
}

if !utils.IsValidOutputFormat(opts.Output) {
return fmt.Errorf("invalid output format: %s", opts.Output)
}

cmd.SetContext(ctx)

return nil
Expand Down
6 changes: 6 additions & 0 deletions internal/cli/kraft/ps/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
machineapi "kraftkit.sh/api/machine/v1alpha1"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/config"
"kraftkit.sh/internal/cli/kraft/cloud/utils"
"kraftkit.sh/internal/tableprinter"
"kraftkit.sh/iostreams"
"kraftkit.sh/log"
Expand Down Expand Up @@ -75,6 +76,11 @@ func NewCmd() *cobra.Command {

func (opts *PsOptions) Pre(cmd *cobra.Command, _ []string) error {
opts.platform = cmd.Flag("plat").Value.String()

if !utils.IsValidOutputFormat(opts.Output) {
return fmt.Errorf("invalid output format: %s", opts.Output)
}

return nil
}

Expand Down
6 changes: 6 additions & 0 deletions internal/cli/kraft/x/probe/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ package probe

import (
"context"
"fmt"

"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"

"kraftkit.sh/cmdfactory"
"kraftkit.sh/internal/cli/kraft/cloud/utils"
"kraftkit.sh/internal/tableprinter"
"kraftkit.sh/iostreams"
"kraftkit.sh/log"
Expand Down Expand Up @@ -55,6 +57,10 @@ func (opts *Probe) Pre(cmd *cobra.Command, _ []string) error {
return err
}

if !utils.IsValidOutputFormat(opts.Output) {
return fmt.Errorf("invalid output format: %s", opts.Output)
}

cmd.SetContext(ctx)

return nil
Expand Down
5 changes: 5 additions & 0 deletions internal/cli/runu/ps/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/spf13/cobra"

"kraftkit.sh/cmdfactory"
"kraftkit.sh/internal/cli/kraft/cloud/utils"
libcontainer "kraftkit.sh/libmocktainer"
"kraftkit.sh/log"
)
Expand Down Expand Up @@ -59,6 +60,10 @@ func (opts *PsOptions) Pre(cmd *cobra.Command, args []string) error {
return fmt.Errorf("state directory (--%s flag) is not set", flagRoot)
}

if !utils.IsValidOutputFormat(opts.Format) {
return fmt.Errorf("invalid output format: %s", opts.Format)
}

return nil
}

Expand Down

0 comments on commit 1879c67

Please sign in to comment.