Skip to content
This repository was archived by the owner on Jan 7, 2025. It is now read-only.

Commit 4fa46a3

Browse files
authored
When deleting app, verify that given appid exists in the project (#16)
* when deleting app, verify that given appid exists in the project * fix error message
1 parent 0c4db5b commit 4fa46a3

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

cmd/delete.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,38 @@ var deleteCmd = &cobra.Command{
3131
log.Fatalf("Missing app ID: %s", err)
3232
}
3333

34+
ctx := cmd.Context()
35+
projects, err := config_client.GetProject(ctx, &configv1.GetProjectRequest{})
36+
if err != nil {
37+
log.Fatalf("Getting projects failed: %s", err)
38+
}
39+
project := projects.Project[0]
40+
apps, err := config_client.ListApps(ctx, &configv1.ListAppsRequest{Project: project})
41+
if err != nil {
42+
log.Fatalf("Getting apps for project %s failed: %s", project, err)
43+
}
44+
45+
if appList := apps.GetApps(); len(appList) > 0 {
46+
if !appIdInAppList(id, appList) {
47+
cmd.Printf("App id '%s' does not exist. Your project has apps: \n", id)
48+
if err := printApps(cmd.OutOrStdout(), appList...); err != nil {
49+
log.Fatalf("Error listing app: %s", err)
50+
}
51+
return
52+
}
53+
} else {
54+
cmd.Println("No applications found.")
55+
return
56+
}
57+
3458
if !force && !confirm(fmt.Sprintf("Deleting app %s, are you sure?", id), cmd.OutOrStdout(), cmd.InOrStdin()) {
3559
cmd.Println("Deletion aborted.")
3660
return
3761
}
3862

3963
if !dry {
4064
if _, err := config_client.DeleteApp(
41-
cmd.Context(),
65+
ctx,
4266
&configv1.DeleteAppRequest{
4367
AppId: id,
4468
},
@@ -82,3 +106,12 @@ func confirm(prompt string, dst io.Writer, src io.Reader) bool {
82106
}
83107
}
84108
}
109+
110+
func appIdInAppList(appId string, apps []*configv1.App) bool {
111+
for _, app := range apps {
112+
if app.GetId() == appId {
113+
return true
114+
}
115+
}
116+
return false
117+
}

0 commit comments

Comments
 (0)