Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
cccb5d46
Commit
cccb5d46
authored
Dec 23, 2013
by
Dave Syer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add ShellCommand and friends
parent
c94fb7fc
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
407 additions
and
7 deletions
+407
-7
pom.xml
spring-boot-cli/pom.xml
+6
-0
SpringCli.java
...src/main/java/org/springframework/boot/cli/SpringCli.java
+5
-4
CommandCompleter.java
...rg/springframework/boot/cli/command/CommandCompleter.java
+121
-0
DefaultCommandFactory.java
...ringframework/boot/cli/command/DefaultCommandFactory.java
+13
-3
PromptCommand.java
...a/org/springframework/boot/cli/command/PromptCommand.java
+28
-0
Shell.java
...main/java/org/springframework/boot/cli/command/Shell.java
+20
-0
ShellCommand.java
...va/org/springframework/boot/cli/command/ShellCommand.java
+191
-0
StopCommand.java
...ava/org/springframework/boot/cli/command/StopCommand.java
+23
-0
No files found.
spring-boot-cli/pom.xml
View file @
cccb5d46
...
@@ -14,6 +14,7 @@
...
@@ -14,6 +14,7 @@
<main.basedir>
${basedir}/..
</main.basedir>
<main.basedir>
${basedir}/..
</main.basedir>
<start-class>
org.springframework.boot.cli.SpringCli
</start-class>
<start-class>
org.springframework.boot.cli.SpringCli
</start-class>
<spring.profiles.active>
default
</spring.profiles.active>
<spring.profiles.active>
default
</spring.profiles.active>
<jline.version>
2.11
</jline.version>
</properties>
</properties>
<profiles>
<profiles>
<profile>
<profile>
...
@@ -49,6 +50,11 @@
...
@@ -49,6 +50,11 @@
</dependencyManagement>
</dependencyManagement>
<dependencies>
<dependencies>
<!-- Compile -->
<!-- Compile -->
<dependency>
<groupId>
jline
</groupId>
<artifactId>
jline
</artifactId>
<version>
${jline.version}
</version>
</dependency>
<dependency>
<dependency>
<groupId>
net.sf.jopt-simple
</groupId>
<groupId>
net.sf.jopt-simple
</groupId>
<artifactId>
jopt-simple
</artifactId>
<artifactId>
jopt-simple
</artifactId>
...
...
spring-boot-cli/src/main/java/org/springframework/boot/cli/SpringCli.java
View file @
cccb5d46
...
@@ -123,11 +123,12 @@ public class SpringCli {
...
@@ -123,11 +123,12 @@ public class SpringCli {
* @throws Exception
* @throws Exception
*/
*/
protected
void
run
(
String
...
args
)
throws
Exception
{
protected
void
run
(
String
...
args
)
throws
Exception
{
if
(
args
.
length
==
0
)
{
String
commandName
=
"shell"
;
throw
new
NoArgumentsException
();
if
(
args
.
length
>
0
)
{
commandName
=
args
[
0
];
}
}
String
commandName
=
args
[
0
];
String
[]
commandArguments
=
args
.
length
>
1
?
Arrays
.
copyOfRange
(
args
,
1
,
String
[]
commandArguments
=
Arrays
.
copyOfRange
(
args
,
1
,
args
.
length
)
;
args
.
length
)
:
new
String
[
0
]
;
Command
command
=
find
(
commandName
);
Command
command
=
find
(
commandName
);
if
(
command
==
null
)
{
if
(
command
==
null
)
{
throw
new
NoSuchCommandException
(
commandName
);
throw
new
NoSuchCommandException
(
commandName
);
...
...
spring-boot-cli/src/main/java/org/springframework/boot/cli/command/CommandCompleter.java
0 → 100644
View file @
cccb5d46
package
org
.
springframework
.
boot
.
cli
.
command
;
import
java.io.IOException
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.ServiceLoader
;
import
jline.console.ConsoleReader
;
import
jline.console.completer.ArgumentCompleter
;
import
jline.console.completer.Completer
;
import
jline.console.completer.NullCompleter
;
import
jline.console.completer.StringsCompleter
;
import
org.springframework.boot.cli.Command
;
import
org.springframework.boot.cli.CommandFactory
;
import
org.springframework.boot.cli.Log
;
import
org.springframework.boot.cli.OptionHelp
;
import
org.springframework.boot.cli.SpringCli
;
import
org.springframework.util.StringUtils
;
/**
* @author Jon Brisbin
*/
public
class
CommandCompleter
extends
StringsCompleter
{
private
final
Map
<
String
,
Completer
>
optionCompleters
=
new
HashMap
<
String
,
Completer
>();
private
List
<
Command
>
commands
=
new
ArrayList
<
Command
>();
private
ConsoleReader
console
;
private
String
lastBuffer
;
public
CommandCompleter
(
ConsoleReader
console
,
SpringCli
cli
)
{
this
.
console
=
console
;
for
(
CommandFactory
fac
:
ServiceLoader
.
load
(
CommandFactory
.
class
,
getClass
().
getClassLoader
()))
{
commands
.
addAll
(
fac
.
getCommands
(
cli
));
}
List
<
String
>
names
=
new
ArrayList
<
String
>();
for
(
Command
c
:
commands
)
{
names
.
add
(
c
.
getName
());
List
<
String
>
opts
=
new
ArrayList
<
String
>();
for
(
OptionHelp
optHelp
:
c
.
getOptionsHelp
())
{
opts
.
addAll
(
optHelp
.
getOptions
());
}
optionCompleters
.
put
(
c
.
getName
(),
new
ArgumentCompleter
(
new
StringsCompleter
(
c
.
getName
()),
new
StringsCompleter
(
opts
),
new
NullCompleter
()
));
}
getStrings
().
addAll
(
names
);
}
@Override
public
int
complete
(
String
buffer
,
int
cursor
,
List
<
CharSequence
>
candidates
)
{
int
i
=
super
.
complete
(
buffer
,
cursor
,
candidates
);
if
(
buffer
.
indexOf
(
' '
)
<
1
)
{
return
i
;
}
String
name
=
buffer
.
substring
(
0
,
buffer
.
indexOf
(
' '
));
if
(
""
.
equals
(
name
.
trim
()))
{
return
i
;
}
for
(
Command
c
:
commands
)
{
if
(!
c
.
getName
().
equals
(
name
))
{
continue
;
}
if
(
buffer
.
equals
(
lastBuffer
))
{
lastBuffer
=
buffer
;
try
{
console
.
println
();
console
.
println
(
"Usage:"
);
console
.
println
(
c
.
getName
()
+
" "
+
c
.
getUsageHelp
());
List
<
List
<
String
>>
rows
=
new
ArrayList
<
List
<
String
>>();
int
maxSize
=
0
;
for
(
OptionHelp
optHelp
:
c
.
getOptionsHelp
())
{
List
<
String
>
cols
=
new
ArrayList
<
String
>();
for
(
String
s
:
optHelp
.
getOptions
())
{
cols
.
add
(
s
);
}
String
opts
=
StringUtils
.
collectionToDelimitedString
(
cols
,
" | "
);
if
(
opts
.
length
()
>
maxSize
)
{
maxSize
=
opts
.
length
();
}
cols
.
clear
();
cols
.
add
(
opts
);
cols
.
add
(
optHelp
.
getUsageHelp
());
rows
.
add
(
cols
);
}
StringBuilder
sb
=
new
StringBuilder
(
"\t"
);
for
(
List
<
String
>
row
:
rows
)
{
String
col1
=
row
.
get
(
0
);
String
col2
=
row
.
get
(
1
);
for
(
int
j
=
0
;
j
<
(
maxSize
-
col1
.
length
());
j
++)
{
sb
.
append
(
" "
);
}
sb
.
append
(
col1
).
append
(
": "
).
append
(
col2
);
console
.
println
(
sb
.
toString
());
sb
=
new
StringBuilder
(
"\t"
);
}
console
.
drawLine
();
}
catch
(
IOException
e
)
{
Log
.
error
(
e
.
getMessage
()
+
" ("
+
e
.
getClass
().
getName
()
+
")"
);
}
}
Completer
completer
=
optionCompleters
.
get
(
c
.
getName
());
if
(
null
!=
completer
)
{
i
=
completer
.
complete
(
buffer
,
cursor
,
candidates
);
break
;
}
}
lastBuffer
=
buffer
;
return
i
;
}
}
spring-boot-cli/src/main/java/org/springframework/boot/cli/command/DefaultCommandFactory.java
View file @
cccb5d46
...
@@ -16,6 +16,7 @@
...
@@ -16,6 +16,7 @@
package
org
.
springframework
.
boot
.
cli
.
command
;
package
org
.
springframework
.
boot
.
cli
.
command
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.Arrays
;
import
java.util.Collection
;
import
java.util.Collection
;
import
java.util.List
;
import
java.util.List
;
...
@@ -32,12 +33,21 @@ import org.springframework.boot.cli.SpringCli;
...
@@ -32,12 +33,21 @@ import org.springframework.boot.cli.SpringCli;
public
class
DefaultCommandFactory
implements
CommandFactory
{
public
class
DefaultCommandFactory
implements
CommandFactory
{
private
static
final
List
<
Command
>
DEFAULT_COMMANDS
=
Arrays
.<
Command
>
asList
(
private
static
final
List
<
Command
>
DEFAULT_COMMANDS
=
Arrays
.<
Command
>
asList
(
new
VersionCommand
(),
new
RunCommand
(),
new
Clean
Command
(),
new
VersionCommand
(),
new
CleanCommand
(),
new
Test
Command
(),
new
TestCommand
(),
new
GrabCommand
());
new
GrabCommand
());
@Override
@Override
public
Collection
<
Command
>
getCommands
(
SpringCli
cli
)
{
public
Collection
<
Command
>
getCommands
(
SpringCli
cli
)
{
return
DEFAULT_COMMANDS
;
Collection
<
Command
>
commands
=
new
ArrayList
<
Command
>(
DEFAULT_COMMANDS
);
RunCommand
run
=
new
RunCommand
();
StopCommand
stop
=
new
StopCommand
(
run
);
ShellCommand
shell
=
new
ShellCommand
(
cli
);
PromptCommand
prompt
=
new
PromptCommand
(
shell
);
commands
.
add
(
run
);
commands
.
add
(
stop
);
commands
.
add
(
shell
);
commands
.
add
(
prompt
);
return
commands
;
}
}
}
}
spring-boot-cli/src/main/java/org/springframework/boot/cli/command/PromptCommand.java
0 → 100644
View file @
cccb5d46
package
org
.
springframework
.
boot
.
cli
.
command
;
import
org.springframework.boot.cli.command.AbstractCommand
;
/**
* @author Dave Syer
*/
public
class
PromptCommand
extends
AbstractCommand
{
private
final
ShellCommand
runCmd
;
public
PromptCommand
(
ShellCommand
runCmd
)
{
super
(
"prompt"
,
"Change the prompt used with the current 'shell' command. Execute with no arguments to return to the previous value."
);
this
.
runCmd
=
runCmd
;
}
@Override
public
void
run
(
String
...
strings
)
throws
Exception
{
if
(
strings
.
length
>
0
)
{
for
(
String
string
:
strings
)
{
runCmd
.
pushPrompt
(
string
+
" "
);
}
}
else
{
runCmd
.
popPrompt
();
}
}
}
spring-boot-cli/src/main/java/org/springframework/boot/cli/command/Shell.java
0 → 100644
View file @
cccb5d46
package
org
.
springframework
.
boot
.
cli
.
command
;
import
java.io.IOException
;
import
org.springframework.boot.cli.SpringCli
;
/**
* @author Dave Syer
*/
public
class
Shell
{
public
static
void
main
(
String
...
args
)
throws
IOException
{
if
(
args
.
length
==
0
)
{
SpringCli
.
main
(
"shell"
);
// right into the REPL by default
}
else
{
SpringCli
.
main
(
args
);
}
}
}
spring-boot-cli/src/main/java/org/springframework/boot/cli/command/ShellCommand.java
0 → 100644
View file @
cccb5d46
package
org
.
springframework
.
boot
.
cli
.
command
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.io.PrintStream
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Stack
;
import
jline.console.ConsoleReader
;
import
jline.console.completer.CandidateListCompletionHandler
;
import
org.codehaus.groovy.runtime.ProcessGroovyMethods
;
import
org.springframework.boot.cli.SpringCli
;
import
org.springframework.util.ReflectionUtils
;
import
org.springframework.util.StringUtils
;
/**
* @author Jon Brisbin
* @author Dave Syer
*/
public
class
ShellCommand
extends
AbstractCommand
{
private
static
final
String
DEFAULT_PROMPT
=
"$ "
;
private
SpringCli
springCli
;
private
String
prompt
=
DEFAULT_PROMPT
;
private
Stack
<
String
>
prompts
=
new
Stack
<
String
>();
public
ShellCommand
(
SpringCli
springCli
)
{
super
(
"shell"
,
"Start a nested shell (REPL)."
);
this
.
springCli
=
springCli
;
}
@Override
public
void
run
(
String
...
args
)
throws
Exception
{
final
ConsoleReader
console
=
new
ConsoleReader
();
console
.
addCompleter
(
new
CommandCompleter
(
console
,
this
.
springCli
));
console
.
setHistoryEnabled
(
true
);
console
.
setCompletionHandler
(
new
CandidateListCompletionHandler
());
final
InputStream
sysin
=
System
.
in
;
final
PrintStream
sysout
=
System
.
out
;
final
PrintStream
syserr
=
System
.
err
;
System
.
setIn
(
console
.
getInput
());
PrintStream
out
=
new
PrintStream
(
new
OutputStream
()
{
@Override
public
void
write
(
int
b
)
throws
IOException
{
console
.
getOutput
().
write
(
b
);
}
});
System
.
setOut
(
out
);
System
.
setErr
(
out
);
String
line
;
StringBuffer
data
=
new
StringBuffer
();
try
{
while
(
null
!=
(
line
=
console
.
readLine
(
this
.
prompt
)))
{
if
(
"quit"
.
equals
(
line
.
trim
()))
{
break
;
}
else
if
(
"clear"
.
equals
(
line
.
trim
()))
{
console
.
clearScreen
();
continue
;
}
List
<
String
>
parts
=
new
ArrayList
<
String
>();
if
(
line
.
contains
(
"<<"
))
{
int
startMultiline
=
line
.
indexOf
(
"<<"
);
data
.
append
(
line
.
substring
(
startMultiline
+
2
));
String
contLine
;
while
(
null
!=
(
contLine
=
console
.
readLine
(
"... "
)))
{
if
(
""
.
equals
(
contLine
.
trim
()))
{
break
;
}
data
.
append
(
contLine
);
}
line
=
line
.
substring
(
0
,
startMultiline
);
}
String
lineToParse
=
line
.
trim
();
if
(
lineToParse
.
startsWith
(
"!"
))
{
lineToParse
=
lineToParse
.
substring
(
1
).
trim
();
}
String
[]
segments
=
StringUtils
.
delimitedListToStringArray
(
lineToParse
,
" "
);
StringBuffer
sb
=
new
StringBuffer
();
boolean
swallowWhitespace
=
false
;
for
(
String
s
:
segments
)
{
if
(
""
.
equals
(
s
))
{
continue
;
}
if
(
s
.
startsWith
(
"\""
))
{
swallowWhitespace
=
true
;
sb
.
append
(
s
.
substring
(
1
));
}
else
if
(
s
.
endsWith
(
"\""
))
{
swallowWhitespace
=
false
;
sb
.
append
(
" "
).
append
(
s
.
substring
(
0
,
s
.
length
()
-
1
));
parts
.
add
(
sb
.
toString
());
sb
=
new
StringBuffer
();
}
else
{
if
(!
swallowWhitespace
)
{
parts
.
add
(
s
);
}
else
{
sb
.
append
(
" "
).
append
(
s
);
}
}
}
if
(
sb
.
length
()
>
0
)
{
parts
.
add
(
sb
.
toString
());
}
if
(
data
.
length
()
>
0
)
{
parts
.
add
(
data
.
toString
());
data
=
new
StringBuffer
();
}
if
(
parts
.
size
()
>
0
)
{
if
(
line
.
trim
().
startsWith
(
"!"
))
{
try
{
ProcessBuilder
pb
=
new
ProcessBuilder
(
parts
);
if
(
isJava7
())
{
inheritIO
(
pb
);
}
pb
.
environment
().
putAll
(
System
.
getenv
());
Process
process
=
pb
.
start
();
if
(!
isJava7
())
{
ProcessGroovyMethods
.
consumeProcessOutput
(
process
,
(
OutputStream
)
sysout
,
(
OutputStream
)
syserr
);
}
process
.
waitFor
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
else
{
if
(!
getName
().
equals
(
parts
.
get
(
0
)))
{
this
.
springCli
.
runAndHandleErrors
(
parts
.
toArray
(
new
String
[
parts
.
size
()]));
}
}
}
}
}
finally
{
System
.
setIn
(
sysin
);
System
.
setOut
(
sysout
);
System
.
setErr
(
syserr
);
console
.
shutdown
();
}
}
public
void
pushPrompt
(
String
prompt
)
{
this
.
prompts
.
push
(
this
.
prompt
);
this
.
prompt
=
prompt
;
}
public
String
popPrompt
()
{
if
(
this
.
prompts
.
isEmpty
())
{
this
.
prompt
=
DEFAULT_PROMPT
;
}
else
{
this
.
prompt
=
this
.
prompts
.
pop
();
}
return
this
.
prompt
;
}
private
void
inheritIO
(
ProcessBuilder
pb
)
{
ReflectionUtils
.
invokeMethod
(
ReflectionUtils
.
findMethod
(
ProcessBuilder
.
class
,
"inheritIO"
),
pb
);
}
private
boolean
isJava7
()
{
if
(
ReflectionUtils
.
findMethod
(
ProcessBuilder
.
class
,
"inheritIO"
)
!=
null
)
{
return
true
;
}
return
false
;
}
}
spring-boot-cli/src/main/java/org/springframework/boot/cli/command/StopCommand.java
0 → 100644
View file @
cccb5d46
package
org
.
springframework
.
boot
.
cli
.
command
;
import
org.springframework.boot.cli.command.AbstractCommand
;
import
org.springframework.boot.cli.command.RunCommand
;
/**
* @author Jon Brisbin
*/
public
class
StopCommand
extends
AbstractCommand
{
private
final
RunCommand
runCmd
;
public
StopCommand
(
RunCommand
runCmd
)
{
super
(
"stop"
,
"Stop the currently-running application started with the 'run' command."
);
this
.
runCmd
=
runCmd
;
}
@Override
public
void
run
(
String
...
strings
)
throws
Exception
{
runCmd
.
stop
();
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment