Skip to content

Commit fd4b036

Browse files
committed
feat: auto install missing tools
1 parent 641ed06 commit fd4b036

File tree

3 files changed

+72
-17
lines changed

3 files changed

+72
-17
lines changed

cli/shell_runner.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,34 @@ func (s *ShellScript) runCmdLine(cmdLine, contextDir string, keepScripts bool) (
7171
s.ShellType = "bash"
7272
}
7373

74+
deps := map[string]string{
75+
s.ShellType: s.ShellType,
76+
}
77+
for _, cmd := range findPotentialCommands(cmdLine) {
78+
deps[cmd] = cmd
79+
}
80+
7481
is := installer.Installer{
7582
Provider: "github",
7683
}
77-
if err = is.CheckDepAndInstall(map[string]string{
78-
s.ShellType: s.ShellType,
79-
}); err == nil {
84+
if err = is.CheckDepAndInstall(deps); err == nil {
8085
err = s.Execer.RunCommandInDir(s.ShellType, contextDir, path.Base(shellFile))
8186
}
8287
}
8388
return
8489
}
8590

91+
func findPotentialCommands(cmdLine string) (cmds []string) {
92+
lines := strings.Split(cmdLine, "\n")
93+
for _, line := range lines {
94+
line = strings.TrimSpace(line)
95+
if items := strings.Split(line, " "); len(items) > 0 && items[0] != "" {
96+
cmds = append(cmds, items[0])
97+
}
98+
}
99+
return
100+
}
101+
86102
// GetTitle returns the title of this script
87103
func (s *ShellScript) GetTitle() string {
88104
return s.Title

cli/shell_runner_fuzz_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func FuzzInputRequest(f *testing.F) {
11+
f.Add("a", "b")
12+
f.Fuzz(func(t *testing.T, key, value string) {
13+
ok, pair, err := isInputRequest(fmt.Sprintf("%s=%s", key, value))
14+
assert.True(t, ok)
15+
assert.Equal(t, key, pair[0])
16+
assert.Equal(t, value, pair[1])
17+
assert.Nil(t, err)
18+
if err != nil {
19+
t.Fail()
20+
}
21+
})
22+
}

cli/shell_runner_test.go

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package cli
22

33
import (
4-
"fmt"
5-
"github.com/linuxsuren/http-downloader/pkg/exec"
64
"testing"
75

6+
"github.com/linuxsuren/http-downloader/pkg/exec"
7+
88
"github.com/stretchr/testify/assert"
99
)
1010

@@ -83,16 +83,33 @@ func TestIsInputRequest(t *testing.T) {
8383
}
8484
}
8585

86-
func FuzzInputRequest(f *testing.F) {
87-
f.Add("a", "b")
88-
f.Fuzz(func(t *testing.T, key, value string) {
89-
ok, pair, err := isInputRequest(fmt.Sprintf("%s=%s", key, value))
90-
assert.True(t, ok)
91-
assert.Equal(t, key, pair[0])
92-
assert.Equal(t, value, pair[1])
93-
assert.Nil(t, err)
94-
if err != nil {
95-
t.Fail()
96-
}
97-
})
86+
func TestFindPotentialCommands(t *testing.T) {
87+
tests := []struct {
88+
name string
89+
cmd string
90+
expect []string
91+
}{{
92+
name: "oneline cmd",
93+
cmd: "k3d create cluster",
94+
expect: []string{"k3d"},
95+
}, {
96+
name: "empty",
97+
cmd: "",
98+
expect: nil,
99+
}, {
100+
name: "multiple lines",
101+
cmd: `k3d create cluster
102+
docker ps`,
103+
expect: []string{"k3d", "docker"},
104+
}, {
105+
name: "with extra whitespace",
106+
cmd: " k3d create cluster",
107+
expect: []string{"k3d"},
108+
}}
109+
for _, tt := range tests {
110+
t.Run(tt.name, func(t *testing.T) {
111+
result := findPotentialCommands(tt.cmd)
112+
assert.Equal(t, tt.expect, result)
113+
})
114+
}
98115
}

0 commit comments

Comments
 (0)