diff --git a/README.md b/README.md index 6fbba544..86f6fa9c 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/install.sh b/install.sh new file mode 100644 index 00000000..26749b19 --- /dev/null +++ b/install.sh @@ -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 "===============================================" diff --git a/internal/cli/root.go b/internal/cli/root.go index 745bd794..b2ba73a5 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -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 { @@ -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 { diff --git a/internal/config/config.go b/internal/config/config.go index 7736b33d..174f7668 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -24,7 +24,10 @@ import ( "github.com/spf13/viper" ) -const CliName = "ticloud" +const ( + CliName = "ticloud" + HomePath = ".ticloud" +) type Config struct { ActiveProfile string