From ea1f67e950c0ea91db9a73d348041ec1f24a0781 Mon Sep 17 00:00:00 2001 From: Oliver Weiler Date: Tue, 16 Feb 2021 21:58:29 +0100 Subject: [PATCH] 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 --- src/main/bash/sdkman-edit.sh | 30 +++++++++++++ src/main/bash/sdkman-main.sh | 2 +- .../sdkman/specs/EditCommandSpec.groovy | 43 +++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 src/main/bash/sdkman-edit.sh create mode 100644 src/test/groovy/sdkman/specs/EditCommandSpec.groovy diff --git a/src/main/bash/sdkman-edit.sh b/src/main/bash/sdkman-edit.sh new file mode 100644 index 00000000..df9b776c --- /dev/null +++ b/src/main/bash/sdkman-edit.sh @@ -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" +} \ No newline at end of file diff --git a/src/main/bash/sdkman-main.sh b/src/main/bash/sdkman-main.sh index 2f3ec812..71975038 100644 --- a/src/main/bash/sdkman-main.sh +++ b/src/main/bash/sdkman-main.sh @@ -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 diff --git a/src/test/groovy/sdkman/specs/EditCommandSpec.groovy b/src/test/groovy/sdkman/specs/EditCommandSpec.groovy new file mode 100644 index 00000000..096a295e --- /dev/null +++ b/src/test/groovy/sdkman/specs/EditCommandSpec.groovy @@ -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.") } + ] + } +}