Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update gvm-installer #435

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 64 additions & 66 deletions binscripts/gvm-installer
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
#!/usr/bin/env bash

display_error() {
tput sgr0
tput setaf 1
echo "ERROR: $1"
tput sgr0
exit 1
tput sgr0
tput setaf 1
echo "ERROR: $1"
tput sgr0
exit 1
}

update_profile() {
[ -f "$1" ] || return 1
[ -f "$1" ] || return 1

grep -F "$source_line" "$1" > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo -e "\n$source_line" >> "$1"
fi
grep -F "$source_line" "$1" >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo -e "\n$source_line" >>"$1"
fi
}

check_existing_go() {

if [ "$GOROOT" = "" ]; then
if which go > /dev/null 2>&1; then
GOROOT=$(go env | grep GOROOT | cut -d"=" -f2)
else
echo "No existing Go versions detected"
return
fi
fi
echo "Created profile for existing install of Go at $GOROOT"
mkdir -p "$GVM_DEST/$GVM_NAME/environments" &> /dev/null || display_error "Failed to create environment directory"
mkdir -p "$GVM_DEST/$GVM_NAME/pkgsets/system/global" &> /dev/null || display_error "Failed to create new package set"
mkdir -p "$GVM_DEST/$GVM_NAME/gos/system" &> /dev/null || display_error "Failed to create new Go folder"
cat << EOF > $GVM_DEST/$GVM_NAME/environments/system
if [ "$GOROOT" = "" ]; then
if which go >/dev/null 2>&1; then
GOROOT=$(go env | grep GOROOT | cut -d"=" -f2)
else
echo "No existing Go versions detected"
return
fi
fi
echo "Created profile for existing install of Go at $GOROOT"
mkdir -p "$GVM_DEST/$GVM_NAME/environments" &>/dev/null || display_error "Failed to create environment directory"
mkdir -p "$GVM_DEST/$GVM_NAME/pkgsets/system/global" &>/dev/null || display_error "Failed to create new package set"
mkdir -p "$GVM_DEST/$GVM_NAME/gos/system" &>/dev/null || display_error "Failed to create new Go folder"
cat <<EOF >$GVM_DEST/$GVM_NAME/environments/system
# Automatically generated file. DO NOT EDIT!
export GVM_ROOT; GVM_ROOT="$GVM_DEST/$GVM_NAME"
export gvm_go_name; gvm_go_name="system"
Expand All @@ -41,11 +41,11 @@ export GOPATH; GOPATH="$GVM_DEST/$GVM_NAME/pkgsets/system/global"
export PATH; PATH="$GVM_DEST/$GVM_NAME/pkgsets/system/global/bin:$GOROOT/bin:$GVM_ROOT/bin:\$PATH"
EOF

# create system@global pkgset
cp $GVM_DEST/$GVM_NAME/environments/system $GVM_DEST/$GVM_NAME/environments/system@global
# @TODO: This is here for consistency with 'gvm pkgset create' which adds
# some duplicated entries when the pkgset is 'global'. Need to fix it!
cat << EOF >> $GVM_DEST/$GVM_NAME/environments/system@global
# create system@global pkgset
cp $GVM_DEST/$GVM_NAME/environments/system $GVM_DEST/$GVM_NAME/environments/system@global
# @TODO: This is here for consistency with 'gvm pkgset create' which adds
# some duplicated entries when the pkgset is 'global'. Need to fix it!
cat <<EOF >>$GVM_DEST/$GVM_NAME/environments/system@global
export gvm_pkgset_name="global"
export GOPATH; GOPATH="$GVM_DEST/$GVM_NAME/pkgsets/system/global:\$GOPATH"
export PATH; PATH="$GVM_DEST/$GVM_NAME/pkgsets/system/global/bin:\$PATH"
Expand All @@ -64,7 +64,7 @@ SRC_REPO=${SRC_REPO:-https://github.com/moovweb/gvm.git}

rm -rf $GVM_DEST/$GVM_NAME"

[ -d "$GVM_DEST" ] || mkdir -p "$GVM_DEST" > /dev/null 2>&1 || display_error "Failed to create $GVM_DEST"
[ -d "$GVM_DEST" ] || mkdir -p "$GVM_DEST" >/dev/null 2>&1 || display_error "Failed to create $GVM_DEST"
[ -z "$(which git)" ] && display_error "Could not find git

debian/ubuntu: apt-get install git
Expand All @@ -79,62 +79,60 @@ SRC_REPO=${SRC_REPO:-https://github.com/moovweb/gvm.git}

GIT_ROOT=$(git rev-parse --show-toplevel 2>/dev/null)

if [[ -z "$GIT_ROOT" || "$(basename "$GIT_ROOT")" != "gvm" ]]
then
echo "Cloning from $SRC_REPO to $GVM_DEST/$GVM_NAME"
if [[ -z "$GIT_ROOT" || "$(basename "$GIT_ROOT")" != "gvm" ]]; then
echo "Cloning from $SRC_REPO to $GVM_DEST/$GVM_NAME"

git clone --quiet "$SRC_REPO" "$GVM_DEST/$GVM_NAME" 2> /dev/null ||
display_error "Failed to clone from $SRC_REPO into $GVM_DEST/$GVM_NAME"
git clone --quiet "$SRC_REPO" "$GVM_DEST/$GVM_NAME" 2>/dev/null ||
display_error "Failed to clone from $SRC_REPO into $GVM_DEST/$GVM_NAME"
else
if [[ $GVM_DEST == *"$GIT_ROOT"* ]]
then
ln -s "$GIT_ROOT" "$GVM_DEST"
else
cp -r "$GIT_ROOT" "$GVM_DEST/$GVM_NAME"
fi
if [[ $GVM_DEST == *"$GIT_ROOT"* ]]; then
ln -s "$GIT_ROOT" "$GVM_DEST"
else
cp -r "$GIT_ROOT" "$GVM_DEST/$GVM_NAME"
fi
fi

# GVM_DEST may be a non-relative path
# i.e: gvm-installer master foo
pushd . > /dev/null
pushd . >/dev/null

builtin cd "$GVM_DEST/$GVM_NAME" && git checkout --quiet "$BRANCH" 2> /dev/null || display_error "Failed to checkout $BRANCH branch"
builtin cd "$GVM_DEST/$GVM_NAME" && git checkout --quiet "$BRANCH" 2>/dev/null || display_error "Failed to checkout $BRANCH branch"

popd > /dev/null
popd >/dev/null

[ -z "$GVM_NO_GIT_BAK" ] && mv "$GVM_DEST/$GVM_NAME/.git" "$GVM_DEST/$GVM_NAME/git.bak"

source_line="[[ -s \"${GVM_DEST}/$GVM_NAME/scripts/gvm\" ]] && source \"${GVM_DEST}/$GVM_NAME/scripts/gvm\""
source_file="${GVM_DEST}/$GVM_NAME/scripts/gvm"

if [ -z "$GVM_NO_UPDATE_PROFILE" ] ; then
if [ -f "$HOME/.zshrc" ]; then
update_profile "$HOME/.zshrc"
fi
if [ "$(uname)" == "Linux" ]; then
update_profile "$HOME/.bashrc" || update_profile "$HOME/.bash_profile"
elif [ "$(uname)" == "Darwin" ]; then
LOGIN_SHELL=$(finger $(id -u -n) | grep Shell | cut -d : -f 3)
echo "macOS detected. User shell is:" $LOGIN_SHELL
if [ $LOGIN_SHELL == "/bin/zsh" ]; then # macOS moved to ZSH after macOS Catalina
update_profile "$HOME/.zshrc"
else
update_profile "$HOME/.profile" || update_profile "$HOME/.bash_profile"
if [ -z "$GVM_NO_UPDATE_PROFILE" ]; then
if [ -f "$HOME/.zshrc" ]; then
update_profile "$HOME/.zshrc"
fi
if [ "$(uname)" = "Linux" ]; then
update_profile "$HOME/.bashrc" || update_profile "$HOME/.bash_profile"
elif [ "$(uname)" = "Darwin" ]; then
LOGIN_SHELL=$(finger $(id -u -n) | grep Shell | cut -d : -f 3)
echo "macOS detected. User shell is:" $LOGIN_SHELL
if [ $LOGIN_SHELL = "/bin/zsh" ]; then # macOS moved to ZSH after macOS Catalina
update_profile "$HOME/.zshrc"
else
update_profile "$HOME/.profile" || update_profile "$HOME/.bash_profile"
fi
fi
fi
fi

if [ -z "$GVM_NO_UPDATE_PROFILE" ] && [ "$?" != "0" ]; then
echo "Unable to locate profile settings file(Something like $HOME/.bashrc or $HOME/.bash_profile)"
echo
echo " You will have to manually add the following line:"
echo
echo " $source_line"
echo
if [ -z "$GVM_NO_UPDATE_PROFILE" ] || [ "$?" != "0" ]; then
echo "Unable to update profile settings file (Something like $HOME/.bashrc or $HOME/.bash_profile)"
echo
echo " You will have to manually add the following line:"
echo
echo " $source_line"
echo
fi

echo "export GVM_ROOT=$GVM_DEST/$GVM_NAME" > "$GVM_DEST/$GVM_NAME/scripts/gvm"
echo ". \$GVM_ROOT/scripts/gvm-default" >> "$GVM_DEST/$GVM_NAME/scripts/gvm"
echo "export GVM_ROOT=$GVM_DEST/$GVM_NAME" >"$GVM_DEST/$GVM_NAME/scripts/gvm"
echo ". \$GVM_ROOT/scripts/gvm-default" >>"$GVM_DEST/$GVM_NAME/scripts/gvm"
check_existing_go
[[ -s "$GVM_DEST/$GVM_NAME/scripts/gvm" ]] && source "$GVM_DEST/$GVM_NAME/scripts/gvm"
echo "Installed GVM v${GVM_VERSION}"
Expand Down