mirror of
https://github.com/thegeeklab/wp-ansible.git
synced 2024-06-02 08:19:40 +02:00
cleanup
This commit is contained in:
parent
a8ec6f2d6b
commit
2b38baa763
2
go.mod
2
go.mod
|
@ -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
4
go.sum
|
@ -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=
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user