-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
wip: gallery refactoring #6482
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
mudler
wants to merge
1
commit into
master
Choose a base branch
from
btree
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
wip: gallery refactoring #6482
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package gallery | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/google/btree" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type Gallery[T GalleryElement] struct { | ||
*btree.BTreeG[T] | ||
} | ||
|
||
func less[T GalleryElement](a, b T) bool { | ||
return a.GetName() < b.GetName() | ||
} | ||
|
||
func (g *Gallery[T]) Search(term string) GalleryElements[T] { | ||
var filteredModels GalleryElements[T] | ||
g.Ascend(func(item T) bool { | ||
if strings.Contains(item.GetName(), term) || | ||
strings.Contains(item.GetDescription(), term) || | ||
strings.Contains(item.GetGallery().Name, term) || | ||
strings.Contains(strings.Join(item.GetTags(), ","), term) { | ||
filteredModels = append(filteredModels, item) | ||
} | ||
return true | ||
}) | ||
|
||
return filteredModels | ||
} | ||
|
||
// processYAMLFile takes a single file path and adds its models to the existing tree. | ||
func processYAMLFile[T GalleryElement](filePath string, tree *Gallery[T]) error { | ||
file, err := os.Open(filePath) | ||
if err != nil { | ||
return fmt.Errorf("could not open %s: %w", filePath, err) | ||
} | ||
defer file.Close() | ||
|
||
decoder := yaml.NewDecoder(file) | ||
|
||
// Stream documents from the file | ||
for { | ||
var model T | ||
err := decoder.Decode(&model) | ||
if err == io.EOF { | ||
break // End of file | ||
} | ||
if err != nil { | ||
return fmt.Errorf("error decoding %s: %w", filePath, err) | ||
} | ||
|
||
tree.ReplaceOrInsert(model) | ||
} | ||
return nil | ||
} | ||
|
||
// loadModelsFromDirectory scans a directory and processes all .yaml/.yml files. | ||
func LoadGalleryFromDirectory[T GalleryElement](dirPath string) (*Gallery[T], error) { | ||
tree := &Gallery[T]{btree.NewG[T](2, less[T])} | ||
|
||
entries, err := os.ReadDir(dirPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not read directory: %w", err) | ||
} | ||
|
||
for _, entry := range entries { | ||
// Skip subdirectories and non-YAML files. | ||
if entry.IsDir() { | ||
continue | ||
} | ||
fileName := entry.Name() | ||
if !strings.HasSuffix(fileName, ".yaml") && !strings.HasSuffix(fileName, ".yml") { | ||
continue | ||
} | ||
|
||
fullPath := filepath.Join(dirPath, fileName) | ||
|
||
err := processYAMLFile(fullPath, tree) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return tree, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / gosec
Potential file inclusion via variable Error