Skip to content

Commit 5af2c02

Browse files
committed
Allow url recipes
Signed-off-by: Peter Verraedt <peter@verraedt.be>
1 parent 2cb6506 commit 5af2c02

File tree

3 files changed

+55
-19
lines changed

3 files changed

+55
-19
lines changed

cmd/iron/cli/app.go

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/c-bata/go-prompt"
1313
"github.com/creativeprojects/go-selfupdate"
14+
"github.com/google/shlex"
1415
"github.com/kuleuven/iron"
1516
"github.com/kuleuven/iron/cmd/iron/shell"
1617
"github.com/sirupsen/logrus"
@@ -76,7 +77,7 @@ func (a *App) Command() *cobra.Command {
7677
shellCmd.PersistentPreRunE = a.ShellInit
7778

7879
// Open subcommand
79-
openURLCmd := openCmd(rootCmd)
80+
openURLCmd := a.xopen()
8081

8182
rootCmd.AddCommand(shellCmd, openURLCmd)
8283

@@ -143,7 +144,7 @@ func (a *App) root(shellCommand bool) *cobra.Command {
143144
return rootCmd
144145
}
145146

146-
func openCmd(rootCmd *cobra.Command) *cobra.Command {
147+
func (a *App) xopen() *cobra.Command {
147148
return &cobra.Command{
148149
Use: "x-open [url]",
149150
Short: "Open a special url, for browser-initiated commands.",
@@ -154,38 +155,73 @@ func openCmd(rootCmd *cobra.Command) *cobra.Command {
154155
SilenceErrors: true,
155156
SilenceUsage: true,
156157
RunE: func(cmd *cobra.Command, args []string) error {
157-
parts, err := url.Parse(args[0])
158+
commands, ok := strings.CutPrefix(args[0], a.name+"://")
159+
if !ok {
160+
return fmt.Errorf("invalid url, can only open %s:// urls", a.name)
161+
}
162+
163+
commands, query, _ := strings.Cut(commands, "?")
164+
165+
options, err := url.ParseQuery(query)
158166
if err != nil {
159167
return err
160168
}
161169

162-
args = []string{"--workdir", parts.Path, parts.Host}
170+
rootCmd := a.root(options.Has("shell"))
171+
172+
for line := range strings.SplitSeq(commands, ";") {
173+
line, err := url.QueryUnescape(line)
174+
if err != nil {
175+
return err
176+
}
163177

164-
for urlarg := range strings.SplitSeq(parts.RawQuery, "&") {
165-
if _, value, ok := strings.Cut(urlarg, "="); ok {
166-
unescaped, err := url.QueryUnescape(value)
167-
if err != nil {
168-
return err
169-
}
178+
prefix := fmt.Sprintf("%s > %s", a.name, a.Workdir)
170179

171-
args = append(args, unescaped)
180+
if a.Workdir == "" {
181+
prefix = a.name
182+
}
183+
184+
fmt.Fprintf(cmd.OutOrStdout(), "%s%s >%s %s\n", Blue, prefix, Reset, line)
185+
186+
if err = executeCommand(rootCmd, line); err != nil {
187+
goto exit
172188
}
173189
}
174190

175-
rootCmd.SetArgs(args)
191+
if options.Has("shell") {
192+
// Drop to shell
193+
hiddenChild := a.root(true)
194+
hiddenChild.Hidden = true
195+
rootCmd.AddCommand(hiddenChild)
176196

177-
err = rootCmd.ExecuteContext(cmd.Context())
197+
shell.New(rootCmd, prompt.OptionLivePrefix(a.prefix)).Run(rootCmd, nil)
178198

179-
if parts.Host != "shell" {
180-
fmt.Fprintf(cmd.OutOrStdout(), "[Press enter to exit]\n")
181-
fmt.Scanln() //nolint:errcheck
199+
return nil
182200
}
183201

202+
exit:
203+
fmt.Fprintf(cmd.OutOrStdout(), "[Press enter to exit]\n")
204+
205+
fmt.Scanln() //nolint:errcheck
206+
184207
return err
185208
},
186209
}
187210
}
188211

212+
func executeCommand(cmd *cobra.Command, line string) error {
213+
args, err := shlex.Split(line)
214+
if err != nil {
215+
return err
216+
}
217+
218+
shell.ResetArgs(cmd, args)
219+
220+
cmd.SetArgs(args)
221+
222+
return cmd.ExecuteContext(cmd.Context())
223+
}
224+
189225
func (a *App) prefix() (string, bool) {
190226
return fmt.Sprintf("%s > %s > ", a.name, a.Workdir), true
191227
}

cmd/iron/cli/app_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ func TestXOpen(t *testing.T) {
437437
cmd := app.Command()
438438
cmd.SetContext(t.Context())
439439

440-
cmd.SetArgs([]string{"x-open", "iron://version"})
440+
cmd.SetArgs([]string{"x-open", `test://version;local%20fls`})
441441

442442
if err := cmd.ExecuteContext(t.Context()); err != nil {
443443
t.Fatal(err)

cmd/iron/shell/shell.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func assert(err error) {
176176
}
177177

178178
func execute(cmd *cobra.Command, args []string) error {
179-
resetArgs(cmd, args)
179+
ResetArgs(cmd, args)
180180

181181
cmd.SetArgs(args)
182182

@@ -188,7 +188,7 @@ func execute(cmd *cobra.Command, args []string) error {
188188
return cmd.ExecuteContext(ctx)
189189
}
190190

191-
func resetArgs(cmd *cobra.Command, args []string) {
191+
func ResetArgs(cmd *cobra.Command, args []string) {
192192
if cmd, _, err := cmd.Find(args); err == nil {
193193
// Reset flag values between runs due to a limitation in Cobra
194194
cmd.Flags().VisitAll(func(flag *pflag.Flag) {

0 commit comments

Comments
 (0)