Skip to content

Commit

Permalink
feat: Include source path comment if not updating files in-place
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Jul 29, 2024
1 parent 650f9a1 commit 464d25a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
18 changes: 14 additions & 4 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ func run(cmd *cobra.Command, args []string) error {
}

var i int
for _, path := range args {
if err := filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error {
for _, arg := range args {
if err := filepath.WalkDir(arg, func(path string, d fs.DirEntry, err error) error {
if err != nil || d.IsDir() || !util.IsYaml(path) {
return err
}
Expand All @@ -102,7 +102,7 @@ func run(cmd *cobra.Command, args []string) error {
}
i++

return openAndTemplateFile(conf, cmd.OutOrStdout(), path)
return openAndTemplateFile(conf, cmd.OutOrStdout(), arg, path)
}); err != nil {
return err
}
Expand All @@ -111,7 +111,7 @@ func run(cmd *cobra.Command, args []string) error {
return nil
}

func openAndTemplateFile(conf *config.Config, w io.Writer, path string) error {
func openAndTemplateFile(conf *config.Config, w io.Writer, dir, path string) error {
f, err := os.Open(path)
if err != nil {
return err
Expand Down Expand Up @@ -178,6 +178,16 @@ func openAndTemplateFile(conf *config.Config, w io.Writer, path string) error {
}
}
} else {
if rel, err := filepath.Rel(dir, path); err == nil && rel != "." {
source := "# Source: " + rel + "\n"
if !strings.HasPrefix(s, "---") {
s = source + s
}
if strings.Contains(s, "---") {
s = strings.ReplaceAll(s, "---\n", "---\n"+source)
}
}

if err := colorize.WriteString(w, s); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func Test_openAndTemplateFile(t *testing.T) {
require.NoError(t, err)

var stdoutBuf strings.Builder
err = openAndTemplateFile(tt.args.conf, &stdoutBuf, p)
err = openAndTemplateFile(tt.args.conf, &stdoutBuf, p, p)
tt.wantErr(t, err)

fileContents, err := os.ReadFile(p)
Expand Down

0 comments on commit 464d25a

Please sign in to comment.