From 244b0a9e58a4256de1b974701414763735dbc06c Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sun, 9 Apr 2023 08:14:05 +0800 Subject: [PATCH] refactor: handling functions for cross-platform compatibility (#166) - Change the `rmcmd` and `mkdircmd` functions to accept an OS parameter - Remove `command_windows.go` file - Modify `removeDestFile` and `Exec` functions to use the OS parameter - Add OS detection logic to `removeAllDestFile` and `Exec` functions - Modify `TestRemoveDestFile` function to use the OS parameter fix https://github.com/appleboy/drone-scp/pull/119 --- command.go | 24 +++++++++++++++++------- command_windows.go | 12 ------------ plugin.go | 36 +++++++++++++++++++++++++++++------- plugin_test.go | 15 +++++++++++++-- 4 files changed, 59 insertions(+), 28 deletions(-) delete mode 100644 command_windows.go diff --git a/command.go b/command.go index aa72a40..f7cdc6c 100644 --- a/command.go +++ b/command.go @@ -1,12 +1,22 @@ -//go:build !windows -// +build !windows - package main -func rmcmd(target string) string { - return "rm -rf " + target +func rmcmd(os, target string) string { + switch os { + case "windows": + return "DEL /F /S " + target + case "unix": + return "rm -rf " + target + } + return "" } -func mkdircmd(target string) string { - return "mkdir -p " + target +func mkdircmd(os, target string) string { + switch os { + case "windows": + return "if not exist " + target + " mkdir " + target + case "unix": + return "mkdir -p " + target + } + + return "" } diff --git a/command_windows.go b/command_windows.go deleted file mode 100644 index 06475b5..0000000 --- a/command_windows.go +++ /dev/null @@ -1,12 +0,0 @@ -//go:build windows -// +build windows - -package main - -func rmcmd(target string) string { - return "DEL /F /S " + target -} - -func mkdircmd(target string) string { - return "if not exist " + target + " mkdir " + target -} diff --git a/plugin.go b/plugin.go index 590b51a..45704a7 100644 --- a/plugin.go +++ b/plugin.go @@ -150,9 +150,9 @@ func (p Plugin) log(host string, message ...interface{}) { } } -func (p *Plugin) removeDestFile(ssh *easyssh.MakeConfig) error { +func (p *Plugin) removeDestFile(os string, ssh *easyssh.MakeConfig) error { p.log(ssh.Server, "remove file", p.DestFile) - _, errStr, _, err := ssh.Run(rmcmd(p.DestFile), p.Config.CommandTimeout) + _, errStr, _, err := ssh.Run(rmcmd(os, p.DestFile), p.Config.CommandTimeout) if err != nil { return err } @@ -193,8 +193,19 @@ func (p *Plugin) removeAllDestFile() error { }, } + _, _, _, err := ssh.Run("ver", p.Config.CommandTimeout) + systemType := "unix" + if err == nil { + systemType = "windows" + } + + _, _, _, err = ssh.Run("uname", p.Config.CommandTimeout) + if err == nil { + systemType = "unix" + } + // remove tar file - err := p.removeDestFile(ssh) + err = p.removeDestFile(systemType, ssh) if err != nil { return err } @@ -310,12 +321,23 @@ func (p *Plugin) Exec() error { }, } + _, _, _, err := ssh.Run("ver", p.Config.CommandTimeout) + systemType := "unix" + if err == nil { + systemType = "windows" + } + + _, _, _, err = ssh.Run("uname", p.Config.CommandTimeout) + if err == nil { + systemType = "unix" + } + // upload file to the tmp path p.DestFile = fmt.Sprintf("%s%s", p.Config.TarTmpPath, p.DestFile) // Call Scp method with file you want to upload to remote server. p.log(host, "scp file to server.") - err := ssh.Scp(tar, p.DestFile) + err = ssh.Scp(tar, p.DestFile) if err != nil { errChannel <- copyError{host, err.Error()} return @@ -326,7 +348,7 @@ func (p *Plugin) Exec() error { if p.Config.Remove { p.log(host, "Remove target folder:", target) - _, _, _, err := ssh.Run(rmcmd(target), p.Config.CommandTimeout) + _, _, _, err := ssh.Run(rmcmd(systemType, target), p.Config.CommandTimeout) if err != nil { errChannel <- err return @@ -334,7 +356,7 @@ func (p *Plugin) Exec() error { } p.log(host, "create folder", target) - _, errStr, _, err := ssh.Run(mkdircmd(target), p.Config.CommandTimeout) + _, errStr, _, err := ssh.Run(mkdircmd(systemType, target), p.Config.CommandTimeout) if err != nil { errChannel <- err return @@ -368,7 +390,7 @@ func (p *Plugin) Exec() error { } // remove tar file - err = p.removeDestFile(ssh) + err = p.removeDestFile(systemType, ssh) if err != nil { errChannel <- err return diff --git a/plugin_test.go b/plugin_test.go index 77d1a91..c333c86 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -607,14 +607,25 @@ func TestRemoveDestFile(t *testing.T) { DestFile: "/etc/resolv.conf", } + _, _, _, err := ssh.Run("ver", plugin.Config.CommandTimeout) + systemType := "unix" + if err == nil { + systemType = "windows" + } + + _, _, _, err = ssh.Run("uname", plugin.Config.CommandTimeout) + if err == nil { + systemType = "unix" + } + // ssh io timeout - err := plugin.removeDestFile(ssh) + err = plugin.removeDestFile(systemType, ssh) assert.Error(t, err) ssh.Timeout = 0 // permission denied - err = plugin.removeDestFile(ssh) + err = plugin.removeDestFile(systemType, ssh) assert.Error(t, err) }