mirror of
https://github.com/sdkman/sdkman-cli.git
synced 2026-05-18 21:45:39 -04:00
Add edit command (#874)
* Add edit command * Fix failing test * Prevent EDITOR from being overwritten * Split up error message * Add missing test case Co-authored-by: Oliver Weiler <oliver.weiler@meinestadt.de>
This commit is contained in:
30
src/main/bash/sdkman-edit.sh
Normal file
30
src/main/bash/sdkman-edit.sh
Normal file
@@ -0,0 +1,30 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
#
|
||||
# Copyright 2017 Marco Vermeulen
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
function __sdk_edit() {
|
||||
local -r editor=${EDITOR:=vi}
|
||||
|
||||
if ! command -v "$editor" > /dev/null; then
|
||||
__sdkman_echo_red "No default editor configured."
|
||||
__sdkman_echo_yellow "Please set the default editor with the EDITOR environment variable."
|
||||
|
||||
return 1
|
||||
fi
|
||||
|
||||
"$editor" "${SDKMAN_DIR}/etc/config"
|
||||
}
|
||||
@@ -118,7 +118,7 @@ function sdk() {
|
||||
|
||||
# Check whether the candidate exists
|
||||
local sdkman_valid_candidate=$(echo ${SDKMAN_CANDIDATES[@]} | grep -w "$QUALIFIER")
|
||||
if [[ -n "$QUALIFIER" && "$COMMAND" != "offline" && "$COMMAND" != "flush" && "$COMMAND" != "selfupdate" && "$COMMAND" != "env" && "$COMMAND" != "completion" && -z "$sdkman_valid_candidate" ]]; then
|
||||
if [[ -n "$QUALIFIER" && "$COMMAND" != "offline" && "$COMMAND" != "flush" && "$COMMAND" != "selfupdate" && "$COMMAND" != "env" && "$COMMAND" != "completion" && "$COMMAND" != "edit" && -z "$sdkman_valid_candidate" ]]; then
|
||||
echo ""
|
||||
__sdkman_echo_red "Stop! $QUALIFIER is not a valid candidate."
|
||||
return 1
|
||||
|
||||
43
src/test/groovy/sdkman/specs/EditCommandSpec.groovy
Normal file
43
src/test/groovy/sdkman/specs/EditCommandSpec.groovy
Normal file
@@ -0,0 +1,43 @@
|
||||
package sdkman.specs
|
||||
|
||||
import sdkman.support.SdkmanEnvSpecification
|
||||
|
||||
class EditCommandSpec extends SdkmanEnvSpecification {
|
||||
def "it should open the config in the system's default editor"() {
|
||||
given:
|
||||
bash = sdkmanBashEnvBuilder
|
||||
.withVersionCache("x.y.z")
|
||||
.withOfflineMode(true)
|
||||
.build()
|
||||
|
||||
bash.start()
|
||||
bash.execute("source $bootstrapScript")
|
||||
|
||||
when:
|
||||
setupEnv(bash)
|
||||
bash.execute("sdk edit")
|
||||
|
||||
then:
|
||||
verifyOutput(bash.output, sdkmanBaseDirectory)
|
||||
|
||||
where:
|
||||
setupEnv << [
|
||||
{
|
||||
it.execute('nano() { echo "nano was called with $1"; }')
|
||||
it.execute("EDITOR=nano")
|
||||
},
|
||||
{
|
||||
it.execute('vi() { echo "vi was called with $1"; }')
|
||||
it.execute("unset EDITOR")
|
||||
},
|
||||
{
|
||||
it.execute("EDITOR=/does/not/exist")
|
||||
}
|
||||
]
|
||||
verifyOutput << [
|
||||
{ output, baseDirectory -> output.contains("nano was called with ${baseDirectory}/.sdkman/etc/config") },
|
||||
{ output, baseDirectory -> output.contains("vi was called with ${baseDirectory}/.sdkman/etc/config") },
|
||||
{ output, _ -> output.contains("No default editor configured.") }
|
||||
]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user