浏览文件
editorcmd.go
49 行 · 1.2 KB历史下载| 1 | // SPDX-FileCopyrightText: Copyright The Lima Authors |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | // From https://raw.githubusercontent.com/norouter/norouter/v0.6.5/cmd/norouter/editorcmd/editorcmd.go |
| 5 | /* |
| 6 | Copyright (C) NoRouter authors. |
| 7 | |
| 8 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | you may not use this file except in compliance with the License. |
| 10 | You may obtain a copy of the License at |
| 11 | |
| 12 | http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | |
| 14 | Unless required by applicable law or agreed to in writing, software |
| 15 | distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | See the License for the specific language governing permissions and |
| 18 | limitations under the License. |
| 19 | */ |
| 20 | |
| 21 | package editorcmd |
| 22 | |
| 23 | import ( |
| 24 | "os" |
| 25 | "os/exec" |
| 26 | ) |
| 27 | |
| 28 | // Detect detects a text editor command. |
| 29 | // Returns an empty string when no editor is found. |
| 30 | func Detect() string { |
| 31 | candidates := []string{ |
| 32 | os.Getenv("VISUAL"), |
| 33 | os.Getenv("EDITOR"), |
| 34 | "editor", |
| 35 | "vim", |
| 36 | "vi", |
| 37 | "emacs", |
| 38 | } |
| 39 | for _, f := range candidates { |
| 40 | if f == "" { |
| 41 | continue |
| 42 | } |
| 43 | x, err := exec.LookPath(f) |
| 44 | if err == nil { |
| 45 | return x |
| 46 | } |
| 47 | } |
| 48 | return "" |
| 49 | } |

