-
Notifications
You must be signed in to change notification settings - Fork 1
/
kind_path.go
97 lines (84 loc) · 2.32 KB
/
kind_path.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
95
96
97
package semantic
import (
"path/filepath"
"strings"
"github.com/darklab8/fl-configs/configs/configs_mapped/parserutils/inireader"
"github.com/darklab8/go-utils/goutils/utils/utils_types"
)
// Linux friendly filepath, that can be returned to Windows way from linux
type Path struct {
*Value
remove_spaces bool
lowercase bool
}
type PathOption func(s *Path)
func WithoutSpacesP() PathOption {
return func(s *Path) { s.remove_spaces = true }
}
func WithLowercaseP() PathOption {
return func(s *Path) { s.lowercase = true }
}
func OptsP(opts ...ValueOption) PathOption {
return func(s *Path) {
for _, opt := range opts {
opt(s.Value)
}
}
}
func NewPath(section *inireader.Section, key string, opts ...PathOption) *Path {
v := NewValue(section, key)
s := &Path{Value: v}
for _, opt := range opts {
opt(s)
}
return s
}
func (s *Path) FileName() utils_types.FilePath {
if s.optional && len(s.section.ParamMap[s.key]) == 0 {
return ""
}
value := s.section.ParamMap[s.key][s.index].Values[s.order].AsString()
value = strings.ReplaceAll(value, "\\", PATH_SEPARATOR)
value = filepath.Base(value)
if s.remove_spaces {
value = strings.ReplaceAll(value, " ", "")
}
if s.lowercase {
value = strings.ToLower(value)
}
return utils_types.FilePath(value)
}
func (s *Path) Get() utils_types.FilePath {
if s.optional && len(s.section.ParamMap[s.key]) == 0 {
return ""
}
value := s.section.ParamMap[s.key][s.index].Values[s.order].AsString()
value = strings.ReplaceAll(value, "\\", PATH_SEPARATOR)
if s.remove_spaces {
value = strings.ReplaceAll(value, " ", "")
}
if s.lowercase {
value = strings.ToLower(value)
}
return utils_types.FilePath(value)
}
func (s *Path) Set(value utils_types.FilePath) {
if s.isComment() {
s.Delete()
}
processed_value := inireader.UniParseStr(string(value))
if len(s.section.ParamMap[s.key]) == 0 {
s.section.AddParamToStart(s.key, (&inireader.Param{IsComment: s.isComment()}).AddValue(processed_value))
}
// implement SetValue in Section
s.section.ParamMap[s.key][0].First = processed_value
s.section.ParamMap[s.key][0].Values[0] = processed_value
}
func (s *Path) Delete() {
delete(s.section.ParamMap, s.key)
for index, param := range s.section.Params {
if param.Key == s.key {
s.section.Params = append(s.section.Params[:index], s.section.Params[index+1:]...)
}
}
}