/
lib.go
77 lines (68 loc) 路 1.52 KB
/
lib.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 (
"log"
"os"
"strings"
"github.com/spf13/cobra"
)
// IfBoolFlag executes a function if a given bool flag is true.
// `os.Exit(0)` is called if successFunc sucessfully runs.
func IfBoolFlag(cmd *cobra.Command, flagName string, successFunc func()) {
boolFlag, err := cmd.Flags().GetBool(flagName)
if err != nil {
log.Fatal(err)
}
if boolFlag {
successFunc()
os.Exit(0)
}
}
// doesExist returns true if a file or folder already exists.
func doesExist(path string) bool {
if _, err := os.Stat(path); !os.IsNotExist(err) {
return true
}
return false
}
func titleString() string {
lines := []string{
" _ _ ",
"| |_ _ __ ___ _ __ | |",
"| __| '_ ` _ \\| '_ \\| |",
"| |_| | | | | | |_) | |",
" \\__|_| |_| |_| .__/|_|",
" |_| ",
"",
}
return strings.Join(lines, "\n")
}
// =============================================================================
// String Slice Functions
// =============================================================================
func contains(slice []string, str string) bool {
for _, s := range slice {
if s == str {
return true
}
}
return false
}
// remove returns a new slice with all given strings removed.
func remove(slice []string, toRemove ...string) []string {
newSlice := make([]string, len(slice)-len(toRemove))
i := 0
for _, s := range slice {
match := false
for _, str := range toRemove {
if str == s {
match = true
break
}
}
if !match {
newSlice[i] = s
i++
}
}
return newSlice
}