/
util.go
68 lines (61 loc) · 1.49 KB
/
util.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
package rrh
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
"time"
humanize "github.com/dustin/go-humanize"
)
/*
IsInputYes print the given prompt and returns TRUE if the user inputs "yes".
*/
func IsInputYes(prompt string) bool {
fmt.Print(prompt)
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
var text = strings.ToLower(scanner.Text())
return text == "yes" || text == "y"
}
/*
CreateParentDir create the parent directories of the given path.
*/
func CreateParentDir(path string) error {
var parent = filepath.Dir(path)
return os.MkdirAll(parent, 0755)
}
/*
Strftime returns the string of the given time.
*/
func Strftime(time time.Time, config *Config) string {
var format = config.GetValue(TimeFormat)
if format != Relative {
return time.Format(format)
}
return HumanizeTime(time)
}
/*
HumanizeTime convert the given time to human friendly formatted string.
*/
func HumanizeTime(time time.Time) string {
return humanize.Time(time)
}
/*
IsExistAndGitRepository checks the given absPath is exist and shows the git repository.
*/
func IsExistAndGitRepository(absPath string, path string) error {
var fmode, err = os.Stat(absPath)
if err != nil {
return err
}
if !fmode.IsDir() {
return fmt.Errorf("%s: not directory", path)
}
fmode, err = os.Stat(filepath.Join(absPath, ".git"))
// If the repository of path is submodule, `.git` will be a file to indicate the `.git` directory.
if os.IsNotExist(err) {
return fmt.Errorf("%s: not git repository", path)
}
return nil
}