-
Notifications
You must be signed in to change notification settings - Fork 0
/
remmanager.go
89 lines (84 loc) · 2.47 KB
/
remmanager.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
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"github.com/jchengjr77/canaveral/lib"
)
// confirmDelete listens for user confirmation and returns a boolean
// Takes in an input channel to increase testability
// * tested
func confirmDelete(projName string, stdin io.Reader) (res bool, finalErr error) {
// defer a recover function that returns the thrown error
defer func() {
if r := recover(); r != nil {
finalErr = r.(error)
}
}()
fmt.Printf("Are you sure you want to delete %s? ('y' or 'n')\n>", projName)
reader := bufio.NewReader(stdin)
response, err := reader.ReadByte()
lib.Check(err)
return (response == 'y'), nil
}
// tryRemProj tries to delete a project of specified name.
// if the project exists, it will delete it and return true.
// else, it will return false or throw an error.
// * tested
func tryRemProj(projName string, wsPath string) (res bool, finalErr error) {
// defer a recover function that returns the thrown error
defer func() {
if r := recover(); r != nil {
finalErr = r.(error)
}
}()
ws, err := ioutil.ReadFile(wsPath)
lib.Check(err)
files, err := ioutil.ReadDir(string(ws))
lib.Check(err)
for _, file := range files {
if file.Name() == projName {
confirm, err := confirmDelete(projName, os.Stdin)
lib.Check(err)
if !confirm {
fmt.Println("Cancelling project deletion.")
return true, nil
}
err = os.RemoveAll(string(ws) + "/" + projName)
lib.Check(err)
fmt.Printf("Removed Project: %s\n", projName)
return true, nil
}
}
return false, nil
}
// remProjectHandler deletes a project from the canaveral workspace.
// Input: project name (string)
// Behavior: If project is found, prompt deletion of project.
// if deletion confirmed -> delete project entirely
// if deletion cancelled -> exit
// if project is not found, exit.
// * tested
func remProjectHandler(projName string) error {
if projName == "" {
fmt.Println("Cannot remove an unspecified project. Please provide the project name.")
return nil
} else if !lib.FileExists(usrHome + confDir + wsFName) {
fmt.Println("No canaveral workspace set. Please specify a workspace.")
fmt.Println(
"Canaveral needs a workspace to remove projects from.")
fmt.Println("(For help, type 'canaveral --help')")
return nil
}
wsPath := usrHome + confDir + wsFName
res, err := tryRemProj(projName, wsPath)
if err != nil {
return err
} else if !res {
fmt.Printf(
"Could not find project %s in canaveral workspace.\n", projName)
}
return nil
}