Skip to content

Commit

Permalink
Merge pull request #16 from catatsuy/feature_refactor_order
Browse files Browse the repository at this point in the history
Refactor file processing function naming for clarity
  • Loading branch information
catatsuy authored Mar 30, 2024
2 parents 778178c + 4e949ac commit 8da8887
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
36 changes: 18 additions & 18 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (c *CLI) Run(args []string) int {
}

if isOverwrite && filePath == "" {
fmt.Fprintln(c.errStream, "Cannot use -i option with stdin")
fmt.Fprintln(c.errStream, "Cannot use -overwrite option with stdin")
return ExitCodeFail
}

Expand All @@ -59,13 +59,16 @@ func (c *CLI) Run(args []string) int {
return ExitCodeFail
}

delimiter := string(replaceExpr[0])
parts := regexp.MustCompile(regexp.QuoteMeta(delimiter)).Split(replaceExpr[1:], -1)
if len(parts) < 2 {
fmt.Fprintln(c.errStream, "Invalid replace expression format. Use \"@search@replace@\"")
return ExitCodeFail
if filePath != "" {
file, err := os.Open(filePath)
if err != nil {
fmt.Fprintf(c.errStream, "Failed to open file: %s\n", err)
return ExitCodeFail
}
defer file.Close()

c.inputStream = file
}
searchPattern, replacement := parts[0], parts[1]

var tmpFile *os.File

Expand All @@ -85,18 +88,15 @@ func (c *CLI) Run(args []string) int {
}
}

if filePath != "" {
file, err := os.Open(filePath)
if err != nil {
fmt.Fprintf(c.errStream, "Failed to open file: %s\n", err)
return ExitCodeFail
}
defer file.Close()

c.inputStream = file
delimiter := string(replaceExpr[0])
parts := regexp.MustCompile(regexp.QuoteMeta(delimiter)).Split(replaceExpr[1:], -1)
if len(parts) < 2 {
fmt.Fprintln(c.errStream, "Invalid replace expression format. Use \"@search@replace@\"")
return ExitCodeFail
}
searchPattern, replacement := parts[0], parts[1]

if err := c.processFiles(searchPattern, replacement); err != nil {
if err := c.replaceProcess(searchPattern, replacement); err != nil {
fmt.Fprintf(c.errStream, "Failed to process files: %s\n", err)
return ExitCodeFail
}
Expand All @@ -111,7 +111,7 @@ func (c *CLI) Run(args []string) int {
return ExitCodeOK
}

func (c *CLI) processFiles(searchPattern, replacement string) error {
func (c *CLI) replaceProcess(searchPattern, replacement string) error {
scanner := bufio.NewScanner(c.inputStream)

re, err := regexp.Compile(searchPattern)
Expand Down
4 changes: 2 additions & 2 deletions cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestProcessFiles_replace(t *testing.T) {

inputStream.WriteString("searchb searchc")

err := cl.ProcessFiles("search", "replacement")
err := cl.ReplaceProcess("search", "replacement")

if err != nil {
t.Errorf("Error=%q", err)
Expand All @@ -94,7 +94,7 @@ func TestProcessFiles_noMatch(t *testing.T) {

inputStream.WriteString("no match")

err := cl.ProcessFiles("search", "replacement")
err := cl.ReplaceProcess("search", "replacement")

if err != nil {
t.Errorf("Error=%q", err)
Expand Down
4 changes: 2 additions & 2 deletions cli/export_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package cli

func (c *CLI) ProcessFiles(searchPattern string, replacement string) error {
return c.processFiles(searchPattern, replacement)
func (c *CLI) ReplaceProcess(searchPattern string, replacement string) error {
return c.replaceProcess(searchPattern, replacement)
}

0 comments on commit 8da8887

Please sign in to comment.