-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BUGFIX-11: Fix breaking cmd.exe on windows platform (#14)
BUGFIX-11: Fix breaking cmd.exe on windows platform
- Loading branch information
1 parent
41df624
commit 6f25b1c
Showing
10 changed files
with
376 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
//go:build !windows | ||
|
||
package ssh | ||
|
||
func BaseCMD() string { | ||
return "ssh" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
//go:build windows | ||
|
||
package ssh | ||
|
||
func BaseCMD() string { | ||
return "cmd /c ssh" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package ssh | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type CommandLineOption interface{} | ||
|
||
type ( | ||
OptionPrivateKey struct{ Value string } | ||
OptionRemotePort struct{ Value string } | ||
OptionLoginName struct{ Value string } | ||
OptionAddress struct{ Value string } | ||
) | ||
|
||
func constructKeyValueOption(optionFlag, optionValue string) string { | ||
optionValue = strings.TrimSpace(optionValue) | ||
if optionValue != "" { | ||
return fmt.Sprintf(" %s %s", optionFlag, optionValue) | ||
} | ||
return "" | ||
} | ||
|
||
func addOption(sb *strings.Builder, rawParameter CommandLineOption) { | ||
var option string | ||
switch p := rawParameter.(type) { | ||
case OptionPrivateKey: | ||
option = constructKeyValueOption("-i", p.Value) | ||
case OptionRemotePort: | ||
option = constructKeyValueOption("-p", p.Value) | ||
case OptionLoginName: | ||
option = constructKeyValueOption("-l", p.Value) | ||
case OptionAddress: | ||
if p.Value != "" { | ||
option = fmt.Sprintf(" %s", p.Value) | ||
} | ||
default: | ||
return | ||
} | ||
|
||
sb.WriteString(option) | ||
} | ||
|
||
// ConstructCMD - build connect command from main app and its arguments | ||
// cmd - main executable | ||
// options - set of command line options. See Option... public variables. | ||
func ConstructCMD(cmd string, options ...CommandLineOption) string { | ||
sb := strings.Builder{} | ||
sb.WriteString(cmd) | ||
|
||
for _, argument := range options { | ||
addOption(&sb, argument) | ||
} | ||
|
||
return sb.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package ssh | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func Test_ConstructKeyValueOption(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
optionFlag string | ||
optionValue string | ||
expectedResult string | ||
}{ | ||
{ | ||
name: "Option with value", | ||
optionFlag: "-i", | ||
optionValue: "private_key", | ||
expectedResult: " -i private_key", | ||
}, | ||
{ | ||
name: "Option with empty value", | ||
optionFlag: "-p", | ||
optionValue: "", | ||
expectedResult: "", | ||
}, | ||
{ | ||
name: "Option with space-padded value", | ||
optionFlag: "-l", | ||
optionValue: " login_name ", | ||
expectedResult: " -l login_name", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := constructKeyValueOption(tt.optionFlag, tt.optionValue) | ||
|
||
if result != tt.expectedResult { | ||
t.Errorf("Expected result %s, but got %s", tt.expectedResult, result) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_AddOption(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
rawParameter CommandLineOption | ||
expectedResult string | ||
}{ | ||
{ | ||
name: "OptionPrivateKey with value", | ||
rawParameter: OptionPrivateKey{Value: "private_key"}, | ||
expectedResult: " -i private_key", | ||
}, | ||
{ | ||
name: "OptionRemotePort with empty value", | ||
rawParameter: OptionRemotePort{Value: ""}, | ||
expectedResult: "", | ||
}, | ||
{ | ||
name: "OptionLoginName with value", | ||
rawParameter: OptionLoginName{Value: "login_name"}, | ||
expectedResult: " -l login_name", | ||
}, | ||
{ | ||
name: "OptionAddress with empty value", | ||
rawParameter: OptionAddress{Value: ""}, | ||
expectedResult: "", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var sb strings.Builder | ||
addOption(&sb, tt.rawParameter) | ||
|
||
result := sb.String() | ||
if result != tt.expectedResult { | ||
t.Errorf("Expected result %s, but got %s", tt.expectedResult, result) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_ConstructCMD(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
cmd string | ||
options []CommandLineOption | ||
expectedResult string | ||
}{ | ||
{ | ||
name: "Command with Options", | ||
cmd: "ssh", | ||
options: []CommandLineOption{OptionPrivateKey{Value: "private_key"}, OptionRemotePort{Value: "22"}}, | ||
expectedResult: "ssh -i private_key -p 22", | ||
}, | ||
{ | ||
name: "Command without Options", | ||
cmd: "ls", | ||
options: []CommandLineOption{}, | ||
expectedResult: "ls", | ||
}, | ||
{ | ||
name: "Command with Address Option", | ||
cmd: "ping", | ||
options: []CommandLineOption{OptionAddress{Value: "example.com"}}, | ||
expectedResult: "ping example.com", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := ConstructCMD(tt.cmd, tt.options...) | ||
|
||
if result != tt.expectedResult { | ||
t.Errorf("Expected result %s, but got %s", tt.expectedResult, result) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.