Skip to content

Commit

Permalink
SSH Command not parsing params at the end
Browse files Browse the repository at this point in the history
- Removed deprecated flag and added some tests.
  • Loading branch information
Carlos Cabanero committed Nov 15, 2023
1 parent 14afe33 commit 3cda061
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 36 deletions.
4 changes: 4 additions & 0 deletions Blink.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
BD028AF32A8EC509002F5F54 /* TrialSupportView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD028AF22A8EC509002F5F54 /* TrialSupportView.swift */; };
BD11E9E6270CD0FD003EA5AE /* openssl.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = D2F64C9425CA99AD00F2225D /* openssl.xcframework */; };
BD1758AC26EA8C5400AEC545 /* MenuController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD1758AB26EA8C5400AEC545 /* MenuController.swift */; };
BD19DB412B056E9C003A4367 /* SSHCommandTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD19DB402B056E9C003A4367 /* SSHCommandTest.swift */; };
BD2E27B529BAA8DA003AF1DA /* ReplaySubject.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD2E27B429BAA8DA003AF1DA /* ReplaySubject.swift */; };
BD3E1E53278D190500333C44 /* Archive.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD3E1E4F278D190500333C44 /* Archive.swift */; };
BD44DCE626D6BEAC00054338 /* BlinkItemIdentifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD44DCE526D6BEAC00054338 /* BlinkItemIdentifier.swift */; };
Expand Down Expand Up @@ -822,6 +823,7 @@
B7D6A6281E2D43A800EDF7B0 /* BKSmartKeysConfigViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BKSmartKeysConfigViewController.m; sourceTree = "<group>"; };
BD028AF22A8EC509002F5F54 /* TrialSupportView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TrialSupportView.swift; sourceTree = "<group>"; };
BD1758AB26EA8C5400AEC545 /* MenuController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuController.swift; sourceTree = "<group>"; };
BD19DB402B056E9C003A4367 /* SSHCommandTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SSHCommandTest.swift; sourceTree = "<group>"; };
BD2E27B429BAA8DA003AF1DA /* ReplaySubject.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReplaySubject.swift; sourceTree = "<group>"; };
BD3E1E4F278D190500333C44 /* Archive.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Archive.swift; sourceTree = "<group>"; };
BD44DCE526D6BEAC00054338 /* BlinkItemIdentifier.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BlinkItemIdentifier.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2090,6 +2092,7 @@
BD9EA215271F83B400874007 /* BlinkLoggingTests.swift */,
BD74A7C12905BD5800ED01CF /* WhatsNewModelTests.swift */,
BDE7C45B29DCAEFA005E033E /* FileLocationPathTests.swift */,
BD19DB402B056E9C003A4367 /* SSHCommandTest.swift */,
);
path = BlinkTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -3229,6 +3232,7 @@
BD9EA218271F846400874007 /* Publisher.swift in Sources */,
BD8BBF5525F829B00084705F /* SEKeyTests.swift in Sources */,
BD9EA216271F83B400874007 /* BlinkLoggingTests.swift in Sources */,
BD19DB412B056E9C003A4367 /* SSHCommandTest.swift in Sources */,
D20CBA57236031D700D93301 /* CompleteUtilsTests.swift in Sources */,
D20CBA5B2360327900D93301 /* CompleteUtils.swift in Sources */,
D265FBC62317E54C0017EAC4 /* SessionParams.swift in Sources */,
Expand Down
72 changes: 36 additions & 36 deletions Blink/Commands/ssh/SSHConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,41 @@ struct SSHCommand: ParsableCommand {
// Commands can define a version for automatic '--version' support.
version: Version)

// Connect to User at Host
@Argument(help: "[user@]host[#port]")
var userAtHost: String
var hostAlias: String {
get {
let comps = userAtHost.components(separatedBy: "@")
let hostAndPort = comps[comps.count - 1]
let compsHost = hostAndPort.components(separatedBy: "#")
return compsHost[0]
}
}
var user: String? {
get {
// Login name preference over user@host
if let user = loginName {
return user
}
var comps = userAtHost.components(separatedBy: "@")
if comps.count > 1 {
comps.removeLast()
return comps.joined(separator: "@")
}
return nil
}
}
var port: UInt16? {
get {
if let port = customPort {
return port
}
let comps = userAtHost.components(separatedBy: "#")
return comps.count > 1 ? UInt16(comps[1]) : nil
}
}

// Port forwarding options
@Option(name: .customShort("L"),
help: "<localport>:<bind_address>:<remoteport> Specifies that the given port on the local (client) host is to be forwarded to the given host and port on the remote side."
Expand Down Expand Up @@ -198,43 +233,8 @@ struct SSHCommand: ParsableCommand {
)
var identityFile: String?

// Connect to User at Host
@Argument(help: "[user@]host[#port]")
var userAtHost: String
var hostAlias: String {
get {
let comps = userAtHost.components(separatedBy: "@")
let hostAndPort = comps[comps.count - 1]
let compsHost = hostAndPort.components(separatedBy: "#")
return compsHost[0]
}
}
var user: String? {
get {
// Login name preference over user@host
if let user = loginName {
return user
}
var comps = userAtHost.components(separatedBy: "@")
if comps.count > 1 {
comps.removeLast()
return comps.joined(separator: "@")
}
return nil
}
}
var port: UInt16? {
get {
if let port = customPort {
return port
}
let comps = userAtHost.components(separatedBy: "#")
return comps.count > 1 ? UInt16(comps[1]) : nil
}
}

@Argument(
parsing: .unconditionalRemaining,
parsing: .remaining,
help: .init(
"If a <command> is specified, it is executed on the remote host instead of a login shell",
valueName: "command"
Expand Down
50 changes: 50 additions & 0 deletions BlinkTests/SSHCommandTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//////////////////////////////////////////////////////////////////////////////////
//
// B L I N K
//
// Copyright (C) 2016-2023 Blink Mobile Shell Project
//
// This file is part of Blink.
//
// Blink is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Blink is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Blink. If not, see <http://www.gnu.org/licenses/>.
//
// In addition, Blink is also subject to certain additional terms under
// GNU GPL version 3 section 7.
//
// You should have received a copy of these additional terms immediately
// following the terms and conditions of the GNU General Public License
// which accompanied the Blink Source Code. If not, see
// <http://www.github.com/blinksh/blink>.
//
////////////////////////////////////////////////////////////////////////////////


import XCTest

@testable import Blink

final class SSHCommandTest: XCTestCase {
func testSSHCommandParams() throws {
var cmd: SSHCommand
XCTAssertThrowsError(try SSHCommand.parse(["-t", "-T", "user@host"]))
XCTAssertThrowsError(try SSHCommand.parse(["-o", "ForwardAgent", "yes", "user@host"]))
cmd = try SSHCommand.parse(["-L", "11:forward:00", "-o", "ForwardAgent=yes", "user@host","-vv", "-p", "2222", "-L", "forward", "--", "cat", "-v", "hello"])
XCTAssertTrue(cmd.customPort == 2222)
XCTAssertTrue(cmd.command == ["cat", "-v", "hello"])
XCTAssertTrue(cmd.localForward.count == 2)
// Resolved at the SSH Config level
XCTAssertTrue(cmd.agentForward == false)
XCTAssertTrue(cmd.verbosity == 2)
}
}

0 comments on commit 3cda061

Please sign in to comment.