Skip to content

Commit

Permalink
Merge pull request #8 from marwanhawari/xdg_spec_4
Browse files Browse the repository at this point in the history
Allow user configuration and support XDG spec by default
  • Loading branch information
marwanhawari authored Mar 7, 2022
2 parents 528fe6d + 91aee6d commit f80e9d3
Show file tree
Hide file tree
Showing 16 changed files with 517 additions and 191 deletions.
13 changes: 7 additions & 6 deletions cmd/browse.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import (

// Browse is executed when you run `stew browse`
func Browse(cliInput string) {
sp := constants.LoadingSpinner

stewPath, err := stew.GetStewPath()
stew.CatchAndExit(err)
systemInfo := stew.NewSystemInfo(stewPath)
userOS, userArch, _, systemInfo, err := stew.Initialize()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

sp := constants.LoadingSpinner

userOS := systemInfo.Os
userArch := systemInfo.Arch
stewBinPath := systemInfo.StewBinPath
stewPkgPath := systemInfo.StewPkgPath
stewLockFilePath := systemInfo.StewLockFilePath
Expand Down
37 changes: 37 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cmd

import (
"fmt"
"os"

"github.com/marwanhawari/stew/constants"
stew "github.com/marwanhawari/stew/lib"
)

func Config() {

userOS, _, stewConfig, _, err := stew.Initialize()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

stewConfigFilePath, err := stew.GetStewConfigFilePath(userOS)
stew.CatchAndExit(err)

inputStewPath, err := stew.PromptInput("Set the stewPath. This will contain all stew data other than the binaries.", stewConfig.StewPath)
stew.CatchAndExit(err)
inputStewBinPath, err := stew.PromptInput("Set the stewBinPath. This is where the binaries will be installed by stew.", stewConfig.StewBinPath)
stew.CatchAndExit(err)

fullStewPath, err := stew.ResolveTilde(inputStewPath)
stew.CatchAndExit(err)
fullStewBinPath, err := stew.ResolveTilde(inputStewBinPath)
stew.CatchAndExit(err)

newStewConfig := stew.StewConfig{StewPath: fullStewPath, StewBinPath: fullStewBinPath}
err = stew.WriteStewConfigJSON(newStewConfig, stewConfigFilePath)
stew.CatchAndExit(err)

fmt.Printf("📄 Updated %v\n", constants.GreenColor(stewConfigFilePath))
}
13 changes: 7 additions & 6 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ import (
// Install is executed when you run `stew install`
func Install(cliInputs []string) {
var err error

userOS, userArch, _, systemInfo, err := stew.Initialize()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

for _, cliInput := range cliInputs {
if strings.Contains(cliInput, "Stewfile") {
cliInputs, err = stew.ReadStewfileContents(cliInput)
Expand All @@ -28,12 +35,6 @@ func Install(cliInputs []string) {
for _, cliInput := range cliInputs {
sp := constants.LoadingSpinner

stewPath, err := stew.GetStewPath()
stew.CatchAndExit(err)
systemInfo := stew.NewSystemInfo(stewPath)

userOS := systemInfo.Os
userArch := systemInfo.Arch
stewBinPath := systemInfo.StewBinPath
stewPkgPath := systemInfo.StewPkgPath
stewLockFilePath := systemInfo.StewLockFilePath
Expand Down
11 changes: 6 additions & 5 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ package cmd

import (
"fmt"
"os"

stew "github.com/marwanhawari/stew/lib"
)

// List is executed when you run `stew list`
func List(cliTagsFlag bool, cliAssetsFlag bool) {

stewPath, err := stew.GetStewPath()
stew.CatchAndExit(err)
systemInfo := stew.NewSystemInfo(stewPath)
userOS, userArch, _, systemInfo, err := stew.Initialize()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

userOS := systemInfo.Os
userArch := systemInfo.Arch
stewLockFilePath := systemInfo.StewLockFilePath

lockFile, err := stew.NewLockFile(stewLockFilePath, userOS, userArch)
Expand Down
12 changes: 6 additions & 6 deletions cmd/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import (
// Rename is executed when you run `stew rename`
func Rename(cliInput string) {

err := stew.ValidateCLIInput(cliInput)
stew.CatchAndExit(err)
userOS, userArch, _, systemInfo, err := stew.Initialize()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

stewPath, err := stew.GetStewPath()
err = stew.ValidateCLIInput(cliInput)
stew.CatchAndExit(err)
systemInfo := stew.NewSystemInfo(stewPath)

userOS := systemInfo.Os
userArch := systemInfo.Arch
stewBinPath := systemInfo.StewBinPath
stewLockFilePath := systemInfo.StewLockFilePath

Expand Down
14 changes: 8 additions & 6 deletions cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,28 @@ package cmd

import (
"fmt"
"os"

"github.com/marwanhawari/stew/constants"
stew "github.com/marwanhawari/stew/lib"
)

// Uninstall is executed when you run `stew uninstall`
func Uninstall(cliFlag bool, binaryName string) {

userOS, userArch, _, systemInfo, err := stew.Initialize()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

if cliFlag && binaryName != "" {
stew.CatchAndExit(stew.CLIFlagAndInputError{})
} else if !cliFlag {
err := stew.ValidateCLIInput(binaryName)
stew.CatchAndExit(err)
}

stewPath, err := stew.GetStewPath()
stew.CatchAndExit(err)
systemInfo := stew.NewSystemInfo(stewPath)

userOS := systemInfo.Os
userArch := systemInfo.Arch
stewBinPath := systemInfo.StewBinPath
stewPkgPath := systemInfo.StewPkgPath
stewLockFilePath := systemInfo.StewLockFilePath
Expand Down
13 changes: 7 additions & 6 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import (

// Upgrade is executed when you run `stew upgrade`
func Upgrade(cliFlag bool, binaryName string) {

userOS, userArch, _, systemInfo, err := stew.Initialize()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

if cliFlag && binaryName != "" {
stew.CatchAndExit(stew.CLIFlagAndInputError{})
} else if !cliFlag {
Expand All @@ -20,12 +27,6 @@ func Upgrade(cliFlag bool, binaryName string) {

sp := constants.LoadingSpinner

stewPath, err := stew.GetStewPath()
stew.CatchAndExit(err)
systemInfo := stew.NewSystemInfo(stewPath)

userOS := systemInfo.Os
userArch := systemInfo.Arch
stewPkgPath := systemInfo.StewPkgPath
stewTmpPath := systemInfo.StewTmpPath
stewLockFilePath := systemInfo.StewLockFilePath
Expand Down
109 changes: 95 additions & 14 deletions install.sh
Original file line number Diff line number Diff line change
@@ -1,25 +1,56 @@
#!/usr/bin/env sh
#!/usr/bin/env bash

# This install script does 3 things:
# 1. Create the ~/.stew directory structure
# 1. Create the stew directory structure
# 2. Download the stew binary
# 3. Add ~/.stew/bin to PATH in ~/.zshrc or ~/.bashrc
# 3. Add the stew binary path to PATH in ~/.zshrc or ~/.bashrc

os=""
arch=""
exe=""
defaultStewPath=""
configPath=""

# Detect os
case "$(uname -s)" in
Darwin)
os="darwin"

if [ -z "$XDG_DATA_HOME" ]
then
defaultStewPath="$HOME/.local/share/stew"
else
defaultStewPath="$XDG_DATA_HOME/stew"
fi

if [ -z "$XDG_CONFIG_HOME" ]
then
configPath="$HOME/.config/stew"
else
configPath="$XDG_CONFIG_HOME/stew"
fi
;;
Linux)
os="linux"
if [ -z "$XDG_DATA_HOME" ]
then
defaultStewPath="$HOME/.local/share/stew"
else
defaultStewPath="$XDG_DATA_HOME/stew"
fi

if [ -z "$XDG_CONFIG_HOME" ]
then
configPath="$HOME/.config/stew"
else
configPath="$XDG_CONFIG_HOME/stew"
fi
;;
CYGWIN*|MSYS*|MINGW*)
os="windows"
exe=".exe"
defaultStewPath="$HOME/AppData/Local/stew"
configPath="$HOME/AppData/Local/stew/Config"
;;
esac

Expand All @@ -38,27 +69,77 @@ esac

if [ "$os" = "" ] || [ "$arch" = "" ]
then
echo "\033[31m\033[1mError:\033[0m Your current OS/arch is not supported by stew"
echo ""
echo "|||||||||||||||||||||"
echo "|| Error ||"
echo "|||||||||||||||||||||"
echo ""
echo "Your current OS/arch is not supported by stew"
echo ""
exit 1
fi

# 1. Create the ~/.stew directory structure
mkdir -p "$HOME"/.stew/bin
mkdir -p "$HOME"/.stew/pkg
# 1. Create the stew directory structure
stewPath=""
stewBinPath=""

read -r -t 60 -p "Set the stewPath. This will contain all stew data other than the binaries. (${defaultStewPath}): " stewPathInput
if [ -z "$stewPathInput" ]
then
stewPath="${defaultStewPath}"
else
stewPath="${stewPathInput/#~/$HOME}"
stewPath="${stewPath/#\$HOME/$HOME}"
stewPath="${stewPath/#\$PWD/$PWD}"
if [ -x "$(command -v dirname)" ] && [ -x "$(command -v basename)" ]
then
stewPath="$(cd "$(dirname "$stewPath")" || exit; pwd)/$(basename "$stewPath")"
fi
fi

read -r -t 60 -p "Set the stewBinPath. This is where the binaries will be installed by stew. (${defaultStewPath}/bin): " stewBinPathInput
if [ -z "$stewBinPathInput" ]
then
stewBinPath="${defaultStewPath}/bin"
else
stewBinPath="${stewBinPathInput/#~/$HOME}"
stewBinPath="${stewBinPath/#\$HOME/$HOME}"
stewBinPath="${stewBinPath/#\$PWD/$PWD}"
if [ -x "$(command -v dirname)" ] && [ -x "$(command -v basename)" ]
then
stewBinPath="$(cd "$(dirname "$stewBinPath")" || exit; pwd)/$(basename "$stewBinPath")"
fi
fi

mkdir -p "${stewPath}/bin"
mkdir -p "${stewPath}/pkg"
mkdir -p "${stewBinPath}"
mkdir -p "${configPath}"

echo "{
\"stewPath\": \"${stewPath}\",
\"stewBinPath\": \"${stewBinPath}\"
}" > "${configPath}/config.json"

# 2. Download the stew binary
curl -o "$HOME"/.stew/bin/stew${exe} -fsSL https://github.com/marwanhawari/stew/releases/latest/download/stew-${os}-${arch}${exe}
chmod +x "$HOME"/.stew/bin/stew${exe}
curl -o "${stewBinPath}/stew${exe}" -fsSL https://github.com/marwanhawari/stew/releases/latest/download/stew-${os}-${arch}${exe}
chmod +x "${stewBinPath}/stew${exe}"

# 3. Add ~/.stew/bin to PATH in ~/.zshrc or ~/.bashrc
# 3. Add the stew binary path to PATH in ~/.zshrc or ~/.bashrc
if [ -f "$HOME"/.zshrc ]
then
echo 'export PATH="$HOME/.stew/bin:$PATH"' >> "$HOME"/.zshrc
echo "export PATH=\"${stewBinPath}:\$PATH\"" >> "$HOME"/.zshrc
elif [ -f "$HOME"/.bashrc ]
then
echo 'export PATH="$HOME/.stew/bin:$PATH"' >> "$HOME"/.bashrc
echo "export PATH=\"${stewBinPath}:\$PATH\"" >> "$HOME"/.bashrc
else
echo "Make sure to add $HOME/.stew/bin to PATH"
echo "Make sure to add ${stewBinPath} to PATH"
fi

echo "\033[32m\033[1mSuccess:\033[0m Start a new terminal session to start using stew"
echo ""
echo "|||||||||||||||||||||"
echo "|| Success ||"
echo "|||||||||||||||||||||"
echo ""
echo "Start a new terminal session to start using stew"
echo ""
Loading

0 comments on commit f80e9d3

Please sign in to comment.