-
-
Notifications
You must be signed in to change notification settings - Fork 337
added README example that makes replacements in multiple files #230
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -220,6 +220,28 @@ script.Echo("a\nb\nc").FilterScan(func(line string, w io.Writer) { | |
// scanned line: "c" | ||
``` | ||
|
||
Alternatively, we can use a `Filter` function that returns a `string`. For example, let's fix typos we've made in the CSV files in our working directory: | ||
|
||
```go | ||
script.ListFiles("*.csv").FilterLine(func(file string) string { | ||
search := "typ" | ||
replace := "typo" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should use completely different search and replace strings, as this particular search and replace would not be idempotent: if you keep running it, "typo" will become "typoo", and so on. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point! I'll change it to "helo" --> "hello". |
||
s, err := File(file).String() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reading the whole file into a single string is a bit limiting—and unnecessarily so when we only need to read it line by line. What about using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good point! I originally chose to read in the whole file like this so that I could use |
||
if err != nil { | ||
return fmt.Sprintf("%s %s", file, err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it's better after all to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I created a test script to test what the equivalent
Even though It seems like two approaches are:
With all of this in mind, which do you prefer? Or perhaps there is a third approach I'm not considering? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, you're the customer: as the person who wants to replace text in multiple files, which behaviour would be more convenient or natural to you? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stopping at the first error seems more natural to me. I'll update my implementation accordingly. |
||
} | ||
count := strings.Count(s, search) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's nice to have a count of the number of replacements, but not essential—and the key to a good example is eliminating all non-essential detail. I don't deny that we probably would want a real |
||
_, err = Echo(strings.ReplaceAll(s, search, replace)).WriteFile(file) | ||
if err != nil { | ||
return fmt.Sprintf("%s %s", file, err) | ||
} | ||
return fmt.Sprintf("%s %d", file, count) | ||
}) | ||
// Output: | ||
// a.csv 3 | ||
// b.csv 0 | ||
``` | ||
|
||
And there's more. Much more. [Read the docs](https://pkg.go.dev/github.com/bitfield/script) for full details, and more examples. | ||
|
||
# A realistic use case | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if referencing CSV files is a red herring here. Readers might think this example is something to do with CSV parsing. Should we just talk about making arbitrary edits to all files matching a pattern?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for disappearing for awhile! Yes I think that's better. I'll make that change.