1
1
package examples
2
2
3
3
import (
4
- "bytes"
5
- "errors"
6
4
"fmt"
7
- "io"
8
5
"os"
9
- "path/filepath"
10
6
"strings"
11
7
12
8
"github.com/FrangipaneTeam/bean/config"
13
9
"github.com/FrangipaneTeam/bean/internal/exlist"
10
+ yml "github.com/FrangipaneTeam/bean/pkg/yaml"
14
11
"github.com/FrangipaneTeam/bean/tui/pages/errorpanel"
15
12
"github.com/charmbracelet/bubbles/list"
16
13
tea "github.com/charmbracelet/bubbletea"
@@ -95,7 +92,7 @@ func createExampleList(dir string) ([]*exlist.Example, *errorpanel.ErrorMsg) {
95
92
}
96
93
97
94
// get only yaml files
98
- if isYaml := isYamlFile (sf .Name ()); ! isYaml {
95
+ if isYaml := yml . IsYamlFile (sf .Name ()); ! isYaml {
99
96
continue
100
97
}
101
98
@@ -157,12 +154,6 @@ func createExampleList(dir string) ([]*exlist.Example, *errorpanel.ErrorMsg) {
157
154
return exampleList , nil
158
155
}
159
156
160
- // isYamlFile returns true if the file is a yaml file.
161
- func isYamlFile (fileName string ) bool {
162
- ext := filepath .Ext (fileName )
163
- return ext == ".yaml" || ext == ".yml"
164
- }
165
-
166
157
func checkForExtraFile (dir string , file string ) (int , * errorpanel.ErrorMsg ) {
167
158
var (
168
159
extraK8S * exlist.Example
@@ -173,7 +164,7 @@ func checkForExtraFile(dir string, file string) (int, *errorpanel.ErrorMsg) {
173
164
fmt .Sprintf ("%s/%s.extra" , dir , file ),
174
165
)
175
166
if err == nil {
176
- extraY , errSplitYaml := splitYAML (extraYFile )
167
+ extraY , errSplitYaml := yml . SplitYAML (extraYFile )
177
168
if errSplitYaml != nil {
178
169
return extraKind , & errorpanel.ErrorMsg {
179
170
Reason : "split yaml error" ,
@@ -204,7 +195,7 @@ func checkForSecretFile(dir string, file string) (int, *errorpanel.ErrorMsg) {
204
195
fmt .Sprintf ("%s/%s.secret" , dir , file ),
205
196
)
206
197
if err == nil {
207
- extraY , errSplitYaml := splitYAML (extraSFile )
198
+ extraY , errSplitYaml := yml . SplitYAML (extraSFile )
208
199
if errSplitYaml != nil {
209
200
return extraKind , & errorpanel.ErrorMsg {
210
201
Reason : "split yaml error" ,
@@ -225,25 +216,3 @@ func checkForSecretFile(dir string, file string) (int, *errorpanel.ErrorMsg) {
225
216
}
226
217
return extraKind , nil
227
218
}
228
-
229
- func splitYAML (resources []byte ) ([][]byte , error ) {
230
- dec := yaml .NewDecoder (bytes .NewReader (resources ))
231
-
232
- var res [][]byte
233
- for {
234
- var value interface {}
235
- err := dec .Decode (& value )
236
- if errors .Is (err , io .EOF ) {
237
- break
238
- }
239
- if err != nil {
240
- return nil , err
241
- }
242
- valueBytes , err := yaml .Marshal (value )
243
- if err != nil {
244
- return nil , err
245
- }
246
- res = append (res , valueBytes )
247
- }
248
- return res , nil
249
- }
0 commit comments