-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathremove.go
More file actions
89 lines (70 loc) · 2.35 KB
/
Copy pathremove.go
File metadata and controls
89 lines (70 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)
package remove
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/cartesi/rollups-node/internal/cli"
"github.com/cartesi/rollups-node/internal/config"
"github.com/cartesi/rollups-node/internal/repository/factory"
)
var Cmd = &cobra.Command{
Use: "remove [app-name-or-address]",
Aliases: []string{"rm"},
Short: "Remove registered applications",
Example: examples,
Args: cobra.ExactArgs(1),
Run: run,
Long: `
Supported Environment Variables:
CARTESI_DATABASE_CONNECTION Database connection string`,
}
const examples = `# Remove application:
cartesi-rollups-cli app remove echo-dapp
# Remove application without confirmation:
cartesi-rollups-cli app remove echo-dapp --yes`
var yesFlag bool
func init() {
Cmd.Flags().BoolVarP(&yesFlag, "yes", "y", false, "Skip confirmation prompts")
origHelpFunc := Cmd.HelpFunc()
Cmd.SetHelpFunc(func(command *cobra.Command, strings []string) {
command.Flags().Lookup("verbose").Hidden = false
command.Flags().Lookup("database-connection").Hidden = false
origHelpFunc(command, strings)
})
}
func run(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
nameOrAddress, err := config.ToApplicationNameOrAddressFromString(args[0])
cobra.CheckErr(err)
dsn, err := config.GetDatabaseConnection()
cobra.CheckErr(err)
repo, err := factory.NewRepositoryFromConnectionString(ctx, dsn.Raw())
cobra.CheckErr(err)
defer repo.Close()
app, err := repo.GetApplication(ctx, nameOrAddress)
cobra.CheckErr(err)
if app == nil {
fmt.Fprintf(os.Stderr, "application %q not found\n", nameOrAddress)
repo.Close()
os.Exit(1) //nolint:gocritic // The repository is closed explicitly before exiting.
}
if app.Enabled {
fmt.Fprintf(os.Stderr, "Error: Application %s has enabled=true. Must disable it first\n", app.Name)
repo.Close()
os.Exit(1)
}
if !yesFlag {
confirmed, promptErr := cli.ConfirmPrompt(
fmt.Sprintf("Are you sure you want to remove application %s (%s)?",
app.Name, app.IApplicationAddress.String()))
if promptErr != nil || !confirmed {
fmt.Println("Operation cancelled")
return
}
}
err = repo.DeleteApplication(ctx, app.ID)
cobra.CheckErr(err)
fmt.Printf("Application %s (%s) successfully removed\n", app.Name, app.IApplicationAddress.String())
}