Skip to content

Commit 578e5e0

Browse files
committed
feat(pkg/task/replace_string_in_local_file): Add TaskReplaceStringInLocalFile
1 parent 563812e commit 578e5e0

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package replace_string_in_local_file
2+
3+
import (
4+
"io/ioutil"
5+
"strings"
6+
7+
"github.com/sikalabs/gobble/pkg/libtask"
8+
)
9+
10+
type TaskReplaceStringInLocalFile struct {
11+
Path string `yaml:"path"`
12+
Find string `yaml:"find"`
13+
Replace string `yaml:"replace"`
14+
}
15+
16+
func Run(
17+
taskInput libtask.TaskInput,
18+
taskParams TaskReplaceStringInLocalFile,
19+
) libtask.TaskOutput {
20+
err := replaceStringInFile(taskParams.Path, taskParams.Find, taskParams.Replace)
21+
return libtask.TaskOutput{
22+
Error: err,
23+
}
24+
}
25+
26+
func replaceStringInFile(filename, searchStr, replaceStr string) error {
27+
// read the entire file into memory
28+
content, err := ioutil.ReadFile(filename)
29+
if err != nil {
30+
return err
31+
}
32+
33+
// replace the string
34+
newContent := strings.ReplaceAll(string(content), searchStr, replaceStr)
35+
36+
// write the updated content back to the file
37+
err = ioutil.WriteFile(filename, []byte(newContent), 0644)
38+
if err != nil {
39+
return err
40+
}
41+
42+
return nil
43+
}

pkg/task/task.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@ import (
1010
"github.com/sikalabs/gobble/pkg/task/lib/command"
1111
"github.com/sikalabs/gobble/pkg/task/lib/cp"
1212
"github.com/sikalabs/gobble/pkg/task/lib/print"
13+
rsilf "github.com/sikalabs/gobble/pkg/task/lib/replace_string_in_local_file"
1314
"github.com/sikalabs/gobble/pkg/task/lib/template"
1415
)
1516

1617
type Task struct {
17-
Name string `yaml:"name"`
18-
AptInstall apt_install.TaskAptInstall `yaml:"apt_install"`
19-
Cp cp.TaskCp `yaml:"cp"`
20-
Template template.TaskTemplate `yaml:"template"`
21-
Command command.TaskCommand `yaml:"command"`
22-
Chmod chmod.TaskChmod `yaml:"chmod"`
23-
Print print.TaskPrint `yaml:"print"`
24-
AuthorizedKey authorized_key.TaskAuthorizedKey `yaml:"authorized_key"`
18+
Name string `yaml:"name"`
19+
AptInstall apt_install.TaskAptInstall `yaml:"apt_install"`
20+
Cp cp.TaskCp `yaml:"cp"`
21+
Template template.TaskTemplate `yaml:"template"`
22+
Command command.TaskCommand `yaml:"command"`
23+
Chmod chmod.TaskChmod `yaml:"chmod"`
24+
Print print.TaskPrint `yaml:"print"`
25+
AuthorizedKey authorized_key.TaskAuthorizedKey `yaml:"authorized_key"`
26+
TaskReplaceStringInLocalFile rsilf.TaskReplaceStringInLocalFile `yaml:"replace_string_in_local_file"`
2527
}
2628

2729
func Run(
@@ -43,6 +45,8 @@ func Run(
4345
return print.Run(taskInput, task.Print)
4446
case task.AuthorizedKey.Key != "":
4547
return authorized_key.Run(taskInput, task.AuthorizedKey)
48+
case task.TaskReplaceStringInLocalFile.Path != "":
49+
return rsilf.Run(taskInput, task.TaskReplaceStringInLocalFile)
4650
}
4751
return libtask.TaskOutput{
4852
Error: fmt.Errorf("task \"%s\" not found", task.Name),

0 commit comments

Comments
 (0)