-
Notifications
You must be signed in to change notification settings - Fork 164
/
fs.go
94 lines (82 loc) · 2.29 KB
/
fs.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
90
91
92
93
94
package fs
import (
"fmt"
"os"
"github.com/Sirupsen/logrus"
"github.com/Songmu/prompter"
"github.com/spf13/afero"
"github.com/spf13/viper"
)
var defaultFs *KitFs
// KitFs wraps an afero.Fs
type KitFs struct {
Fs afero.Fs
}
func (f *KitFs) init(dir string) {
var inFs afero.Fs
if viper.GetBool("gk_testing") {
inFs = afero.NewMemMapFs()
} else {
if viper.GetString("gk_folder") != "" {
inFs = afero.NewBasePathFs(afero.NewOsFs(), viper.GetString("gk_folder"))
} else {
inFs = afero.NewOsFs()
}
}
if dir != "" {
f.Fs = afero.NewBasePathFs(inFs, dir)
} else {
f.Fs = inFs
}
}
// ReadFile reads the file from `path` and returns the content in string format
// or returns an error if it occurs.
func (f *KitFs) ReadFile(path string) (string, error) {
d, err := afero.ReadFile(f.Fs, path)
return string(d), err
}
// WriteFile writs a file to the `path` with `data` as content, if `force` is set
// to true it will override the file if it already exists.
func (f *KitFs) WriteFile(path string, data string, force bool) error {
if b, _ := f.Exists(path); b && !(viper.GetBool("gk_force_override") || force) {
s, _ := f.ReadFile(path)
if s == data {
logrus.Warnf("`%s` exists and is identical it will be ignored", path)
return nil
}
b := prompter.YN(fmt.Sprintf("`%s` already exists do you want to override it ?", path), false)
if !b {
return nil
}
}
return afero.WriteFile(f.Fs, path, []byte(data), os.ModePerm)
}
// Mkdir creates a directory.
func (f *KitFs) Mkdir(dir string) error {
return f.Fs.Mkdir(dir, os.ModePerm)
}
// MkdirAll creates a directory and its parents if they don't exist.
func (f *KitFs) MkdirAll(path string) error {
return f.Fs.MkdirAll(path, os.ModePerm)
}
// Exists returns true,nil if the dir/file exists or false,nil if
// the dir/file does not exist, it will return an error if something
// went wrong.
func (f *KitFs) Exists(path string) (bool, error) {
return afero.Exists(f.Fs, path)
}
// NewDefaultFs creates a KitFs with `dir` as root.
func NewDefaultFs(dir string) *KitFs {
dfs := &KitFs{}
dfs.init(dir)
defaultFs = dfs
return dfs
}
// Get returns a new KitFs if it was not initiated before or
// it returns the existing defaultFs if it is initiated.
func Get() *KitFs {
if defaultFs == nil {
return NewDefaultFs("")
}
return defaultFs
}