@@ -53,16 +53,16 @@ func RunImageMenu(
5353 fmt .Printf ("\n %s%s%s\n " , utils .ColorGreen , tr .MenuFunction , utils .ColorNC )
5454 fmt .Println ("------------------------------" )
5555 fmt .Printf (" 1. %s\n " , tr .PullImage )
56- fmt .Printf (" 2. %s\n " , tr .DeleteSpecifiedImage )
57- fmt .Printf (" 3. %s\n " , tr .DeleteAllImages )
56+ fmt .Printf (" 2. %s\n " , tr .MenuRunImage )
57+ fmt .Printf (" 3. %s\n " , tr .MenuDeleteImage )
58+ fmt .Printf (" 4. %s\n " , tr .DeleteAllImages )
5859 fmt .Println ("------------------------------" )
5960 fmt .Printf (" s. %s\n " , tr .SearchImageTitle )
6061 fmt .Println ("------------------------------" )
6162 fmt .Printf (" 0. %s\n " , tr .Return )
6263 fmt .Println ("------------------------------" )
63- fmt .Print (tr .InputServiceNameIdx ) // Using common prompt
6464
65- input := readInput ("" )
65+ input := readInput (tr . InputImageToRun )
6666 input = strings .TrimSpace (input )
6767
6868 if input == "0" || input == "" || input == "q" {
@@ -77,95 +77,100 @@ func RunImageMenu(
7777 result := runFzfSelect (tr .SearchImageTitle , lines )
7878 selected := parseFzfResult (result )
7979 if selected != "" {
80- // Handle selected image
8180 var idx int
8281 _ , _ = fmt .Sscanf (selected , "%d" , & idx )
8382 if idx > 0 && idx <= len (images ) {
84- handleImageAction (images [idx - 1 ], tr , readInput , runInteractiveSubprocess , runFzfSelect , parseFzfResult )
83+ directRunImage (images [idx - 1 ], tr , readInput , runInteractiveSubprocess )
8584 }
8685 }
8786 continue
8887 }
8988
9089 switch input {
9190 case "1" :
92- fmt .Printf ("%s: " , tr .PullImage )
93- name := readInput ("" )
91+ name := readInput (tr .PullImage + ": " )
9492 name = strings .TrimSpace (name )
9593 if name != "" {
9694 fmt .Printf ("%s%s %s...%s\n " , utils .ColorYellow , tr .PullImage , name , utils .ColorNC )
9795 cmd := exec .Command ("docker" , "pull" , name )
9896 _ = runInteractiveSubprocess (cmd )
9997 }
100- case "2" :
101- fmt .Print (tr .InputServiceNameIdx )
102- idxStr := readInput ("" )
98+ case "2" : // Direct Run
10399 var idx int
104- _ , _ = fmt .Sscanf (strings . TrimSpace ( idxStr ) , "%d" , & idx )
100+ _ , _ = fmt .Sscanf (input , "%d" , & idx ) // input is already the index since it was read at top
105101 if idx > 0 && idx <= len (images ) {
106- handleImageAction (images [idx - 1 ], tr , readInput , runInteractiveSubprocess , runFzfSelect , parseFzfResult )
102+ directRunImage (images [idx - 1 ], tr , readInput , runInteractiveSubprocess )
107103 } else {
108104 fmt .Printf ("%s%s%s\n " , utils .ColorRed , tr .InvalidIndex , utils .ColorNC )
109105 time .Sleep (1 * time .Second )
110106 }
111- case "3" :
107+ case "3" : // Direct Delete
108+ var idx int
109+ _ , _ = fmt .Sscanf (input , "%d" , & idx )
110+ if idx > 0 && idx <= len (images ) {
111+ directDeleteImage (images [idx - 1 ], tr , readInput )
112+ } else {
113+ fmt .Printf ("%s%s%s\n " , utils .ColorRed , tr .InvalidIndex , utils .ColorNC )
114+ time .Sleep (1 * time .Second )
115+ }
116+ case "4" :
112117 fmt .Printf ("%s%s%s" , utils .ColorRed , tr .DangerDeleteAllImages , utils .ColorNC )
113118 confirm := readInput ("" )
114119 if strings .ToLower (strings .TrimSpace (confirm )) == "y" {
115120 fmt .Printf ("%s%s%s\n " , utils .ColorYellow , tr .CleaningAllUnusedImages , utils .ColorNC )
116121 cmd := exec .Command ("docker" , "image" , "prune" , "-af" )
117122 _ = runInteractiveSubprocess (cmd )
118123 }
124+ default :
125+ // Try to treat input as index directly if it's a number
126+ var idx int
127+ n , _ := fmt .Sscanf (input , "%d" , & idx )
128+ if n > 0 && idx > 0 && idx <= len (images ) {
129+ // Default behavior for index: show action menu? No, user wants fast.
130+ // But we don't know if they want to run or delete.
131+ // Given they mostly run, let's show the direct action choice or just run.
132+ // Actually, the user asked to split 2 and 3, so we follow that.
133+ }
119134 }
120135 }
121136}
122137
123- func handleImageAction (
138+ func directRunImage (
124139 img * commands.Image ,
125140 tr * i18n.TranslationSet ,
126141 readInput func (string ) string ,
127142 runInteractiveSubprocess func (* exec.Cmd ) error ,
128- runFzfSelect func (string , []string ) string ,
129- parseFzfResult func (string ) string ,
130143) {
131- lines := []string {
132- fmt .Sprintf ("1: %s" , tr .RemoveImage ),
133- fmt .Sprintf ("2: %s" , tr .RunImage ),
144+ name := readInput (tr .InputContainerName )
145+ name = strings .TrimSpace (name )
146+
147+ fmt .Printf ("%s%s%s\n " , utils .ColorBlue , tr .DetectingShell , utils .ColorNC )
148+ checkCmd := exec .Command ("docker" , "run" , "--rm" , img .ID , "which" , "bash" )
149+ shell := "sh"
150+ if err := checkCmd .Run (); err == nil {
151+ shell = "bash"
134152 }
135153
136- result := runFzfSelect (fmt .Sprintf (tr .SelectActionForImage , img .Name ), lines )
137- actionID := parseFzfResult (result )
138-
139- switch actionID {
140- case "1" : // Remove
141- fmt .Printf ("%s%s %s (ID: %s) ? (y/n): %s" , utils .ColorYellow , tr .ConfirmDeleteImage , img .Name , img .ID [:12 ], utils .ColorNC )
142- confirm := readInput ("" )
143- if strings .ToLower (strings .TrimSpace (confirm )) == "y" {
144- fmt .Printf ("%s%s%s\n " , utils .ColorYellow , tr .DeletingImage , utils .ColorNC )
145- if err := img .Remove (image.RemoveOptions {Force : true }); err != nil {
146- fmt .Printf ("%s%s: %v%s\n " , utils .ColorRed , tr .ActionFailed , err , utils .ColorNC )
147- } else {
148- fmt .Printf ("%s%s%s\n " , utils .ColorGreen , tr .ActionSuccess , utils .ColorNC )
149- }
150- time .Sleep (1 * time .Second )
151- }
152- case "2" : // Run (Interactive)
153- fmt .Print (tr .InputContainerName )
154- name := readInput ("" )
155- name = strings .TrimSpace (name )
156-
157- fmt .Printf ("%s%s%s\n " , utils .ColorBlue , tr .DetectingShell , utils .ColorNC )
158- // 参考进入容器的做法,通过一个临时容器检测 shell
159- checkCmd := exec .Command ("docker" , "run" , "--rm" , img .ID , "which" , "bash" )
160- shell := "sh"
161- if err := checkCmd .Run (); err == nil {
162- shell = "bash"
163- }
154+ cmd := img .GetInteractiveRunCmd (name , shell )
155+ if err := runInteractiveSubprocess (cmd ); err != nil {
156+ fmt .Printf ("%s%s%s\n " , utils .ColorRed , fmt .Sprintf (tr .RunImageFailed , err ), utils .ColorNC )
157+ time .Sleep (1 * time .Second )
158+ }
159+ }
164160
165- cmd := img .GetInteractiveRunCmd (name , shell )
166- if err := runInteractiveSubprocess (cmd ); err != nil {
167- fmt .Printf ("%s%s%s\n " , utils .ColorRed , fmt .Sprintf (tr .RunImageFailed , err ), utils .ColorNC )
168- time .Sleep (1 * time .Second )
161+ func directDeleteImage (
162+ img * commands.Image ,
163+ tr * i18n.TranslationSet ,
164+ readInput func (string ) string ,
165+ ) {
166+ confirm := readInput (fmt .Sprintf ("%s%s %s (ID: %s) ? (y/n): %s" , utils .ColorYellow , tr .ConfirmDeleteImage , img .Name , img .ID [:12 ], utils .ColorNC ))
167+ if strings .ToLower (strings .TrimSpace (confirm )) == "y" {
168+ fmt .Printf ("%s%s%s\n " , utils .ColorYellow , tr .DeletingImage , utils .ColorNC )
169+ if err := img .Remove (image.RemoveOptions {Force : true }); err != nil {
170+ fmt .Printf ("%s%s: %v%s\n " , utils .ColorRed , tr .ActionFailed , err , utils .ColorNC )
171+ } else {
172+ fmt .Printf ("%s%s%s\n " , utils .ColorGreen , tr .ActionSuccess , utils .ColorNC )
169173 }
174+ time .Sleep (1 * time .Second )
170175 }
171176}
0 commit comments