-
Notifications
You must be signed in to change notification settings - Fork 1
/
rm.go
77 lines (68 loc) · 1.41 KB
/
rm.go
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
package cmd
import (
"fmt"
"os"
"github.com/AlecAivazis/survey/v2"
"github.com/spf13/cobra"
"github.com/tcd/prjr/internal/prjr"
)
var rmCmd = &cobra.Command{
Use: "rm [OPTIONS]",
Aliases: []string{"remove"},
Short: "Remove an existing project",
Long: "Remove an existing project",
Run: func(cmd *cobra.Command, args []string) {
projects, err := prjr.GetLocalProjects()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
noconfirm, _ := cmd.Flags().GetBool("noconfirm")
if noconfirm {
rmFuncNoconfirm(projects)
} else {
rmFunc(projects)
}
},
}
func init() {
rootCmd.AddCommand(rmCmd)
rmCmd.Flags().BoolP("noconfirm", "Y", false, "Bypass any and all confirmation messages.")
}
func rmFunc(pjs prjr.Projects) {
cwd, err := os.Getwd()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
shouldRemove := false
prompt := &survey.Confirm{Message: fmt.Sprintf("Remove project %q?", cwd)}
survey.AskOne(prompt, &shouldRemove)
if shouldRemove {
pjs.RemoveByRoot(cwd)
err = pjs.Save()
if err != nil {
fmt.Println(err)
os.Exit(1)
} else {
fmt.Printf("Project %q removed\n", cwd)
os.Exit(0)
}
}
}
func rmFuncNoconfirm(pjs prjr.Projects) {
cwd, err := os.Getwd()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
pjs.RemoveByRoot(cwd)
err = pjs.Save()
if err != nil {
fmt.Println(err)
os.Exit(1)
} else {
fmt.Printf("Project %q removed\n", cwd)
os.Exit(0)
}
}