diff --git a/README.md b/README.md index 82623e4..829f1aa 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ [![Codacy Badge](https://app.codacy.com/project/badge/Grade/5022a74d146f487581821fd1c3435437)](https://www.codacy.com/gh/LinuxSuRen/md-exec/dashboard?utm_source=github.com&utm_medium=referral&utm_content=LinuxSuRen/md-exec&utm_campaign=Badge_Grade) [![Codacy Badge](https://app.codacy.com/project/badge/Coverage/5022a74d146f487581821fd1c3435437)](https://www.codacy.com/gh/LinuxSuRen/md-exec/dashboard?utm_source=github.com&utm_medium=referral&utm_content=LinuxSuRen/md-exec&utm_campaign=Badge_Coverage) +![GitHub All Releases](https://img.shields.io/github/downloads/linuxsuren/md-exec/total) ## Usage `md-exec` could exec the commands in the Markdown files. diff --git a/cli/golang_runner.go b/cli/golang_runner.go index 83164eb..e004bc0 100644 --- a/cli/golang_runner.go +++ b/cli/golang_runner.go @@ -25,21 +25,18 @@ func (s *GolangScript) Run() (err error) { s.Content = strings.ReplaceAll(s.Content, "#!title: "+s.Title, "") var shellFile string - if shellFile, err = writeAsShell(fmt.Sprintf(sampleGo, s.Content), s.Dir); err != nil { - fmt.Println(err) - return - } + if shellFile, err = writeAsShell(fmt.Sprintf(sampleGo, s.Content), s.Dir); err == nil { + goSourceFile := fmt.Sprintf("%s.go", shellFile) + os.Rename(shellFile, goSourceFile) - goSourceFile := fmt.Sprintf("%s.go", shellFile) - os.Rename(shellFile, goSourceFile) + if !s.KeepScripts { + defer func() { + _ = os.RemoveAll(goSourceFile) + }() + } - if !s.KeepScripts { - defer func() { - _ = os.RemoveAll(goSourceFile) - }() + err = s.Execer.RunCommandInDir("go", s.Dir, "run", path.Base(goSourceFile)) } - - err = s.Execer.RunCommandInDir("go", s.Dir, "run", path.Base(goSourceFile)) return } diff --git a/cli/python_runner.go b/cli/python_runner.go index 420fbed..2d6a802 100644 --- a/cli/python_runner.go +++ b/cli/python_runner.go @@ -1,7 +1,6 @@ package cli import ( - "fmt" "os" "path" ) @@ -14,17 +13,15 @@ type PythonScript struct { // Run executes the script func (s *PythonScript) Run() (err error) { var shellFile string - if shellFile, err = writeAsShell(s.Content, s.Dir); err != nil { - fmt.Println(err) - return - } - if !s.KeepScripts { - defer func() { - _ = os.RemoveAll(shellFile) - }() - } + if shellFile, err = writeAsShell(s.Content, s.Dir); err == nil { + if !s.KeepScripts { + defer func() { + _ = os.RemoveAll(shellFile) + }() + } - err = s.Execer.RunCommandInDir("python3", s.Dir, path.Base(shellFile)) + err = s.Execer.RunCommandInDir("python3", s.Dir, path.Base(shellFile)) + } return } diff --git a/cli/root.go b/cli/root.go index 3e36c3b..e14cc7f 100644 --- a/cli/root.go +++ b/cli/root.go @@ -62,7 +62,9 @@ func (o *option) runE(cmd *cobra.Command, args []string) (err error) { if scriptRunners.Size() > 1 { for { - err = o.executeScripts(scriptRunners) + if err = o.executeScripts(scriptRunners); err != nil { + fmt.Fprintln(cmd.ErrOrStderr(), err.Error()) + } if !o.loop { break diff --git a/cli/root_test.go b/cli/root_test.go index 95a02b6..bfb6f87 100644 --- a/cli/root_test.go +++ b/cli/root_test.go @@ -40,3 +40,15 @@ func TestNewRootCommand(t *testing.T) { }) } } + +func TestParseMarkdownRUnner(t *testing.T) { + opt := &option{} + runners, err := opt.parseMarkdownRunner("../README.md") + if assert.Nil(t, err) { + assert.True(t, len(runners) > 0) + assert.NotNil(t, runners.GetRunner("Variable Input Hello World")) + assert.NotNil(t, runners.GetRunner("Python Hello World")) + assert.NotNil(t, runners.GetRunner("Run long time")) + assert.NotNil(t, runners.GetRunner("Golang Hello World")) + } +} diff --git a/cli/shell_runner.go b/cli/shell_runner.go index ab5d8f9..6c5278f 100644 --- a/cli/shell_runner.go +++ b/cli/shell_runner.go @@ -1,14 +1,14 @@ package cli import ( - "fmt" - "github.com/linuxsuren/http-downloader/pkg/installer" "io" "os" "path" "regexp" "strings" + "github.com/linuxsuren/http-downloader/pkg/installer" + "github.com/AlecAivazis/survey/v2" ) @@ -60,27 +60,25 @@ func (s *ShellScript) Run() (err error) { func (s *ShellScript) runCmdLine(cmdLine, contextDir string, keepScripts bool) (err error) { var shellFile string - if shellFile, err = writeAsShell(cmdLine, contextDir); err != nil { - fmt.Println(err) - return - } - if !keepScripts { - defer func() { - _ = os.RemoveAll(shellFile) - }() - } + if shellFile, err = writeAsShell(cmdLine, contextDir); err == nil { + if !keepScripts { + defer func() { + _ = os.RemoveAll(shellFile) + }() + } - if s.ShellType == "shell" || s.ShellType == "" { - s.ShellType = "bash" - } + if s.ShellType == "shell" || s.ShellType == "" { + s.ShellType = "bash" + } - is := installer.Installer{ - Provider: "github", - } - if err = is.CheckDepAndInstall(map[string]string{ - s.ShellType: s.ShellType, - }); err == nil { - err = s.Execer.RunCommandInDir(s.ShellType, contextDir, path.Base(shellFile)) + is := installer.Installer{ + Provider: "github", + } + if err = is.CheckDepAndInstall(map[string]string{ + s.ShellType: s.ShellType, + }); err == nil { + err = s.Execer.RunCommandInDir(s.ShellType, contextDir, path.Base(shellFile)) + } } return } diff --git a/cli/types_test.go b/cli/types_test.go new file mode 100644 index 0000000..065a4be --- /dev/null +++ b/cli/types_test.go @@ -0,0 +1,22 @@ +package cli + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestScriptRunners(t *testing.T) { + runners := ScriptRunners{} + assert.Equal(t, 0, runners.Size()) + assert.Nil(t, runners.GetRunner("fake")) + assert.Equal(t, []string{}, runners.GetTitles()) + + runners = NewScriptRunners() + quitRunner := runners.GetRunner("Quit") + if assert.NotNil(t, quitRunner) { + assert.Equal(t, "Quit", quitRunner.GetTitle()) + assert.Nil(t, quitRunner.Run()) + } + assert.Equal(t, []string{"Quit"}, runners.GetTitles()) +}