@@ -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+
189225func (a * App ) prefix () (string , bool ) {
190226 return fmt .Sprintf ("%s > %s > " , a .name , a .Workdir ), true
191227}
0 commit comments