Skip to content

Commit 4303030

Browse files
author
Timo
committed
Modify the mirroring function
1 parent 8040aae commit 4303030

3 files changed

Lines changed: 73 additions & 59 deletions

File tree

‎pkg/i18n/chinese.go‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ func chineseSet() TranslationSet {
218218
// 镜像管理相关
219219
SearchImageTitle: "搜索所有镜像 (支持名称搜索, Esc 返回)",
220220
RunImage: "运行镜像",
221-
InputContainerName: "输入容器名称 (可选,直接回车使用默认): ",
222221
RunImageSuccess: "容器 %s 已启动",
223222
RunImageFailed: "运行镜像失败: %v",
224223
SelectActionForImage: "选择对镜像 [%s] 执行的操作 (Esc 返回)",
@@ -228,8 +227,12 @@ func chineseSet() TranslationSet {
228227
DangerDeleteAllImages: "危险: 这将删除所有未被使用的镜像! 是否继续? (y/n): ",
229228
CleaningAllUnusedImages: "正在清理所有未使用镜像...",
230229
PullImage: "拉取镜像",
231-
DeleteSpecifiedImage: "删除指定镜像",
232-
DeleteAllImages: "删除所有镜像",
230+
MenuRunImage: "运行镜像",
231+
MenuDeleteImage: "删除镜像",
232+
DeleteAllImages: "清理所有未使用镜像",
233+
InputImageToRun: "请输入要运行的镜像索引或名称: ",
234+
InputImageToDelete: "请输入要删除的镜像索引或名称: ",
235+
InputContainerName: "请输入运行名称: ",
233236
DetectingShell: "正在检测可用 Shell...",
234237
}
235238
}

‎pkg/i18n/english.go‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,11 @@ type TranslationSet struct {
231231
DangerDeleteAllImages string
232232
CleaningAllUnusedImages string
233233
PullImage string
234-
DeleteSpecifiedImage string
234+
MenuRunImage string
235+
MenuDeleteImage string
235236
DeleteAllImages string
237+
InputImageToRun string
238+
InputImageToDelete string
236239
DetectingShell string
237240
}
238241

@@ -463,7 +466,6 @@ func englishSet() TranslationSet {
463466
// Image Management
464467
SearchImageTitle: "Search all images (Supports name search, Esc to return)",
465468
RunImage: "Run Image",
466-
InputContainerName: "Input container name (optional, press Enter for default): ",
467469
RunImageSuccess: "Container %s is running",
468470
RunImageFailed: "Run image failed: %v",
469471
SelectActionForImage: "Select action for image [%s] (Esc to return)",
@@ -473,8 +475,12 @@ func englishSet() TranslationSet {
473475
DangerDeleteAllImages: "DANGER: This will delete all unused images! Continue? (y/n): ",
474476
CleaningAllUnusedImages: "Cleaning all unused images...",
475477
PullImage: "Pull Image",
476-
DeleteSpecifiedImage: "Delete Specified Image",
477-
DeleteAllImages: "Delete All Images",
478+
MenuRunImage: "Run Image",
479+
MenuDeleteImage: "Delete Image",
480+
DeleteAllImages: "Prune All Unused Images",
481+
InputImageToRun: "Enter image index or name to RUN: ",
482+
InputImageToDelete: "Enter image index or name to DELETE: ",
483+
InputContainerName: "Enter run name: ",
478484
DetectingShell: "Detecting available shell...",
479485
}
480486
}

‎pkg/manager/image.go‎

Lines changed: 57 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)
⌂ 换网址