Skip to content

Commit

Permalink
Add install script to optimize install (#13)
Browse files Browse the repository at this point in the history
- Add install script to make install in Linux or macOS easier.
- modify the README about how to install.
- fix root command test.
  • Loading branch information
xuanyu66 authored Nov 25, 2022
1 parent cf22bf7 commit d4c8976
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 27 deletions.
22 changes: 2 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,10 @@ The `ticloud` command line tool brings deploy cluster requests, and other TiDB C

## Installation

#### macOS
#### macOS and Linux

- amd64
```
curl -L https://github.com/tidbcloud/tidbcloud-cli/releases/download/v0.1.0-rc1/ticloud_0.1.0-rc1_macos_x86_64.tar.gz | tar -xz
```

- arm64
```
curl -L https://github.com/tidbcloud/tidbcloud-cli/releases/download/v0.1.0-rc1/ticloud_0.1.0-rc1_macos_arm64.tar.gz | tar -xz
```

#### Linux

- amd64
```
curl -L https://github.com/tidbcloud/tidbcloud-cli/releases/download/v0.1.0-rc1/ticloud_0.1.0-rc1_linux_x86_64.tar.gz | tar -xz
```

- arm64
```
curl -L https://github.com/tidbcloud/tidbcloud-cli/releases/download/v0.1.0-rc1/ticloud_0.1.0-rc1_linux_arm64.tar.gz | tar -xz
curl https://raw.githubusercontent.com/tidbcloud/tidbcloud-cli/main/install.sh | sh
```

#### Manually
Expand Down
97 changes: 97 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/bin/sh

version="0.1.0-rc2"

repo='https://github.com/tidbcloud/tidbcloud-cli/releases/download'
if [ -n "$TICLOUD_MIRRORS" ]; then
repo=$TICLOUD_MIRRORS
fi

case $(uname -s) in
Linux|linux) os=linux ;;
Darwin|darwin) os=macos ;;
*) os= ;;
esac

if [ -z "$os" ]; then
echo "OS $(uname -s) not supported." >&2
exit 1
fi

case $(uname -m) in
amd64|x86_64) arch=amd64 ;;
arm64|aarch64) arch=arm64 ;;
*) arch= ;;
esac

if [ -z "$arch" ]; then
echo "Architecture $(uname -m) not supported." >&2
exit 1
fi

if [ -z "$TICLOUD_HOME" ]; then
TICLOUD_HOME=$HOME/.ticloud
fi
bin_dir=$TICLOUD_HOME/bin
mkdir -p "$bin_dir"

install_binary() {
curl -L "$repo/v${version}/ticloud_${version}_${os}_$arch.tar.gz" -o "/tmp/ticloud_${version}_${os}_$arch.tar.gz" || return 1
tar -zxf "/tmp/ticloud_${version}_${os}_$arch.tar.gz" -C "$bin_dir" || return 1
rm "/tmp/ticloud_${version}_${os}_$arch.tar.gz"
return 0
}

check_depends() {
pass=0
command -v curl >/dev/null || {
echo "Dependency check failed: please install 'curl' before proceeding."
pass=1
}
command -v tar >/dev/null || {
echo "Dependency check failed: please install 'tar' before proceeding."
pass=1
}
return $pass
}

if ! check_depends; then
exit 1
fi

if ! install_binary; then
echo "Failed to download and/or extract ticloud archive."
exit 1
fi

chmod 755 "$bin_dir/ticloud"

bold=$(tput bold 2>/dev/null)
sgr0=$(tput sgr0 2>/dev/null)

# Refrence: https://stackoverflow.com/questions/14637979/how-to-permanently-set-path-on-linux-unix
shell=$(echo $SHELL | awk 'BEGIN {FS="/";} { print $NF }')
echo "Detected shell: ${bold}$shell${sgr0}"
if [ -f "${HOME}/.${shell}_profile" ]; then
PROFILE=${HOME}/.${shell}_profile
elif [ -f "${HOME}/.${shell}_login" ]; then
PROFILE=${HOME}/.${shell}_login
elif [ -f "${HOME}/.${shell}rc" ]; then
PROFILE=${HOME}/.${shell}rc
else
PROFILE=${HOME}/.profile
fi
echo "Shell profile: ${bold}$PROFILE${sgr0}"

case :$PATH: in
*:$bin_dir:*) : "PATH already contains $bin_dir" ;;
*) printf '\nexport PATH=%s:$PATH\n' "$bin_dir" >> "$PROFILE"
echo "$PROFILE has been modified to add ticloud to PATH"
echo "open a new terminal or ${bold}source ${PROFILE}${sgr0} to use it"
;;
esac

echo "Installed path: ${bold}$bin_dir/ticloud${sgr0}"
echo "==============================================="
echo "Have a try: ${bold}ticloud${sgr0}"
echo "==============================================="
15 changes: 9 additions & 6 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func Execute(ctx context.Context, ver, commit, buildDate string) {
}

rootCmd := RootCmd(h, ver, commit, buildDate)
initConfig()

err := rootCmd.ExecuteContext(ctx)
if err != nil {
Expand Down Expand Up @@ -124,20 +125,22 @@ func shouldCheckAuth(cmd *cobra.Command) bool {
return true
}

func init() {
cobra.OnInitialize(initConfig)
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
// Find home directory.
home, err := os.UserHomeDir()
cobra.CheckErr(err)
path := home + "/" + config.HomePath
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
color.Red("Failed to create ticloud home directory: %s", err)
os.Exit(1)
}

// Search config in home directory with name ".tidbcloud-cli" (without extension).
viper.AddConfigPath(home)
viper.AddConfigPath(path)
viper.SetConfigType("toml")
viper.SetConfigName(".tidbcloud-cli")
viper.SetConfigName("config")
_ = viper.SafeWriteConfig()
err = viper.ReadInConfig()
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import (
"github.com/spf13/viper"
)

const CliName = "ticloud"
const (
CliName = "ticloud"
HomePath = ".ticloud"
)

type Config struct {
ActiveProfile string
Expand Down

0 comments on commit d4c8976

Please sign in to comment.