mirror of
https://github.com/appleboy/drone-scp.git
synced 2026-06-04 18:23:59 +08:00
refactor: refactor host configuration handling
- Replace the iteration over `p.Config.Host` with `trimValues(p.Config.Host)` - Replace `len(p.Config.Host)` with `len(hosts)` - Replace iteration over `p.Config.Host` with `for _, host := range hosts` - Add `trimValues` function to trim spaces and empty values of a string slice Signed-off-by: Bo-Yi.Wu <appleboy.tw@gmail.com>
This commit is contained in:
@@ -165,7 +165,7 @@ func (p *Plugin) removeDestFile(os string, ssh *easyssh.MakeConfig) error {
|
||||
}
|
||||
|
||||
func (p *Plugin) removeAllDestFile() error {
|
||||
for _, host := range p.Config.Host {
|
||||
for _, host := range trimValues(p.Config.Host) {
|
||||
ssh := &easyssh.MakeConfig{
|
||||
Server: host,
|
||||
User: p.Config.Username,
|
||||
@@ -251,7 +251,8 @@ func (p *Plugin) buildArgs(target string) []string {
|
||||
|
||||
// Exec executes the plugin.
|
||||
func (p *Plugin) Exec() error {
|
||||
if len(p.Config.Host) == 0 {
|
||||
hosts := trimValues(p.Config.Host)
|
||||
if len(hosts) == 0 {
|
||||
return errMissingHost
|
||||
}
|
||||
|
||||
@@ -291,8 +292,9 @@ func (p *Plugin) Exec() error {
|
||||
wg.Add(len(p.Config.Host))
|
||||
errChannel := make(chan error)
|
||||
finished := make(chan struct{})
|
||||
for _, host := range p.Config.Host {
|
||||
for _, host := range hosts {
|
||||
go func(host string) {
|
||||
defer wg.Done()
|
||||
// Create MakeConfig instance with remote username, server address and path to private key.
|
||||
ssh := &easyssh.MakeConfig{
|
||||
Server: host,
|
||||
@@ -395,8 +397,6 @@ func (p *Plugin) Exec() error {
|
||||
errChannel <- err
|
||||
return
|
||||
}
|
||||
|
||||
wg.Done()
|
||||
}(host)
|
||||
}
|
||||
|
||||
@@ -427,3 +427,18 @@ func (p *Plugin) Exec() error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func trimValues(keys []string) []string {
|
||||
var newKeys []string
|
||||
|
||||
for _, value := range keys {
|
||||
value = strings.TrimSpace(value)
|
||||
if len(value) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
newKeys = append(newKeys, value)
|
||||
}
|
||||
|
||||
return newKeys
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user