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:
Oliver Weiler
2021-02-16 21:58:29 +01:00
committed by GitHub
parent e88bb5131b
commit ea1f67e950
3 changed files with 74 additions and 1 deletions

View 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"
}

View File

@@ -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

View 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.") }
]
}
}