Skip to content

Commit a9d73db

Browse files
committed
chore: improved validate
1 parent dae29b4 commit a9d73db

3 files changed

Lines changed: 47 additions & 5 deletions

File tree

cmd/scroll_validate.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"github.com/spf13/cobra"
88
)
99

10+
var strict bool
11+
1012
var ScrollValidateCmd = &cobra.Command{
1113
Use: "validate",
1214
Short: "Validates the scroll file",
@@ -24,11 +26,15 @@ var ScrollValidateCmd = &cobra.Command{
2426
return fmt.Errorf("failed to load scroll: %w", err)
2527
}
2628

27-
if err := scroll.Validate(); err != nil {
29+
if err := scroll.Validate(strict); err != nil {
2830
return fmt.Errorf("failed to validate scroll: %w", err)
2931
}
3032

3133
fmt.Println("Scroll validated successfully.")
3234
return nil
3335
},
3436
}
37+
38+
func init() {
39+
ScrollValidateCmd.Flags().BoolVar(&strict, "strict", false, "Enable strict validation mode")
40+
}

internal/core/domain/scroll.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ type File struct {
7373

7474
type Scroll struct {
7575
File
76+
filePath string
7677
}
7778

7879
type Procedure struct {
@@ -110,7 +111,9 @@ func NewScroll(scrollDir string) (*Scroll, error) {
110111
if err != nil {
111112
return nil, fmt.Errorf("failed to read scroll.yaml - %w", err)
112113
}
113-
scroll := Scroll{}
114+
scroll := Scroll{
115+
filePath: filePath,
116+
}
114117
if _, err = scroll.ParseFile(file); err != nil {
115118
return nil, err
116119
}
@@ -130,7 +133,7 @@ func (sc *Scroll) ParseFile(file []byte) (*Scroll, error) {
130133
return sc, nil
131134
}
132135

133-
func (sc *Scroll) Validate() error {
136+
func (sc *Scroll) Validate(strict bool) error {
134137
if sc.Name == "" {
135138
return fmt.Errorf("scroll name is required")
136139
}
@@ -174,6 +177,28 @@ func (sc *Scroll) Validate() error {
174177
ids[*p.Id] = true
175178
}
176179
}
180+
//scan for files in sc.filePath
181+
entries, err := os.ReadDir(sc.filePath)
182+
if err != nil {
183+
return fmt.Errorf("failed to read scroll directory - %w", err)
184+
}
185+
for _, entry := range entries {
186+
var found = false
187+
for fileName := range ScrollFiles {
188+
if entry.Name() == fileName {
189+
found = true
190+
break
191+
}
192+
}
193+
if !found {
194+
if !strict {
195+
logger.Log().Warn("Directory contains file that is not defined in ScrollFiles", zap.String("file", entry.Name()))
196+
} else {
197+
return fmt.Errorf("directory contains file that is not defined in ScrollFiles: %s", entry.Name())
198+
}
199+
}
200+
}
201+
177202
return nil
178203
}
179204

@@ -184,3 +209,12 @@ func (sc *Scroll) CanColdStart() bool {
184209
func (sc *Scroll) GetColdStartPorts() []Port {
185210
return sc.Ports
186211
}
212+
213+
var ScrollFiles = map[string]ArtifactType{
214+
"update": ArtifactTypeScrollFs,
215+
"scroll.yaml": ArtifactTypeScrollFs,
216+
"packet_handler": ArtifactTypeScrollFs,
217+
"public": ArtifactTypeScrollFs,
218+
"private": ArtifactTypeScrollFs,
219+
"data": ArtifactTypeScrollData,
220+
}

internal/core/services/registry/oci.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,13 @@ func (c *OciClient) PackFolders(fs *file.Store, dirs []string, artifactType doma
266266
// the root has to leaves, one is the real scroll (fs) and the other is meta information about the scroll
267267
func (c *OciClient) Push(folder string, repo string, tag string, overrides map[string]string, packMeta bool, scrollFile *domain.File) (v1.Descriptor, error) {
268268

269-
availableFileNames := []string{"update", "scroll.yaml", "packet_handler", "public", "private"}
270269
fsFileNames := []string{}
271270

272271
//check if files exisits (file or folder) and remove from slice if not
273-
for _, fileName := range availableFileNames {
272+
for fileName, artifactType := range domain.ScrollFiles {
273+
if artifactType != domain.ArtifactTypeScrollData {
274+
continue
275+
}
274276
exists, _ := utils.FileExists(filepath.Join(folder, fileName))
275277
if exists {
276278
fsFileNames = append(fsFileNames, fileName)

0 commit comments

Comments
 (0)