Skip to content

Commit

Permalink
[refactor-runner-adxt-783] Tested and cleaned code
Browse files Browse the repository at this point in the history
  • Loading branch information
CelianR committed Dec 18, 2024
1 parent 2448f9d commit 74d4b02
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
9 changes: 9 additions & 0 deletions components/command/osCommand.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package command

import (
"runtime"
"strings"

"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
Expand Down Expand Up @@ -88,3 +89,11 @@ func copyRemoteFile(
Triggers: pulumi.Array{createCommand, deleteCommand, pulumi.BoolPtr(useSudo)},
}, opts...)
}

func NewLocalOSCommand() OSCommand {
if runtime.GOOS == "windows" {
return NewWindowsOSCommand()
} else {

Check failure on line 96 in components/command/osCommand.go

View workflow job for this annotation

GitHub Actions / lint-go

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)
return NewUnixOSCommand()
}
}
4 changes: 2 additions & 2 deletions components/command/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func (r *LocalRunner) PulumiOptions() []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 // TODO A
useSudo := false

return r.Command(name,
&Args{
Expand All @@ -292,7 +292,7 @@ func (r *LocalRunner) CopyWindowsFile(name string, src, dst pulumi.StringInput,
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 // TODO A
useSudo := false

return r.Command(name,
&Args{
Expand Down
6 changes: 3 additions & 3 deletions components/command/unixOSCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ func formatCommandIfNeeded(command pulumi.StringInput, sudo bool, password bool,

func (fs unixOSCommand) MoveRemoteFile(runner Runner, name string, source, destination pulumi.StringInput, sudo bool, opts ...pulumi.ResourceOption) (Command, error) {
backupPath := pulumi.Sprintf("%v.%s", destination, backupExtension)
copyCommand := pulumi.Sprintf(`cp '%v' '%v'`, source, destination)
createCommand := pulumi.Sprintf(`bash -c 'if [ -f '%v' ]; then mv -f '%v' '%v'; fi; %v'`, destination, destination, backupPath, copyCommand)
deleteCommand := pulumi.Sprintf(`bash -c 'if [ -f '%v' ]; then mv -f '%v' '%v'; else rm -f '%v'; fi'`, backupPath, backupPath, destination, destination)
copyCommand := pulumi.Sprintf(`cp "%v" "%v"`, source, destination)
createCommand := pulumi.Sprintf(`bash -c 'if [ -f "%v" ]; then mv -f "%v" "%v"; fi; %v'`, destination, destination, backupPath, copyCommand)
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))...)
}
6 changes: 3 additions & 3 deletions components/command/windowsOSCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ func (fs windowsOSCommand) NewCopyFile(runner Runner, name string, localPath, re

func (fs windowsOSCommand) MoveRemoteFile(runner Runner, name string, source, destination pulumi.StringInput, sudo bool, opts ...pulumi.ResourceOption) (Command, error) {
backupPath := pulumi.Sprintf("%v.%s", destination, backupExtension)
copyCommand := pulumi.Sprintf(`Copy-Item -Path %v -Destination %v`, source, destination)
createCommand := pulumi.Sprintf(`if (Test-Path %v) { Move-Item -Force -Path %v -Destination %v }; %v`, destination, destination, backupPath, copyCommand)
deleteCommand := pulumi.Sprintf(`if (Test-Path %v) { Move-Item -Force -Path %v -Destination %v } else { Remove-Item -Force -Path %v }`, backupPath, backupPath, destination, destination)
copyCommand := pulumi.Sprintf(`Copy-Item -Path '%v' -Destination '%v'`, source, destination)
createCommand := pulumi.Sprintf(`if (Test-Path '%v') { Move-Item -Force -Path '%v' -Destination '%v' }; %v`, destination, destination, backupPath, copyCommand)
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))...)
}
9 changes: 1 addition & 8 deletions resources/local/podman/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
_ "embed"
"os"
"path"
"runtime"

"github.com/DataDog/test-infra-definitions/common/utils"
"github.com/DataDog/test-infra-definitions/components/command"
Expand All @@ -22,13 +21,7 @@ var dockerfileContent string
var customDockerConfig = "{}"

func NewInstance(e resourceslocal.Environment, args VMArgs, opts ...pulumi.ResourceOption) (address pulumi.StringOutput, user string, port int, err error) {
var osCommand command.OSCommand
if runtime.GOOS == "windows" {
osCommand = command.NewWindowsOSCommand()
} else {
osCommand = command.NewUnixOSCommand()
}
runner := command.NewLocalRunner(&e, command.LocalRunnerArgs{OSCommand: osCommand})
runner := command.NewLocalRunner(&e, command.LocalRunnerArgs{OSCommand: command.NewLocalOSCommand()})
fileManager := command.NewFileManager(runner)

publicKey, err := os.ReadFile(e.DefaultPublicKeyPath())
Expand Down

0 comments on commit 74d4b02

Please sign in to comment.