Skip to content

Commit

Permalink
add the ability to configure stew
Browse files Browse the repository at this point in the history
  • Loading branch information
marwanhawari committed Mar 7, 2022
1 parent 528fe6d commit a7e8b6a
Show file tree
Hide file tree
Showing 11 changed files with 451 additions and 313 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
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
231 changes: 231 additions & 0 deletions lib/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
package stew

import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"runtime"
)

// GetDefaultStewPath will return the default path to the top-level stew directory
func GetDefaultStewPath(userOS string) (string, error) {

homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}

var stewPath string
switch userOS {
case "windows":
stewPath = filepath.Join(homeDir, "AppData", "Local", "stew")
default:
xdgDataHomePath := os.Getenv("XDG_DATA_HOME")
if xdgDataHomePath == "" {
stewPath = filepath.Join(homeDir, ".local", "share", "stew")
} else {
stewPath = filepath.Join(xdgDataHomePath, "stew")

}
}

return stewPath, nil
}

// GetDefaultStewBinPath will return the default path where binaries are installed by stew
func GetDefaultStewBinPath(userOS string) (string, error) {

homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}

var stewBinPath string
switch userOS {
case "windows":
stewBinPath = filepath.Join(homeDir, "AppData", "Local", "stew", "bin")
default:
xdgDataHomePath := os.Getenv("XDG_DATA_HOME")
if xdgDataHomePath == "" {
stewBinPath = filepath.Join(homeDir, ".local", "share", "stew", "bin")
} else {
stewBinPath = filepath.Join(xdgDataHomePath, "stew", "bin")

}
}

return stewBinPath, nil
}

// GetStewConfigFilePath will return the stew config file path
func GetStewConfigFilePath(userOS string) (string, error) {

homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}

var stewConfigFilePath string
switch userOS {
case "windows":
stewConfigFilePath = filepath.Join(homeDir, "AppData", "Local", "stew", "Config", "config.json")
default:
xdgConfigHomePath := os.Getenv("XDG_CONFIG_HOME")
if xdgConfigHomePath == "" {
stewConfigFilePath = filepath.Join(xdgConfigHomePath, "stew", "config.json")
} else {
stewConfigFilePath = filepath.Join(homeDir, ".config", "stew", "config.json")
}
}

return stewConfigFilePath, nil
}

// StewConfig contains all the stew configuration data
type StewConfig struct {
StewPath string `json:"stewPath"`
StewBinPath string `json:"stewBinPath"`
}

func readStewConfigJSON(stewConfigFilePath string) (StewConfig, error) {

stewConfigFileBytes, err := ioutil.ReadFile(stewConfigFilePath)
if err != nil {
return StewConfig{}, err
}

var stewConfig StewConfig
err = json.Unmarshal(stewConfigFileBytes, &stewConfig)
if err != nil {
return StewConfig{}, err
}

return stewConfig, nil
}

// WriteStewConfigJSON will write the config JSON file
func WriteStewConfigJSON(stewConfigFileJSON StewConfig, outputPath string) error {

stewConfigFileBytes, err := json.MarshalIndent(stewConfigFileJSON, "", "\t")
if err != nil {
return err
}

err = ioutil.WriteFile(outputPath, stewConfigFileBytes, 0644)
if err != nil {
return err
}

return nil
}

// NewStewConfig creates a new instance of the StewConfig struct
func NewStewConfig(userOS string) (StewConfig, error) {
var stewConfig StewConfig

stewConfigFilePath, err := GetStewConfigFilePath(userOS)
if err != nil {
return StewConfig{}, err
}
defaultStewPath, err := GetDefaultStewPath(userOS)
if err != nil {
return StewConfig{}, err
}
defaultStewBinPath, err := GetDefaultStewBinPath(userOS)
if err != nil {
return StewConfig{}, err
}

configExists, err := PathExists(stewConfigFilePath)
if err != nil {
return StewConfig{}, err
}
if configExists {
stewConfig, err = readStewConfigJSON(stewConfigFilePath)
if err != nil {
return StewConfig{}, err
}
}

if stewConfig.StewPath == "" {
stewConfig.StewPath = defaultStewPath
}

if stewConfig.StewBinPath == "" {
stewConfig.StewBinPath = defaultStewBinPath
}

err = createStewDirsAndFiles(stewConfig, stewConfigFilePath)
if err != nil {
return StewConfig{}, err
}

return stewConfig, nil
}

func createStewDirsAndFiles(stewConfig StewConfig, stewConfigFilePath string) error {
var err error

err = os.MkdirAll(stewConfig.StewPath, 0755)
if err != nil {
return err
}
err = os.MkdirAll(filepath.Join(stewConfig.StewPath, "bin"), 0755)
if err != nil {
return err
}
err = os.MkdirAll(filepath.Join(stewConfig.StewPath, "pkg"), 0755)
if err != nil {
return err
}

err = os.MkdirAll(stewConfig.StewBinPath, 0755)
if err != nil {
return err
}

err = os.MkdirAll(filepath.Dir(stewConfigFilePath), 0755)
if err != nil {
return err
}
err = WriteStewConfigJSON(stewConfig, stewConfigFilePath)
if err != nil {
return err
}

return nil
}

// SystemInfo contains system specific info like OS, arch, and ~/.stew paths
type SystemInfo struct {
StewPath string
StewBinPath string
StewPkgPath string
StewLockFilePath string
StewTmpPath string
}

// NewSystemInfo creates a new instance of the SystemInfo struct
func NewSystemInfo(stewConfig StewConfig) SystemInfo {
var systemInfo SystemInfo
systemInfo.StewPath = stewConfig.StewPath
systemInfo.StewBinPath = stewConfig.StewBinPath
systemInfo.StewPkgPath = filepath.Join(stewConfig.StewPath, "pkg")
systemInfo.StewLockFilePath = filepath.Join(stewConfig.StewPath, "Stewfile.lock.json")
systemInfo.StewTmpPath = filepath.Join(stewConfig.StewPath, "tmp")
return systemInfo
}

// Initialize returns pertinent initialization information like OS, arch, configuration, and system info
func Initialize() (string, string, StewConfig, SystemInfo, error) {
userOS := runtime.GOOS
userArch := runtime.GOARCH
stewConfig, err := NewStewConfig(userOS)
if err != nil {
return "", "", StewConfig{}, SystemInfo{}, err
}
systemInfo := NewSystemInfo(stewConfig)

return userOS, userArch, stewConfig, systemInfo, nil
}
Loading

0 comments on commit a7e8b6a

Please sign in to comment.