2024-05-05 20:14:55 +00:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
2024-05-06 18:29:57 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-05-14 11:39:04 +00:00
|
|
|
|
2024-05-17 19:49:59 +00:00
|
|
|
plugin_exec "github.com/thegeeklab/wp-plugin-go/v3/exec"
|
2024-05-05 20:14:55 +00:00
|
|
|
)
|
|
|
|
|
2024-05-06 18:29:57 +00:00
|
|
|
const (
|
|
|
|
netrcFile = `machine %s
|
|
|
|
login %s
|
|
|
|
password %s
|
|
|
|
`
|
|
|
|
)
|
|
|
|
|
2024-05-06 20:17:45 +00:00
|
|
|
const strictFilePerm = 0o600
|
2024-05-06 18:29:57 +00:00
|
|
|
|
|
|
|
// WriteNetrc writes the netrc file.
|
|
|
|
func WriteNetrc(path, machine, login, password string) error {
|
|
|
|
netrcPath := filepath.Join(path, ".netrc")
|
|
|
|
netrcContent := fmt.Sprintf(netrcFile, machine, login, password)
|
2024-05-05 20:14:55 +00:00
|
|
|
|
2024-05-06 18:29:57 +00:00
|
|
|
if err := os.WriteFile(netrcPath, []byte(netrcContent), strictFilePerm); err != nil {
|
|
|
|
return fmt.Errorf("failed to create .netrc file: %w", err)
|
2024-05-05 20:14:55 +00:00
|
|
|
}
|
|
|
|
|
2024-05-06 18:29:57 +00:00
|
|
|
return nil
|
2024-05-05 20:14:55 +00:00
|
|
|
}
|
2024-05-14 11:39:04 +00:00
|
|
|
|
|
|
|
// ExecBatch executes a batch of commands. If any command in the batch fails, the function will return the error.
|
2024-05-17 19:49:59 +00:00
|
|
|
func ExecBatch(batchCmd []*plugin_exec.Cmd) error {
|
2024-05-14 11:39:04 +00:00
|
|
|
for _, cmd := range batchCmd {
|
|
|
|
if cmd == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|