0
0
mirror of https://github.com/thegeeklab/wp-git-action.git synced 2024-11-14 09:00:42 +00:00
wp-git-action/plugin/util.go

46 lines
889 B
Go
Raw Normal View History

package plugin
import (
2024-05-06 18:29:57 +00:00
"fmt"
"os"
"path/filepath"
plugin_exec "github.com/thegeeklab/wp-plugin-go/v3/exec"
)
2024-05-06 18:29:57 +00:00
const (
netrcFile = `machine %s
login %s
password %s
`
)
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-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-06 18:29:57 +00:00
return nil
}
// ExecBatch executes a batch of commands. If any command in the batch fails, the function will return the error.
func ExecBatch(batchCmd []*plugin_exec.Cmd) error {
for _, cmd := range batchCmd {
if cmd == nil {
continue
}
if err := cmd.Run(); err != nil {
return err
}
}
return nil
}