6
6
"github.com/isbm/textwrap"
7
7
"github.com/pelletier/go-toml/v2"
8
8
"gopkg.in/yaml.v3"
9
+ "os"
10
+ "path/filepath"
9
11
"strings"
10
12
)
11
13
@@ -69,11 +71,6 @@ func FromToml(s string) interface{} {
69
71
70
72
}
71
73
72
- func stringMap (v interface {}) (map [string ]interface {}, error ) {
73
- // i don't feel like writing a recursive function right now
74
- return nil , errors .New ("not implemented" )
75
- }
76
-
77
74
// WrapText wraps text to a given width
78
75
func WrapText (width int , text string ) []string {
79
76
wrapper := textwrap .NewTextWrap ()
@@ -89,3 +86,108 @@ func WrapComment(prefix string, width int, comment string) string {
89
86
}
90
87
return strings .Join (wrapped , "\n " )
91
88
}
89
+
90
+ func PathAbsolute (path string ) string {
91
+ path = pathCommonClean (path )
92
+ path , err := filepath .Abs (path )
93
+ if err != nil {
94
+ panic (err )
95
+ }
96
+ return path
97
+ }
98
+
99
+ func PathGlob (path string ) []string {
100
+ path = pathCommonClean (path )
101
+ files , err := filepath .Glob (path )
102
+ if err != nil {
103
+ panic (err )
104
+ }
105
+ return files
106
+ }
107
+
108
+ func PathStat (path string ) map [string ]interface {} {
109
+ path = pathCommonClean (path )
110
+ stat , err := os .Stat (path )
111
+ if err != nil {
112
+ if os .IsNotExist (err ) {
113
+ panic (errors .Join (fmt .Errorf ("file not found: %s" , path )))
114
+ }
115
+ if os .IsPermission (err ) {
116
+ panic (errors .Join (fmt .Errorf ("permission denied: %s" , path )))
117
+ }
118
+ panic (errors .Join (fmt .Errorf ("unknown error %v: %s" , err , path )))
119
+
120
+ }
121
+ return map [string ]interface {}{
122
+ "Name" : stat .Name (),
123
+ "Size" : stat .Size (),
124
+ "Mode" : stat .Mode ().String (),
125
+ "ModTime" : stat .ModTime (),
126
+ "IsDir" : stat .IsDir (),
127
+ "Sys" : stat .Sys (),
128
+ }
129
+ }
130
+
131
+ func pathCommonClean (path string ) string {
132
+ return filepath .Clean (os .ExpandEnv (path ))
133
+ }
134
+
135
+ func PathIsDir (path string ) bool {
136
+ path = pathCommonClean (path )
137
+ stat , err := os .Stat (path )
138
+ if err != nil {
139
+ return false
140
+ }
141
+ return stat .IsDir ()
142
+ }
143
+
144
+ func PathIsFile (path string ) bool {
145
+ path = pathCommonClean (path )
146
+ stat , err := os .Stat (path )
147
+ if err != nil {
148
+ return false
149
+ }
150
+ return ! stat .IsDir ()
151
+ }
152
+
153
+ func PathExists (path string ) bool {
154
+ path = pathCommonClean (path )
155
+ _ , err := os .Stat (path )
156
+ return ! os .IsNotExist (err )
157
+ }
158
+
159
+ func FileRead (path string ) string {
160
+ path = pathCommonClean (path )
161
+ info , err := os .Stat (path )
162
+ if err != nil {
163
+ if os .IsNotExist (err ) {
164
+ return ""
165
+ } else {
166
+ panic (fmt .Errorf ("file not found: %s" , path ))
167
+ }
168
+ }
169
+ if info .IsDir () {
170
+ panic (fmt .Errorf ("cannot read a directory: %s" , path ))
171
+ }
172
+ nBytes := int (info .Size ())
173
+ return FileReadN (nBytes , path )
174
+ }
175
+
176
+ func FileReadN (nBytes int , path string ) string {
177
+ path = pathCommonClean (path )
178
+ f , err := os .Open (path )
179
+ if err != nil {
180
+ panic (err )
181
+ }
182
+ defer f .Close ()
183
+ data := make ([]byte , nBytes )
184
+ n , err := f .Read (data )
185
+ if err != nil {
186
+ panic (err )
187
+ }
188
+ return string (data [:n ])
189
+ }
190
+
191
+ func TypeOf (v interface {}) string {
192
+ return fmt .Sprintf ("%T" , v )
193
+ }
0 commit comments