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

feat: script to help update mgc #19

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,21 @@ Para utilizar a CLI você precisa já ter criado uma conta na Magalu Cloud atrav

Uma vez tendo criado a sua conta, basta seguir o procedimento de download e instalação para o seus sistema.




## Download e Instalação

Baixe a *release* correta para seu sistema e arquitetura no link abaixo.

### Linux

#### **Script - Bash ou GitBash**

```
curl https://raw.githubusercontent.com/MagaluCloud/mgccli/utils/update_mgccli_latest.sh | sudo bash
```

[Releases](https://github.com/MagaluCloud/mgccli/releases/)

### Linux
Expand Down
191 changes: 191 additions & 0 deletions utils/update_mgccli_latest.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
#!/bin/bash

function check_dependences() {
CURL_RESULT=$(whereis curl|cut -d\: -f2)

if [ -z "$CURL_RESULT" ]; then
echo "o binário CURL não está instalado, satisfaça a dependencia antes de continuar"
echo "Ex: apt update && apt install curl tar gzip unzip -y"
exit 1
fi

UNZIP_RESULT=$(whereis unzip|cut -d\: -f2)

if [ -z "$UNZIP_RESULT" ]; then
echo "o binário UNZIP não está instalado, satisfaça a dependencia antes de continuar"
echo "Ex: apt update && apt install curl tar gzip unzip -y"
exit 1
fi


TAR_RESULT=$(whereis tar|cut -d\: -f2)

if [ -z "$TAR_RESULT" ]; then
echo "o binário TAR não está instalado, satisfaça a dependencia antes de continuar"
echo "Ex: apt update && apt install curl tar gzip unzip -y"
exit 1
fi

if [ $(whoami) != "root" ]; then
echo "Usuário precisa ter permissão de root, use sudo ou vire root"
exit 1
fi
}

function check_version(){

LATEST_VRESION=$(curl -s https://api.github.com/repos/MagaluCloud/mgccli/releases/latest|grep tag_name|cut -d\: -f2|cut -d\" -f2)
CURRTENT_VRESION=$(mgc --version 2> /dev/null |cut -d\ -f3)
if [ $? -eq 1 ]; then
echo "Comando não encontrado ou corrompido"
else
if [ "$LATEST_VRESION" == "$CURRTENT_VRESION" ]; then
echo "Ultima versão já instalada"
exit 0
else
echo "mgc não instalado ou desatualizado"
fi
fi
}


check_version
check_dependences

#Definie OS
MACOS="Darwin"
LINUX="Linux"
GITBASH_01="MSYS"
GITBASH_02="MINGW32"
GET_OS=$(uname -s)
ARCH=$(uname -m)

function update_linux() {
TIMESTAMP=$(date +%Y%M%d%H%M%S)
DIR_DOWNLOAD="/tmp/mgccli/$TIMESTAMP"
mkdir -p $DIR_DOWNLOAD

echo "Baixando a ultima versão"
sleep 2

if [ "$ARCH" == "x86_64" ]; then

curl -L $(curl -s https://api.github.com/repos/MagaluCloud/mgccli/releases/latest|grep browser_download_url|cut -d\: -f2,3|grep -E "(tar.gz)"|grep linux|grep "amd64"|cut -d\" -f2) -o $DIR_DOWNLOAD/mgccli.tar.gz

else

curl -L $(curl -s https://api.github.com/repos/MagaluCloud/mgccli/releases/latest|grep browser_download_url|cut -d\: -f2,3|grep -E "(tar.gz)"|grep linux|grep "arm64"|cut -d\" -f2) -o $DIR_DOWNLOAD/mgccli.tar.gz

fi

echo "Descompactando"
sleep 2

tar xzf $DIR_DOWNLOAD/mgccli.tar.gz -C $DIR_DOWNLOAD

echo "Intalando a ultima versão"
sleep 2

mv $DIR_DOWNLOAD/mgc /usr/bin/mgc
chmod +x /usr/bin/mgc
}

function update_macos() {
TIMESTAMP=$(date +%Y%M%d%H%M%S)
DIR_DOWNLOAD="~/mgccli/$TIMESTAMP"
mkdir -p $DIR_DOWNLOAD

echo "Baixando a ultima versão"
sleep 2

if [ "$ARCH" == "x86_64" ]; then

curl -L $(curl -s https://api.github.com/repos/MagaluCloud/mgccli/releases/latest|grep browser_download_url|cut -d\: -f2,3|grep -E "(tar.gz)"|grep darwin|grep "amd64"|cut -d\" -f2) -o $DIR_DOWNLOAD/mgccli.tar.gz

else

curl -L $(curl -s https://api.github.com/repos/MagaluCloud/mgccli/releases/latest|grep browser_download_url|cut -d\: -f2,3|grep -E "(tar.gz)"|grep darwin|grep "arm64"|cut -d\" -f2) -o $DIR_DOWNLOAD/mgccli.tar.gz

fi

echo "Descompactando"
sleep 2

tar xzf $DIR_DOWNLOAD/mgccli.tar.gz -C $DIR_DOWNLOAD/

echo "Intalando a ultima versão"
sleep 2

mv $DIR_DOWNLOAD//mgc ~/bin/mgc
chmod +x ~/bin/mgc
}

function update_gitbash() {
TIMESTAMP=$(date +%Y%M%d%H%M%S)
DIR_DOWNLOAD="~/mgccli/$TIMESTAMP"
mkdir -p $DIR_DOWNLOAD

echo "Baixando a ultima versão"
sleep 2

if [ "$ARCH" == "x86_64" ]; then

curl -L $(curl -s https://api.github.com/repos/MagaluCloud/mgccli/releases/latest|grep browser_download_url|cut -d\: -f2,3|grep -E "(zip)"|grep windows|grep "amd64"|cut -d\" -f2) -o $DIR_DOWNLOAD/mgccli.zip

else

curl -L $(curl -s https://api.github.com/repos/MagaluCloud/mgccli/releases/latest|grep browser_download_url|cut -d\: -f2,3|grep -E "(zip)"|grep windows|grep "arm64"|cut -d\" -f2) -o $DIR_DOWNLOAD/mgccli.zip

fi


echo "Descompactando a ultima versão"
sleep 2

unzip $DIR_DOWNLOAD/mgccli.zip -d $DIR_DOWNLOAD/

echo "Instalando a ultima versão"
sleep 2

mv $DIR_DOWNLOAD/mgc.exe /usr/local/bin/mgc.exe
chmod +x /usr/local/bin/mgc.exe
}

### Install ON Linux
if [ $GET_OS == $LINUX ]; then
update_linux

if [ $? -eq 0 ]; then
echo "Para testar, use o comando: mgc --help ou mgc --version"
else
echo "Falha na instalação"
fi
fi


### Install ON MacOS
if [ $GET_OS == $MACOS ]; then
update_macos

if [ $? -eq 0 ]; then
echo "Para testar, use o comando: mgc --help ou mgc --version"
else
echo "Falha na instalação"
fi

fi


### Install ON Gitbash
if [ $GET_OS == $GITBASH_01 ] || [ $GET_OS == $GITBASH_02 ]; then
update_gitbash

if [ $? -eq 0 ]; then
echo "Para testar, use o comando: mgc.exe --help ou mgc.exe --version"
else
echo "Falha na instalação"
fi

fi

rm -rf $DIR_DOWNLOAD