Skip to content

Commit

Permalink
[refactor-runner-adxt-783] copy*file: Moved implementation to commands
Browse files Browse the repository at this point in the history
  • Loading branch information
CelianR committed Dec 23, 2024
1 parent a6e0c8e commit 8654e05
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 68 deletions.
2 changes: 2 additions & 0 deletions components/command/osCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type OSCommand interface {
IsPathAbsolute(path string) bool

NewCopyFile(runner Runner, name string, localPath, remotePath pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error)
copyLocalFile(runner *LocalRunner, name string, src, dst pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error)
copyRemoteFile(runner *RemoteRunner, name string, src, dst pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error)
}

// ------------------------------
Expand Down
68 changes: 2 additions & 66 deletions components/command/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package command

import (
"fmt"
"path/filepath"

"github.com/pulumi/pulumi-command/sdk/go/command/local"
"github.com/pulumi/pulumi-command/sdk/go/command/remote"
Expand Down Expand Up @@ -111,8 +110,6 @@ type Runner interface {
Command(name string, args *Args, opts ...pulumi.ResourceOption) (Command, error)

newCopyFile(name string, localPath, remotePath pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error)
copyWindowsFile(name string, src, dst pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error)
copyUnixFile(name string, src, dst pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error)
}

var _ Runner = &RemoteRunner{}
Expand Down Expand Up @@ -202,7 +199,7 @@ func (r *RemoteRunner) Command(name string, args *Args, opts ...pulumi.ResourceO
}

func (r *RemoteRunner) newCopyFile(name string, localPath, remotePath pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) {
return r.osCommand.NewCopyFile(r, name, localPath, remotePath, opts...)
return r.osCommand.copyRemoteFile(r, name, localPath, remotePath, opts...)
}

func (r *RemoteRunner) PulumiOptions() []pulumi.ResourceOption {
Expand Down Expand Up @@ -267,70 +264,9 @@ func (r *LocalRunner) Command(name string, args *Args, opts ...pulumi.ResourceOp
}

func (r *LocalRunner) newCopyFile(name string, localPath, remotePath pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) {
return r.osCommand.NewCopyFile(r, name, localPath, remotePath, opts...)
return r.osCommand.copyLocalFile(r, name, localPath, remotePath, opts...)
}

func (r *LocalRunner) PulumiOptions() []pulumi.ResourceOption {
return []pulumi.ResourceOption{}
}

func (r *LocalRunner) copyWindowsFile(name string, src, dst pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) {
createCmd := pulumi.Sprintf("Copy-Item -Path '%v' -Destination '%v'", src, dst)
deleteCmd := pulumi.Sprintf("Remove-Item -Path '%v'", dst)
useSudo := false

return r.Command(name,
&Args{
Create: createCmd,
Delete: deleteCmd,
Sudo: useSudo,
Triggers: pulumi.Array{createCmd, deleteCmd, pulumi.BoolPtr(useSudo)},
}, opts...)
}

func (r *LocalRunner) copyUnixFile(name string, src, dst pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) {
createCmd := pulumi.Sprintf("cp '%v' '%v'", src, dst)
deleteCmd := pulumi.Sprintf("rm '%v'", dst)
useSudo := false

return r.Command(name,
&Args{
Create: createCmd,
Delete: deleteCmd,
Sudo: useSudo,
Triggers: pulumi.Array{createCmd, deleteCmd, pulumi.BoolPtr(useSudo)},
}, opts...)
}

func (r *RemoteRunner) copyWindowsFile(name string, src, dst pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) {
return remote.NewCopyFile(r.Environment().Ctx(), r.Namer().ResourceName("copy", name), &remote.CopyFileArgs{
Connection: r.Config().connection,
LocalPath: src,
RemotePath: dst,
Triggers: pulumi.Array{src, dst},
}, utils.MergeOptions(r.PulumiOptions(), opts...)...)
}

func (r *RemoteRunner) copyUnixFile(name string, src, dst pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) {
tempRemotePath := src.ToStringOutput().ApplyT(func(path string) string {
return filepath.Join(r.OsCommand().GetTemporaryDirectory(), filepath.Base(path))
}).(pulumi.StringOutput)

tempCopyFile, err := remote.NewCopyFile(r.Environment().Ctx(), r.Namer().ResourceName("copy", name), &remote.CopyFileArgs{
Connection: r.Config().connection,
LocalPath: src,
RemotePath: tempRemotePath,
Triggers: pulumi.Array{src, tempRemotePath},
}, utils.MergeOptions(r.PulumiOptions(), opts...)...)

if err != nil {
return nil, err
}

moveCommand, err := r.OsCommand().MoveFile(r, name, tempRemotePath, dst, true, utils.MergeOptions(opts, utils.PulumiDependsOn(tempCopyFile))...)
if err != nil {
return nil, err
}

return moveCommand, err
}
42 changes: 41 additions & 1 deletion components/command/unixOSCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package command

import (
"fmt"
"path/filepath"
"strings"

"github.com/DataDog/test-infra-definitions/common/utils"

"github.com/alessio/shellescape"
"github.com/pulumi/pulumi-command/sdk/go/command/remote"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

Expand Down Expand Up @@ -71,7 +73,7 @@ func (fs unixOSCommand) IsPathAbsolute(path string) bool {
}

func (fs unixOSCommand) NewCopyFile(runner Runner, name string, localPath, remotePath pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) {
return runner.copyUnixFile(name, localPath, remotePath, opts...)
return runner.newCopyFile(name, localPath, remotePath, opts...)
}

func formatCommandIfNeeded(command pulumi.StringInput, sudo bool, password bool, user string) pulumi.StringInput {
Expand Down Expand Up @@ -102,3 +104,41 @@ func (fs unixOSCommand) MoveFile(runner Runner, name string, source, destination
deleteCommand := pulumi.Sprintf(`bash -c 'if [ -f "%v" ]; then mv -f "%v" "%v"; else rm -f "%v"; fi'`, backupPath, backupPath, destination, destination)
return copyRemoteFile(runner, fmt.Sprintf("move-file-%s", name), createCommand, deleteCommand, sudo, utils.MergeOptions(opts, pulumi.ReplaceOnChanges([]string{"*"}), pulumi.DeleteBeforeReplace(true))...)
}

func (fs unixOSCommand) copyLocalFile(runner *LocalRunner, name string, src, dst pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) {
createCmd := pulumi.Sprintf("cp '%v' '%v'", src, dst)
deleteCmd := pulumi.Sprintf("rm '%v'", dst)
useSudo := false

return runner.Command(name,
&Args{
Create: createCmd,
Delete: deleteCmd,
Sudo: useSudo,
Triggers: pulumi.Array{createCmd, deleteCmd, pulumi.BoolPtr(useSudo)},
}, opts...)
}

func (fs unixOSCommand) copyRemoteFile(runner *RemoteRunner, name string, src, dst pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) {
tempRemotePath := src.ToStringOutput().ApplyT(func(path string) string {
return filepath.Join(runner.OsCommand().GetTemporaryDirectory(), filepath.Base(path))
}).(pulumi.StringOutput)

tempCopyFile, err := remote.NewCopyFile(runner.Environment().Ctx(), runner.Namer().ResourceName("copy", name), &remote.CopyFileArgs{
Connection: runner.Config().connection,
LocalPath: src,
RemotePath: tempRemotePath,
Triggers: pulumi.Array{src, tempRemotePath},
}, utils.MergeOptions(runner.PulumiOptions(), opts...)...)

if err != nil {
return nil, err
}

moveCommand, err := runner.OsCommand().MoveFile(runner, name, tempRemotePath, dst, true, utils.MergeOptions(opts, utils.PulumiDependsOn(tempCopyFile))...)
if err != nil {
return nil, err
}

return moveCommand, err
}
26 changes: 25 additions & 1 deletion components/command/windowsOSCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/DataDog/test-infra-definitions/common/utils"

"github.com/pulumi/pulumi-command/sdk/go/command/remote"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

Expand Down Expand Up @@ -78,7 +79,7 @@ func (fs windowsOSCommand) IsPathAbsolute(path string) bool {
}

func (fs windowsOSCommand) NewCopyFile(runner Runner, name string, localPath, remotePath pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) {
return runner.copyWindowsFile(name, localPath, remotePath, opts...)
return runner.newCopyFile(name, localPath, remotePath, opts...)
}

func (fs windowsOSCommand) MoveFile(runner Runner, name string, source, destination pulumi.StringInput, sudo bool, opts ...pulumi.ResourceOption) (Command, error) {
Expand All @@ -88,3 +89,26 @@ func (fs windowsOSCommand) MoveFile(runner Runner, name string, source, destinat
deleteCommand := pulumi.Sprintf(`if (Test-Path '%v') { Move-Item -Force -Path '%v' -Destination '%v' } else { Remove-Item -Force -Path %v }`, backupPath, backupPath, destination, destination)
return copyRemoteFile(runner, fmt.Sprintf("move-file-%s", name), createCommand, deleteCommand, sudo, utils.MergeOptions(opts, pulumi.ReplaceOnChanges([]string{"*"}), pulumi.DeleteBeforeReplace(true))...)
}

func (fs windowsOSCommand) copyLocalFile(runner *LocalRunner, name string, src, dst pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) {
createCmd := pulumi.Sprintf("Copy-Item -Path '%v' -Destination '%v'", src, dst)
deleteCmd := pulumi.Sprintf("Remove-Item -Path '%v'", dst)
useSudo := false

return runner.Command(name,
&Args{
Create: createCmd,
Delete: deleteCmd,
Sudo: useSudo,
Triggers: pulumi.Array{createCmd, deleteCmd, pulumi.BoolPtr(useSudo)},
}, opts...)
}

func (fs windowsOSCommand) copyRemoteFile(runner *RemoteRunner, name string, src, dst pulumi.StringInput, opts ...pulumi.ResourceOption) (pulumi.Resource, error) {
return remote.NewCopyFile(runner.Environment().Ctx(), runner.Namer().ResourceName("copy", name), &remote.CopyFileArgs{
Connection: runner.Config().connection,
LocalPath: src,
RemotePath: dst,
Triggers: pulumi.Array{src, dst},
}, utils.MergeOptions(runner.PulumiOptions(), opts...)...)
}

0 comments on commit 8654e05

Please sign in to comment.