2020import com .github .fonimus .ssh .shell .SshShellHelper ;
2121import com .github .fonimus .ssh .shell .SshShellProperties ;
2222import com .github .fonimus .ssh .shell .manage .SshShellSessionManager ;
23+ import lombok .AllArgsConstructor ;
24+ import lombok .extern .slf4j .Slf4j ;
2325import org .apache .sshd .server .channel .ChannelSession ;
2426import org .apache .sshd .server .session .ServerSession ;
2527import org .springframework .boot .autoconfigure .condition .ConditionalOnProperty ;
2628import org .springframework .context .annotation .Lazy ;
2729import org .springframework .shell .Availability ;
28- import org .springframework .shell .standard . ShellCommandGroup ;
29- import org .springframework .shell .standard . ShellMethod ;
30- import org .springframework .shell .standard .ShellMethodAvailability ;
31- import org .springframework .shell . standard . ShellOption ;
30+ import org .springframework .shell .CompletionContext ;
31+ import org .springframework .shell .CompletionProposal ;
32+ import org .springframework .shell .standard .* ;
33+ import org .springframework .stereotype . Component ;
3234
3335import java .util .Arrays ;
36+ import java .util .List ;
3437import java .util .Map ;
38+ import java .util .stream .Collectors ;
3539
3640import static com .github .fonimus .ssh .shell .manage .SshShellSessionManager .sessionUserName ;
3741
@@ -80,7 +84,7 @@ public String manageSessionsList() {
8084
8185 @ ShellMethod (key = COMMAND_MANAGE_SESSIONS_INFO , value = "Displays session" )
8286 @ ShellMethodAvailability ("manageSessionsInfoAvailability" )
83- public String manageSessionsInfo (@ ShellOption (help = "Session identifier" ) long sessionId ) {
87+ public String manageSessionsInfo (@ ShellOption (help = "Session identifier" , valueProvider = SessionsValuesProvider . class ) long sessionId ) {
8488 ChannelSession session = sessionManager .getSession (sessionId );
8589 if (session == null ) {
8690 return helper .getError ("Session [" + sessionId + "] not found" );
@@ -90,7 +94,7 @@ public String manageSessionsInfo(@ShellOption(help = "Session identifier") long
9094
9195 @ ShellMethod (key = COMMAND_MANAGE_SESSIONS_STOP , value = "Stop session" )
9296 @ ShellMethodAvailability ("manageSessionsStopAvailability" )
93- public String manageSessionsStop (@ ShellOption (help = "Session identifier" ) long sessionId ) {
97+ public String manageSessionsStop (@ ShellOption (help = "Session identifier" , valueProvider = SessionsValuesProvider . class ) long sessionId ) {
9498 return sessionManager .stopSession (sessionId ) ?
9599 helper .getSuccess ("Session [" + sessionId + "] stopped" ) :
96100 helper .getWarning ("Unable to stop session [" + sessionId + "], maybe it does not exist" );
@@ -119,3 +123,18 @@ private Availability manageSessionsStopAvailability() {
119123 return availability (GROUP , COMMAND_MANAGE_SESSIONS_STOP );
120124 }
121125}
126+
127+ @ Slf4j
128+ @ Component
129+ @ AllArgsConstructor
130+ class SessionsValuesProvider
131+ implements ValueProvider {
132+
133+ @ Lazy
134+ private final SshShellSessionManager sessionManager ;
135+
136+ @ Override
137+ public List <CompletionProposal > complete (CompletionContext completionContext ) {
138+ return sessionManager .listSessions ().keySet ().stream ().map (id -> new CompletionProposal (id .toString ())).collect (Collectors .toList ());
139+ }
140+ }
0 commit comments