Skip to content

Commit f823723

Browse files
authored
feat: support to write string to file (#339)
Co-authored-by: rick <[email protected]>
1 parent 4211b93 commit f823723

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

pkg/os/generic_installer.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package os
33
import (
44
"bytes"
55
"fmt"
6+
"io/fs"
67
"os"
8+
"path"
9+
"strconv"
710
"strings"
811
"text/template"
912

@@ -59,6 +62,28 @@ type CmdWithArgs struct {
5962
Cmd string `yaml:"cmd"`
6063
Args []string `yaml:"args"`
6164
SystemCall bool `yaml:"systemCall"`
65+
WriteTo *WriteTo `yaml:"writeTo"`
66+
}
67+
68+
// WriteTo represents the task to write content to file
69+
type WriteTo struct {
70+
File string
71+
Mod string
72+
Content string
73+
}
74+
75+
// Write writes content to file
76+
func (w *WriteTo) Write() (err error) {
77+
var mod int
78+
if mod, err = strconv.Atoi(w.Mod); err != nil {
79+
mod = 0644
80+
}
81+
82+
parent := path.Dir(w.File)
83+
if err = os.MkdirAll(parent, 0644); err == nil {
84+
err = os.WriteFile(w.File, []byte(w.Content), fs.FileMode(mod))
85+
}
86+
return
6287
}
6388

6489
func parseGenericPackages(configFile string, genericPackages *genericPackages) (err error) {
@@ -172,11 +197,19 @@ func (i *genericPackage) Install() (err error) {
172197
}
173198

174199
if needInstall {
175-
preInstall.Cmd.Args = i.sliceReplace(preInstall.Cmd.Args)
176-
fmt.Println(preInstall.Cmd.Args)
200+
if preInstall.Cmd.WriteTo != nil {
201+
if err = preInstall.Cmd.WriteTo.Write(); err != nil {
202+
return
203+
}
204+
}
177205

178-
if err = i.execer.RunCommand(preInstall.Cmd.Cmd, preInstall.Cmd.Args...); err != nil {
179-
return
206+
if preInstall.Cmd.Cmd != "" {
207+
preInstall.Cmd.Args = i.sliceReplace(preInstall.Cmd.Args)
208+
fmt.Println(preInstall.Cmd.Args)
209+
210+
if err = i.execer.RunCommand(preInstall.Cmd.Cmd, preInstall.Cmd.Args...); err != nil {
211+
return
212+
}
180213
}
181214
}
182215
}

pkg/os/generic_installer_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package os
22

33
import (
44
"errors"
5+
"io"
6+
"os"
57
"testing"
68

79
"github.com/linuxsuren/http-downloader/pkg/exec"
@@ -77,4 +79,29 @@ func TestURLReplace(t *testing.T) {
7779
assert.NotNil(t, withErrorPreInstall.Install())
7880
assert.NotNil(t, withErrorPreInstall.Uninstall())
7981
assert.True(t, withErrorPreInstall.Available())
82+
83+
tmpFile, err := os.CreateTemp(os.TempDir(), "installer")
84+
assert.Nil(t, err)
85+
defer func() {
86+
os.Remove(tmpFile.Name())
87+
}()
88+
writeToFileInstall := &genericPackage{
89+
execer: exec.FakeExecer{
90+
ExpectOS: exec.OSLinux,
91+
},
92+
PreInstall: []preInstall{{
93+
Cmd: CmdWithArgs{
94+
WriteTo: &WriteTo{
95+
File: tmpFile.Name(),
96+
Content: "sample",
97+
},
98+
},
99+
}},
100+
CommonInstaller: fake.NewFakeInstaller(true, false),
101+
}
102+
err = writeToFileInstall.Install()
103+
assert.Nil(t, err)
104+
data, err := io.ReadAll(tmpFile)
105+
assert.Nil(t, err)
106+
assert.Equal(t, "sample", string(data))
80107
}

0 commit comments

Comments
 (0)