-
Notifications
You must be signed in to change notification settings - Fork 8
feat: public go package blueprint #38
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,121 @@ | ||
| // Package blueprint provides functions for packaging Enapter blueprints | ||
| // into zip archives, with support for .blueprintignore files. | ||
| package blueprint | ||
|
|
||
| import ( | ||
| "archive/zip" | ||
| "bufio" | ||
| "bytes" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "io/fs" | ||
| "strings" | ||
|
|
||
| "github.com/go-git/go-git/v5/plumbing/format/gitignore" | ||
| ) | ||
|
|
||
| const ignoreFileName = ".blueprintignore" | ||
|
|
||
| // Zip creates a zip archive from the blueprint directory at the given | ||
| // filesystem root. It respects .blueprintignore patterns (gitignore syntax). | ||
| // The .blueprintignore file itself is excluded from the archive. | ||
| func Zip(fsys fs.FS) ([]byte, error) { | ||
| matcher, err := loadIgnoreMatcher(fsys) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("load %s: %w", ignoreFileName, err) | ||
| } | ||
|
|
||
| buf := &bytes.Buffer{} | ||
| zw := zip.NewWriter(buf) | ||
|
|
||
| err = fs.WalkDir(fsys, ".", func(path string, entry fs.DirEntry, err error) error { | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if path == "." { | ||
| return nil | ||
| } | ||
|
|
||
| if matcher.Match(path, entry.IsDir()) { | ||
| if entry.IsDir() { | ||
| return fs.SkipDir | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| if entry.IsDir() { | ||
| return nil | ||
| } | ||
|
|
||
| f, err := fsys.Open(path) | ||
| if err != nil { | ||
| return fmt.Errorf("open: %w", err) | ||
| } | ||
| defer f.Close() | ||
|
|
||
| zf, err := zw.Create(path) | ||
| if err != nil { | ||
| return fmt.Errorf("create: %w", err) | ||
| } | ||
|
|
||
| if _, err = io.Copy(zf, f); err != nil { | ||
| return fmt.Errorf("copy: %w", err) | ||
| } | ||
| return nil | ||
| }) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("walk dir: %w", err) | ||
| } | ||
|
|
||
| if err := zw.Close(); err != nil { | ||
| return nil, fmt.Errorf("close zip: %w", err) | ||
| } | ||
|
|
||
| return buf.Bytes(), nil | ||
| } | ||
|
|
||
| type ignoreMatcher struct { | ||
| m gitignore.Matcher | ||
| noFile bool | ||
| } | ||
|
|
||
| func (m *ignoreMatcher) Match(path string, isDir bool) bool { | ||
| if m.noFile { | ||
| return false | ||
| } | ||
|
|
||
| if path == ignoreFileName { | ||
| return true | ||
| } | ||
|
|
||
| parts := strings.Split(path, "/") | ||
| return m.m.Match(parts, isDir) | ||
| } | ||
|
|
||
| func loadIgnoreMatcher(fsys fs.FS) (*ignoreMatcher, error) { | ||
| f, err := fsys.Open(ignoreFileName) | ||
| if errors.Is(err, fs.ErrNotExist) { | ||
| return &ignoreMatcher{noFile: true}, nil | ||
| } | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer f.Close() | ||
|
|
||
| var patterns []gitignore.Pattern | ||
|
|
||
| scanner := bufio.NewScanner(f) | ||
| for scanner.Scan() { | ||
| line := strings.TrimSpace(scanner.Text()) | ||
| if line == "" || strings.HasPrefix(line, "#") { | ||
| continue | ||
| } | ||
| patterns = append(patterns, gitignore.ParsePattern(line, nil)) | ||
| } | ||
| if err := scanner.Err(); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &ignoreMatcher{m: gitignore.NewMatcher(patterns)}, nil | ||
| } | ||
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,17 @@ | ||
| package cliflags | ||
|
|
||
| import ( | ||
| "context" | ||
| "strings" | ||
|
|
||
| "github.com/urfave/cli/v3" | ||
| ) | ||
|
|
||
| // TrimSpaceAction returns a cli.StringFlag Action that trims leading and | ||
| // trailing whitespace from the flag value and stores it in dest. | ||
| func TrimSpaceAction(dest *string) func(context.Context, *cli.Command, string) error { | ||
| return func(_ context.Context, _ *cli.Command, v string) error { | ||
| *dest = strings.TrimSpace(v) | ||
| return nil | ||
| } | ||
| } |
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
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
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
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
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.