diff --git a/README.md b/README.md index 0ae0b0f..93323cd 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ - Search available version of Go. - Manage locally cache files(such as `*.tar.gz`, `*.tar.gz.sha256`). - Upgrade `goup` itself. +- Customize `GOUP_HOME`(default `$HOME/.goup`)(>= v0.11.x); - Friendly prompt. - Should be pretty fast. @@ -258,6 +259,9 @@ Default log level is `Info`. You can use `goup -v ` or `goup -vv = v0.11.x) + `goup` use the `$HOME/.goup` directory as `GOUP_HOME`. if you want to customize the `GOUP_HOME`(most are Windows users), you can set `GOUP_HOME` environment variable to use another directory, before install `goup`, make sure you has set the customize `GOUP_HOME` environment variable and the target directory permissions, otherwise, it may lead to surprising results, refer issue [#265](https://github.com/thinkgos/goup-rs/issues/265) [#270](https://github.com/thinkgos/goup-rs/pull/270) + ## License [Apache 2.0](LICENSE) diff --git a/README_CN.md b/README_CN.md index bdc9d5d..57c5e7f 100644 --- a/README_CN.md +++ b/README_CN.md @@ -27,10 +27,11 @@ - 支持搜索可用的Go版本. - 支持管理本地缓存文件(如 `*.tar.gz`, `*.tar.gz.sha256`). - 支持`goup`自我更新. +- 支持自定义`GOUP_HOME`(默认`$HOME/.goup`)(>= v0.11.x); - 友好的提示. - 应该很快. -`goup` 是对上述特性的一种尝试,其灵感主要来自于 [Rustup](https://rustup.rs/), [golang/dl](https://github.com/golang/dl), [goup](https://github.com/owenthereal/goup), [goenv](https://github.com/syndbg/goenv), [gvm](https://github.com/moovweb/gvm) and [getgo](https://github.com/golang/tools/tree/master/cmd/getgo). +`goup` 是对上述特性的一种尝试, 其灵感主要来自于 [Rustup](https://rustup.rs/), [golang/dl](https://github.com/golang/dl), [goup](https://github.com/owenthereal/goup), [goenv](https://github.com/syndbg/goenv), [gvm](https://github.com/moovweb/gvm) and [getgo](https://github.com/golang/tools/tree/master/cmd/getgo). ## 安装 @@ -243,7 +244,7 @@ goup completion zsh > _goup - 编译和安装源代码失败? 所需的Go最低版本取决于Go的目标版本, 更多信息请参见[source installation instructions](https://go.dev/doc/install/source) - [`semver`](https://semver.org/) - - exact(`=`): 允许更新到与版本完全一致的最新版本, 因此`=1.21.4`表示与版本`1.21.4`完全一致。 + - exact(`=`): 允许更新到与版本完全一致的最新版本, 因此`=1.21.4`表示与版本`1.21.4`完全一致. - greater(`>`): 允许更新到大于该版本的最新版本, 因此`>1.21.4`表示大于`1.21.4`. - greater equal(`>=`): 允许更新到大于或等于该版本的最新版本, 因此 `>1.21.4` 表示大于或等于`1.21.4`. - less(`<`): 允许更新到小于该版本的最新版本, 因此`>1.21.4`表示大于`1.21.4`. @@ -258,6 +259,9 @@ goup completion zsh > _goup 大于v0.10.3版本已解决. 看多信息查看[issue #251](https://github.com/thinkgos/goup-rs/issues/251) +- 如何自定义 `GOUP_HOME`? (>= v0.11.x) + `goup`使用`$HOME/.goup`目录作为 `GOUP_HOME`. 如果需要自定义`GOUP_HOME`(大多数是Windows用户), 可以设置`GOUP_HOME`环境变量来使用其他目录, 安装`goup`之前, 请确保已设置自定义`GOUP_HOME`环境变量和目标目录权限, 否则可能会导致令人惊讶的结果, 请参阅issue [#265](https://github.com/thinkgos/goup-rs/issues/265) [#270](https://github.com/thinkgos/goup-rs/pull/270) + ## 许可证 [Apache 2.0](LICENSE) diff --git a/goup-version/src/consts.rs b/goup-version/src/consts.rs index 45694fa..2dc98d5 100644 --- a/goup-version/src/consts.rs +++ b/goup-version/src/consts.rs @@ -1,5 +1,6 @@ use std::env; +pub const GOUP_HOME: &str = "GOUP_HOME"; pub const GOUP_GO_HOST: &str = "GOUP_GO_HOST"; pub const GOUP_GO_DOWNLOAD_BASE_URL: &str = "GOUP_GO_DOWNLOAD_BASE_URL"; pub const GOUP_GO_SOURCE_GIT_URL: &str = "GOUP_GO_SOURCE_GIT_URL"; diff --git a/goup-version/src/dir.rs b/goup-version/src/dir.rs index 7c79b48..d0abab5 100644 --- a/goup-version/src/dir.rs +++ b/goup-version/src/dir.rs @@ -1,12 +1,14 @@ use std::{ - fs, - fs::File, + env, + fs::{self, File}, ops::{Deref, DerefMut}, path::{Path, PathBuf}, }; use anyhow::anyhow; +use super::consts::GOUP_HOME; + /// Dir `${path}/.goup` contain a `PathBuf`. #[derive(Debug, Clone, PartialEq)] pub struct Dir { @@ -16,7 +18,7 @@ pub struct Dir { impl Dir { /// Returns the path to the user's home directory. pub fn home_dir() -> Result { - dirs::home_dir().ok_or_else(|| anyhow!("where is home")) + dirs::home_dir().ok_or_else(|| anyhow!("home dir get failed")) } /// Allocates a Dir as `${path}/.goup` pub fn new>(p: P) -> Self { @@ -24,9 +26,17 @@ impl Dir { path.push(".goup"); Self { path } } - /// Allocates a `GOUP_HOME` Dir as `${HOME}/.goup` + /// Allocates a `GOUP_HOME` Dir as Environment Or `${HOME}/.goup` pub fn goup_home() -> Result { - Ok(Self::new(Self::home_dir()?)) + env::var(GOUP_HOME) + .ok() + .filter(|s| !s.is_empty()) + .map(|s| { + Ok(Self { + path: PathBuf::from(s), + }) + }) + .unwrap_or_else(|| Self::home_dir().map(Self::new)) } // Creates an owned [`Dir`] with `path` adjoined to `self`. pub fn join_path>(&self, path: P) -> Self { @@ -120,6 +130,14 @@ impl DerefMut for Dir { } } +impl Default for Dir { + fn default() -> Self { + Self { + path: PathBuf::new(), + } + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/goup/setup_env_unix b/goup/setup_env_unix index e51752a..4b8d641 100644 --- a/goup/setup_env_unix +++ b/goup/setup_env_unix @@ -1,22 +1,25 @@ #!/bin/sh + +local GOUP_HOME=${GOUP_HOME:-$HOME/.goup} + # goup shell setup # affix colons on either side of $PATH to simplify matching case ":${PATH}:" in - *:"$HOME/.goup/current/bin":*) + *:"$GOUP_HOME/current/bin":*) ;; *) # Prepending path in case a system-installed go needs to be overridden - export GOROOT=$HOME/.goup/current + export GOROOT=$GOUP_HOME/current export PATH=$PATH:$GOROOT/bin ;; esac # affix colons on either side of $PATH to simplify matching case ":${PATH}:" in - *:"$HOME/.goup/bin":*) + *:"$GOUP_HOME/bin":*) ;; *) # Prepending path in case a system-installed rustc needs to be overridden - export PATH="$HOME/.goup/bin:$PATH" + export PATH="$GOUP_HOME/bin:$PATH" ;; -esac \ No newline at end of file +esac diff --git a/goup/src/command/env.rs b/goup/src/command/env.rs index 83a1c00..9f79993 100644 --- a/goup/src/command/env.rs +++ b/goup/src/command/env.rs @@ -1,5 +1,5 @@ use clap::Args; -use goup_version::consts; +use goup_version::{Dir, consts}; use prettytable::{Table, row}; use super::Run; @@ -12,6 +12,11 @@ impl Run for Env { let mut table = Table::new(); table.add_row(row!["Key", "Value", "Explain"]); + table.add_row(row![ + consts::GOUP_HOME, + Dir::goup_home().unwrap_or_default().to_string_lossy(), + "Get goup home directory, default: '$HOME/.goup'", + ]); table.add_row(row![ consts::GOUP_GO_HOST, consts::go_host(), diff --git a/install.sh b/install.sh index 4b64461..8f3e2d3 100755 --- a/install.sh +++ b/install.sh @@ -132,9 +132,9 @@ function main() { local dld=$(get_dld) local _url="https://github.com/thinkgos/goup-rs/releases/latest/download/${_target}" - local GOUP_DIR="$HOME/.goup" - local GOUP_BIN_DIR="${GOUP_DIR}/bin" - local GOUP_BIN_FILE="${GOUP_DIR}/bin/goup" + local GOUP_HOME=${GOUP_HOME:-$HOME/.goup} + local GOUP_BIN_DIR="${GOUP_HOME}/bin" + local GOUP_BIN_FILE="${GOUP_HOME}/bin/goup" local _dir if ! _dir="$(ensure mktemp -d)"; then @@ -144,7 +144,7 @@ function main() { fi local _target_file="${_dir}/${_target}" - ensure mkdir -p ${GOUP_DIR} + ensure mkdir -p ${GOUP_HOME} ensure mkdir -p ${GOUP_BIN_DIR} echo "[1/3] Download goup..." @@ -162,7 +162,7 @@ function main() { ignore rm "$_target_file" ignore rmdir "$_dir" - echo "[3/3] Please add '. "\$HOME/.goup/env"' to your shell environment!!" + echo "[3/3] Please add '. "${GOUP_HOME}/env"' to your shell environment!!" echo " And then try to run 'goup --version'" }