Skip to content

Commit

Permalink
feat: warn user based on flags
Browse files Browse the repository at this point in the history
Like volume mount, pid, privileged, etc

Signed-off-by: Yves Brissaud <[email protected]>
  • Loading branch information
eunomie committed Oct 15, 2024
1 parent 02117a9 commit d3713d0
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/dustin/go-humanize v1.0.1
github.com/gertd/go-pluralize v0.2.1
github.com/google/go-containerregistry v0.20.2
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/mattn/go-isatty v0.0.20
github.com/muesli/termenv v0.15.3-0.20240618155329-98d742f6907a
github.com/spf13/cobra v1.8.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
github.com/google/go-containerregistry v0.20.2 h1:B1wPJ1SN/S7pB+ZAimcciVD+r+yV/l/DSArMxlbwseo=
github.com/google/go-containerregistry v0.20.2/go.mod h1:z38EKdKh4h7IP2gSfUUqEvalZBqs6AoLeWfUy34nQC8=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/gorilla/css v1.0.1 h1:ntNaBIghp6JmvWnxbZKANoLyuXTPZ4cAMlo6RyhlbO8=
github.com/gorilla/css v1.0.1/go.mod h1:BvnYkspnSzMmwRK+b8/xgNPLiIuNZr6vbZBTPQ2A3b0=
github.com/gorilla/mux v1.7.0 h1:tOSd0UKHQd6urX6ApfOn4XdBMY6Sh1MfxV3kmaazO+U=
Expand Down
29 changes: 27 additions & 2 deletions internal/commands/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package root

import (
"context"
"errors"
"fmt"
"io"
"os"
"strings"

"github.com/charmbracelet/huh"
"github.com/charmbracelet/huh/spinner"
"github.com/gertd/go-pluralize"
"github.com/spf13/cobra"
Expand All @@ -19,6 +21,7 @@ import (
"github.com/eunomie/docker-runx/internal/commands/help"
"github.com/eunomie/docker-runx/internal/commands/version"
"github.com/eunomie/docker-runx/internal/constants"
"github.com/eunomie/docker-runx/internal/pizza"
"github.com/eunomie/docker-runx/internal/prompt"
"github.com/eunomie/docker-runx/internal/registry"
"github.com/eunomie/docker-runx/internal/sugar"
Expand Down Expand Up @@ -224,13 +227,35 @@ func run(ctx context.Context, out io.Writer, src string, rk *runkit.RunKit, acti
return err
}

_, _ = fmt.Fprintln(out, tui.Markdown(fmt.Sprintf(`
mdCommand := fmt.Sprintf(`
> **Running the following command:**
%s
---
`, runnable.Command)))
`, runnable.Command)

if flags, err := runnable.CheckFlags(); err != nil {
return err
} else if len(flags) > 0 {
_, _ = fmt.Fprintln(out, tui.Markdown(mdCommand+fmt.Sprintf(`
> **Some flags require your attention:**
%s
`, strings.Join(pizza.Map(flags, func(flag string) string {
return fmt.Sprintf("- `%s`", flag)
}), "\n"))))
var cont bool
err = huh.NewConfirm().Title("Continue?").Value(&cont).Run()
if err != nil {
return err
}
if !cont {
return errors.New("aborted")
}
} else {
_, _ = fmt.Fprintln(out, tui.Markdown(mdCommand))
}

return runnable.Run(ctx)
}
Expand Down
51 changes: 51 additions & 0 deletions runkit/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"
"text/template"

"github.com/google/shlex"
"github.com/spf13/pflag"
"mvdan.cc/sh/v3/expand"
"mvdan.cc/sh/v3/interp"
"mvdan.cc/sh/v3/syntax"
Expand Down Expand Up @@ -149,6 +151,19 @@ func (r *Runnable) compute() error {
return nil
}

func flagSet() *pflag.FlagSet {
f := pflag.NewFlagSet("", pflag.ContinueOnError)
f.ParseErrorsWhitelist.UnknownFlags = true
f.StringArrayP("volume", "v", nil, "")
f.StringArray("mount", nil, "")
f.StringArrayP("publish", "p", nil, "")
f.StringP("publish-all", "P", "", "")
f.String("pid", "", "")
f.Bool("privileged", false, "")
f.String("network", "", "")
return f
}

func (r *Runnable) SetOptionValues(opts map[string]string) error {
for _, opt := range r.Action.Options {
if opt.Required && opts[opt.Name] == "" {
Expand All @@ -163,9 +178,45 @@ func (r *Runnable) SetOptionValues(opts map[string]string) error {
}

r.Command = fmt.Sprintf("%s %s", r.command, r.args)

return nil
}

func (r *Runnable) CheckFlags() ([]string, error) {
if r.Action.Type != ActionTypeRun {
return nil, nil
}
tokens, err := shlex.Split(r.args)
if err != nil {
return nil, err
}

f := flagSet()
if err = f.Parse(tokens); err != nil {
return nil, err
}
if f.NArg() > 0 {
args, _, _ := strings.Cut(r.args, f.Arg(0))
tokens, err = shlex.Split(args)
if err != nil {
return nil, err
}
f = flagSet()
if err = f.Parse(tokens); err != nil {
return nil, err
}
}

var flagsSet []string
f.Visit(func(flag *pflag.Flag) {
if flag.Changed {
flagsSet = append(flagsSet, flag.Name)
}
})

return flagsSet, nil
}

func (r *Runnable) Run(ctx context.Context) error {
if r.Command == "" {
return fmt.Errorf("command not set")
Expand Down

0 comments on commit d3713d0

Please sign in to comment.