Skip to content

Commit

Permalink
refactor: add option to format when loading yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
dcreey committed Apr 17, 2024
1 parent d58be4b commit 46fd62f
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 25 deletions.
2 changes: 1 addition & 1 deletion command/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (c *CheckCommand) Run(ctx context.Context, originalArgs []string) error {

fsys := os.DirFS(".")

files, err := loadYAMLFiles(fsys, args)
files, err := loadYAMLFiles(fsys, args, false)
if err != nil {
return err
}
Expand Down
15 changes: 11 additions & 4 deletions command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (r loadResults) nodes() []*yaml.Node {
return n
}

func loadYAMLFiles(fsys fs.FS, paths []string) (loadResults, error) {
func loadYAMLFiles(fsys fs.FS, paths []string, format bool) (loadResults, error) {
r := make(loadResults, 0, len(paths))

for _, pth := range paths {
Expand All @@ -137,12 +137,19 @@ func loadYAMLFiles(fsys fs.FS, paths []string) (loadResults, error) {
if err := yaml.Unmarshal(contents, &node); err != nil {
return nil, fmt.Errorf("failed to parse yaml for %s: %w", pth, err)
}

r = append(r, &loadResult{
lr := &loadResult{
path: pth,
node: &node,
contents: contents,
})
}

if format {
if err := FixIndentation(lr); err != nil {
return nil, fmt.Errorf("failed to format indentation: %w", err)
}
}

r = append(r, lr)
}

return r, nil
Expand Down
6 changes: 1 addition & 5 deletions command/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (c *PinCommand) Run(ctx context.Context, originalArgs []string) error {

fsys := os.DirFS(".")

files, err := loadYAMLFiles(fsys, args)
files, err := loadYAMLFiles(fsys, args, true)
if err != nil {
return err
}
Expand All @@ -89,10 +89,6 @@ func (c *PinCommand) Run(ctx context.Context, originalArgs []string) error {
return fmt.Errorf("-out must be a directory when pinning multiple files")
}

for _, f := range files {
FixIndentation(f)
}

if err := parser.Pin(ctx, res, par, files.nodes(), c.flagConcurrency); err != nil {
return fmt.Errorf("failed to pin refs: %w", err)
}
Expand Down
6 changes: 1 addition & 5 deletions command/unpin.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (c *UnpinCommand) Run(ctx context.Context, originalArgs []string) error {

fsys := os.DirFS(".")

files, err := loadYAMLFiles(fsys, args)
files, err := loadYAMLFiles(fsys, args, true)
if err != nil {
return err
}
Expand All @@ -73,10 +73,6 @@ func (c *UnpinCommand) Run(ctx context.Context, originalArgs []string) error {
return fmt.Errorf("-out must be a directory when pinning multiple files")
}

for _, f := range files {
FixIndentation(f)
}

if err := parser.Unpin(ctx, files.nodes()); err != nil {
return fmt.Errorf("failed to pin refs: %w", err)
}
Expand Down
6 changes: 1 addition & 5 deletions command/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (c *UpdateCommand) Run(ctx context.Context, originalArgs []string) error {

fsys := os.DirFS(".")

files, err := loadYAMLFiles(fsys, args)
files, err := loadYAMLFiles(fsys, args, true)
if err != nil {
return err
}
Expand All @@ -78,10 +78,6 @@ func (c *UpdateCommand) Run(ctx context.Context, originalArgs []string) error {
return fmt.Errorf("-out must be a directory when pinning multiple files")
}

for _, f := range files {
FixIndentation(f)
}

if err := parser.Unpin(ctx, files.nodes()); err != nil {
return fmt.Errorf("failed to pin refs: %w", err)
}
Expand Down
6 changes: 1 addition & 5 deletions command/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (c *UpgradeCommand) Run(ctx context.Context, originalArgs []string) error {

fsys := os.DirFS(".")

files, err := loadYAMLFiles(fsys, args)
files, err := loadYAMLFiles(fsys, args, true)
if err != nil {
return err
}
Expand All @@ -86,10 +86,6 @@ func (c *UpgradeCommand) Run(ctx context.Context, originalArgs []string) error {
return fmt.Errorf("-out must be a directory when upgrading multiple files")
}

for _, f := range files {
FixIndentation(f)
}

if err := parser.Unpin(ctx, files.nodes()); err != nil {
return fmt.Errorf("failed to unpin refs: %w", err)
}
Expand Down

0 comments on commit 46fd62f

Please sign in to comment.