Skip to content

Commit

Permalink
Allows to set manual download dir if can't be automatically detected (#…
Browse files Browse the repository at this point in the history
…108)

* Allows to set manual download dir if can't be automatically detected

This commits allows the user to specify a download directorty maunally
if it can't be automatically detected by traversing the PATH folders

Ideally it fixes 107

* Fix typo

* Implement default path configuration for windows
  • Loading branch information
marcosnils authored Oct 24, 2021
1 parent 82acb49 commit eb703a2
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 13 deletions.
5 changes: 3 additions & 2 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package cmd
import (
"bufio"
"fmt"
"os"
"strings"

"github.com/apex/log"
"github.com/fatih/color"
"github.com/hashicorp/go-version"
"github.com/marcosnils/bin/pkg/config"
"github.com/marcosnils/bin/pkg/providers"
"github.com/spf13/cobra"
"os"
"strings"
)

type updateCmd struct {
Expand Down
23 changes: 22 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package config

import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
"path"
"path/filepath"
"runtime"
"strings"

"github.com/apex/log"
)
Expand Down Expand Up @@ -64,9 +66,28 @@ func CheckAndLoad() error {
if err := write(); err != nil {
return err
}
} else {
for {
log.Info("Could not find a PATH directory automatically, falling back to manual selection")
reader := bufio.NewReader(os.Stdin)
var response string
fmt.Printf("\nPlease specify a download directory: ")
response, err := reader.ReadString('\n')
if err != nil {
return fmt.Errorf("Invalid input")
}

if err = checkDirExistsAndWritable(strings.TrimSpace(response)); err != nil {
log.Debugf("Could not set download directory [%s]: [%v]", response, err)
// Keep looping until writable and existing dir is selected
continue
}

cfg.DefaultPath = response

}
}
log.Debugf("Download path set to %s", cfg.DefaultPath)

return nil
}

Expand Down
18 changes: 16 additions & 2 deletions pkg/config/config_unix.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//go:build !windows
// +build !windows

package config

import (
"errors"
"fmt"
"os"
"strings"
Expand All @@ -24,8 +26,7 @@ func getDefaultPath() (string, error) {
for _, p := range strings.Split(penv, ":") {
log.Debugf("Checking path %s", p)

//TODO make this work in non unix platforms
err := unix.Access(p, unix.W_OK)
err := checkDirExistsAndWritable(p)

if err != nil {
log.Debugf("Error [%s] checking path", err)
Expand All @@ -37,9 +38,22 @@ func getDefaultPath() (string, error) {

}

// TODO this logic is also duplicated in the windows config. We should
// move it to config.go
choice, err := options.Select("Pick a default download dir: ", opts)
if err != nil {
return "", err
}
return choice.(fmt.Stringer).String(), nil
}

func checkDirExistsAndWritable(dir string) error {
if fi, err := os.Stat(dir); err != nil {
return fmt.Errorf("Error setting download path [%w]", err)
} else if !fi.IsDir() {
return errors.New("Download path is not a directory")
}
//TODO make this work in non unix platforms
err := unix.Access(dir, unix.W_OK)
return err
}
27 changes: 19 additions & 8 deletions pkg/config/config_windows.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"errors"
"fmt"
"os"
"strings"
Expand All @@ -19,19 +20,14 @@ func getDefaultPath() (string, error) {
log.Debugf("User PATH is [%s]", penv)
opts := []fmt.Stringer{}
for _, p := range strings.Split(penv, ";") {
log.Debugf("Checking path %s", p)

info, err := os.Stat(p)
if err != nil || !info.IsDir() {
if err := checkDirExistsAndWritable(p); err != nil {
log.Debugf("Error [%s] checking path", err)
continue
}

// Check if the user bit is enabled in file permission
if info.Mode().Perm()&(1<<(uint(7))) != 0 {
log.Debugf("%s seems to be a dir and writable, adding option.", p)
opts = append(opts, options.LiteralStringer(p))
}
log.Debugf("%s seems to be a dir and writable, adding option.", p)
opts = append(opts, options.LiteralStringer(p))

}

Expand All @@ -42,3 +38,18 @@ func getDefaultPath() (string, error) {
return choice.(fmt.Stringer).String(), nil

}

func checkDirExistsAndWritable(dir string) error {
log.Debugf("Checking path %s", dir)
info, err := os.Stat(dir)
if err != nil || !info.IsDir() {
return err
}

// Check if the user bit is enabled in file permission
if info.Mode().Perm()&(1<<(uint(7))) == 0 {
return errors.New(fmt.Sprintf("Dir %s is not writable", dir))
}
return nil

}

0 comments on commit eb703a2

Please sign in to comment.