Skip to content

Commit

Permalink
feat: allow setting data directory with env var
Browse files Browse the repository at this point in the history
  • Loading branch information
mworzala committed May 15, 2024
1 parent 875158e commit 58b4025
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ make build
## Usage
todo

It is possibly to change the data directory by setting `MC_CLI_DATA_DIR` to the directory.
Files will be placed directly in this directory.

## Automation
todo discuss output options, non interactive mode, etc

Expand Down
30 changes: 16 additions & 14 deletions internal/pkg/platform/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,26 @@ import (
// if the config directory cannot be found.
//
// If the directory does not exist, it will be created.
func GetConfigDir(indev bool) (string, error) {
var err error
var configDir string
if indev {
configDir, err = os.Getwd()
if err != nil {
return "", err
}
func GetConfigDir(indev bool) (dataDir string, err error) {

if configDir, ok := os.LookupEnv("MC_CLI_DATA_DIR"); ok {
dataDir = configDir
} else {
configDir, err = os.UserConfigDir()
if err != nil {
return "", err
if indev {
configDir, err = os.Getwd()
if err != nil {
return "", err
}
} else {
configDir, err = os.UserConfigDir()
if err != nil {
return "", err
}
}
dataDir = path.Join(configDir, "mc-cli")
}

dataDir := path.Join(configDir, "mc-cli")

if _, err := os.Stat(dataDir); errors.Is(err, fs.ErrNotExist) {
if _, err = os.Stat(dataDir); errors.Is(err, fs.ErrNotExist) {
err = os.MkdirAll(dataDir, 0755)
if err != nil {
return "", fmt.Errorf("unable to create data directory: %w", err)
Expand Down

0 comments on commit 58b4025

Please sign in to comment.