-
-
Notifications
You must be signed in to change notification settings - Fork 17
feat: implement mutate command #199
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
Open
thesayyn
wants to merge
4
commits into
vbatts:main
Choose a base branch
from
thesayyn:update
base: main
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.
Open
Changes from 2 commits
Commits
Show all changes
4 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,181 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "math" | ||
| "os" | ||
| "slices" | ||
| "strings" | ||
|
|
||
| cli "github.com/urfave/cli/v2" | ||
| "github.com/vbatts/go-mtree" | ||
| ) | ||
|
|
||
| func NewMutateCommand() *cli.Command { | ||
|
|
||
| return &cli.Command{ | ||
| Name: "mutate", | ||
| Usage: "mutate an mtree", | ||
| Description: `Mutate an mtree to have different shapes. | ||
| TODO: more info examples`, | ||
| Action: mutateAction, | ||
| ArgsUsage: "<path to mtree> [path to output]", | ||
| Flags: []cli.Flag{ | ||
| &cli.StringSliceFlag{ | ||
| Name: "strip-prefix", | ||
| }, | ||
| &cli.BoolFlag{ | ||
| Name: "keep-comments", | ||
| Value: false, | ||
| }, | ||
| &cli.BoolFlag{ | ||
| Name: "keep-blank", | ||
| Value: false, | ||
| }, | ||
| &cli.StringFlag{ | ||
| Name: "output", | ||
| TakesFile: true, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func mutateAction(c *cli.Context) error { | ||
| mtreePath := c.Args().Get(0) | ||
| outputPath := c.Args().Get(1) | ||
| stripPrexies := c.StringSlice("strip-prefix") | ||
| keepComments := c.Bool("keep-comments") | ||
| keepBlank := c.Bool("keep-blank") | ||
|
|
||
| if mtreePath == "" { | ||
| return fmt.Errorf("mtree path is required.") | ||
| } else if outputPath == "" { | ||
| outputPath = mtreePath | ||
| } | ||
|
|
||
| file, err := os.Open(mtreePath) | ||
| if err != nil { | ||
| return fmt.Errorf("opening %s: %w", mtreePath, err) | ||
| } | ||
|
|
||
| spec, err := mtree.ParseSpec(file) | ||
| if err != nil { | ||
| return fmt.Errorf("parsing mtree %s: %w", mtreePath, err) | ||
| } | ||
|
|
||
| stripPrefixVisitor := stripPrefixVisitor{ | ||
| prefixes: stripPrexies, | ||
| } | ||
| tidyVisitor := tidyVisitor{ | ||
| keepComments: keepComments, | ||
| keepBlank: keepBlank, | ||
| } | ||
| visitors := []Visitor{ | ||
| &stripPrefixVisitor, | ||
| &tidyVisitor, | ||
| } | ||
|
|
||
| dropped := []int{} | ||
| entries := []mtree.Entry{} | ||
|
|
||
| skip: | ||
| for _, entry := range spec.Entries { | ||
|
|
||
| if entry.Parent != nil && slices.Contains(dropped, entry.Parent.Pos) { | ||
| if entry.Type == mtree.DotDotType { | ||
| // directory for this .. has been dropped so shall this | ||
| continue | ||
| } | ||
| entry.Parent = entry.Parent.Parent | ||
| // TODO: i am not sure if this is the correct behavior | ||
| entry.Raw = strings.TrimPrefix(entry.Raw, " ") | ||
| } | ||
|
|
||
| for _, visitor := range visitors { | ||
| drop, err := visitor.Visit(&entry) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if drop { | ||
| dropped = append(dropped, entry.Pos) | ||
| continue skip | ||
| } | ||
| } | ||
|
|
||
| entries = append(entries, entry) | ||
| } | ||
|
|
||
| spec.Entries = entries | ||
|
|
||
| var writer io.Writer = os.Stdout | ||
| if outputPath != "-" { | ||
| writer, err = os.Create(outputPath) | ||
| if err != nil { | ||
| return fmt.Errorf("creating output %s: %w", outputPath, err) | ||
| } | ||
| } | ||
|
|
||
| spec.WriteTo(writer) | ||
|
thesayyn marked this conversation as resolved.
Outdated
|
||
|
|
||
| return nil | ||
| } | ||
|
|
||
| type Visitor interface { | ||
| Visit(entry *mtree.Entry) (bool, error) | ||
| } | ||
|
|
||
| type tidyVisitor struct { | ||
| keepComments bool | ||
| keepBlank bool | ||
| } | ||
|
|
||
| func (m *tidyVisitor) Visit(entry *mtree.Entry) (bool, error) { | ||
| if !m.keepComments && entry.Type == mtree.CommentType { | ||
| return true, nil | ||
| } else if !m.keepBlank && entry.Type == mtree.BlankType { | ||
| return true, nil | ||
| } | ||
| return false, nil | ||
| } | ||
|
|
||
| type stripPrefixVisitor struct { | ||
| prefixes []string | ||
| } | ||
|
|
||
| func (m *stripPrefixVisitor) Visit(entry *mtree.Entry) (bool, error) { | ||
| if entry.Type != mtree.FullType && entry.Type != mtree.RelativeType { | ||
| return false, nil | ||
| } | ||
|
|
||
| fp, err := entry.Path() | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| pathSegments := strings.Split(fp, "/") | ||
|
|
||
| for _, prefix := range m.prefixes { | ||
|
|
||
| prefixSegments := strings.Split(prefix, "/") | ||
| minLen := int(math.Min(float64(len(pathSegments)), float64(len(prefixSegments)))) | ||
| matches := make([]string, minLen) | ||
| for i := 0; i < minLen; i++ { | ||
| if pathSegments[i] == prefixSegments[i] { | ||
| matches[i] = prefixSegments[i] | ||
| } | ||
| } | ||
|
|
||
| strip := strings.Join(matches, "/") | ||
| if entry.Type == mtree.FullType { | ||
| entry.Name = strings.TrimPrefix(entry.Name, strip) | ||
| entry.Name = strings.TrimPrefix(entry.Name, "/") | ||
| if entry.Name == "" { | ||
| return true, nil | ||
| } | ||
| } else if fp == strip { | ||
| return true, nil | ||
| } | ||
| } | ||
| return false, 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| module github.com/vbatts/go-mtree | ||
|
|
||
| go 1.17 | ||
| go 1.18 | ||
|
|
||
| require ( | ||
| github.com/davecgh/go-spew v1.1.1 | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # ./lib | ||
| lib type=dir mode=0644 | ||
| foo mode=0644 size=12288 time=1457644483.833957552 type=file | ||
|
|
||
| .. | ||
|
|
||
| ./lib type=dir mode=0644 | ||
|
|
||
|
|
||
| ayo mode=0644 size=12288 time=1457644483.833957552 type=file | ||
|
|
||
| lib/dir/sub type=dir | ||
| lib/dir/sub/file.txt type=file | ||
|
|
||
| lib/dir/PKG.info type=file mode=0644 |
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.