0
0
mirror of https://github.com/thegeeklab/wp-ansible.git synced 2024-06-02 08:19:40 +02:00
This commit is contained in:
Robert Kaussow 2024-05-06 14:38:05 +02:00
parent a8ec6f2d6b
commit 2b38baa763
Signed by: xoxys
GPG Key ID: 4E692A2EAECC03C0
5 changed files with 6 additions and 68 deletions

2
go.mod
View File

@ -6,7 +6,7 @@ require (
github.com/rs/zerolog v1.32.0
github.com/stretchr/testify v1.9.0
github.com/thegeeklab/wp-plugin-go v1.8.0
github.com/thegeeklab/wp-plugin-go/v2 v2.2.0
github.com/thegeeklab/wp-plugin-go/v2 v2.3.0
github.com/urfave/cli/v2 v2.27.2
golang.org/x/sys v0.20.0
)

4
go.sum
View File

@ -49,8 +49,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/thegeeklab/wp-plugin-go v1.8.0 h1:hmXXMRYUauSKN7QaWsQBzufzGVfrViJaDJisKh7JeKU=
github.com/thegeeklab/wp-plugin-go v1.8.0/go.mod h1:OvXWizBaHdZ77KTQKmJI6ssg33dy3eoG0t81rt5AgfY=
github.com/thegeeklab/wp-plugin-go/v2 v2.2.0 h1:Z6UzL8N0v3J2uuk67DBnH19QNV1vXihaKC2OoH2TMAY=
github.com/thegeeklab/wp-plugin-go/v2 v2.2.0/go.mod h1:I/3M/4OPvr4FFS+s0aaImpX1llA/lS2KC6Bnp+qzsCs=
github.com/thegeeklab/wp-plugin-go/v2 v2.3.0 h1:9LOdITzjxEEbgcH9yU0tDZL0dcDZzNYzbk2+hZ5QsBA=
github.com/thegeeklab/wp-plugin-go/v2 v2.3.0/go.mod h1:I/3M/4OPvr4FFS+s0aaImpX1llA/lS2KC6Bnp+qzsCs=
github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI=
github.com/urfave/cli/v2 v2.27.2/go.mod h1:g0+79LmHHATl7DAcHO99smiR/T7uGLw84w8Y42x+4eM=
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 h1:+qGGcbkzsfDQNPPe9UDgpxAWQrhbbBXOYJFQDq/dtJw=

View File

@ -5,6 +5,7 @@ import (
"fmt"
"os"
"github.com/thegeeklab/wp-plugin-go/v2/file"
"github.com/thegeeklab/wp-plugin-go/v2/types"
)
@ -38,7 +39,7 @@ func (p *Plugin) Execute() error {
batchCmd = append(batchCmd, p.Settings.Ansible.Version())
if p.Settings.PrivateKey != "" {
if p.Settings.Ansible.PrivateKeyFile, err = WriteFile("privateKey", p.Settings.PrivateKey); err != nil {
if p.Settings.Ansible.PrivateKeyFile, err = file.WriteTmpFile("privateKey", p.Settings.PrivateKey); err != nil {
return err
}
@ -46,7 +47,7 @@ func (p *Plugin) Execute() error {
}
if p.Settings.VaultPassword != "" {
if p.Settings.Ansible.VaultPasswordFile, err = WriteFile("vaultPass", p.Settings.VaultPassword); err != nil {
if p.Settings.Ansible.VaultPasswordFile, err = file.WriteTmpFile("vaultPass", p.Settings.VaultPassword); err != nil {
return err
}

View File

@ -1,9 +1,6 @@
package plugin
import (
"fmt"
"os"
"github.com/thegeeklab/wp-plugin-go/v2/types"
"golang.org/x/sys/execabs"
)
@ -24,21 +21,3 @@ func PipInstall(req string) *types.Cmd {
Cmd: execabs.Command(pipBin, args...),
}
}
// WriteFile creates a temporary file with the given name and content, and returns the path to the created file.
func WriteFile(name, content string) (string, error) {
tmpfile, err := os.CreateTemp("", name)
if err != nil {
return "", fmt.Errorf("failed to create file: %w", err)
}
if _, err := tmpfile.Write([]byte(content)); err != nil {
return "", fmt.Errorf("failed to write file: %w", err)
}
if err := tmpfile.Close(); err != nil {
return "", fmt.Errorf("failed to close file: %w", err)
}
return tmpfile.Name(), nil
}

View File

@ -1,7 +1,6 @@
package plugin
import (
"os"
"testing"
"github.com/stretchr/testify/require"
@ -27,44 +26,3 @@ func TestPipInstall(t *testing.T) {
})
}
}
func TestWriteFile(t *testing.T) {
tests := []struct {
name string
filename string
content string
wantErr bool
}{
{
name: "successful write",
filename: "test.txt",
content: "test content",
wantErr: false,
},
{
name: "empty content",
filename: "test.txt",
content: "",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path, err := WriteFile(tt.filename, tt.content)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
defer os.Remove(path)
content, err := os.ReadFile(path)
require.NoError(t, err)
require.Equal(t, tt.content, string(content))
})
}
}