From 5308c3ce44783d68fa7eed33b5d2fc1619608a75 Mon Sep 17 00:00:00 2001 From: mdouchement Date: Fri, 28 Jan 2022 18:36:22 +0100 Subject: [PATCH] Use the nearest .fichennrc (#6) --- Taskfile.yml | 2 +- cmd/finn/fichenn.go | 83 +++++++++++++++++++++++++++++++-------------- 2 files changed, 59 insertions(+), 26 deletions(-) diff --git a/Taskfile.yml b/Taskfile.yml index d618364..01283a0 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -4,7 +4,7 @@ version: '3' vars: - VERSION: 0.2.2 + VERSION: 0.3.0 REVISION: { sh: git rev-parse HEAD } env: diff --git a/cmd/finn/fichenn.go b/cmd/finn/fichenn.go index ac44f23..4e9f64e 100644 --- a/cmd/finn/fichenn.go +++ b/cmd/finn/fichenn.go @@ -7,9 +7,9 @@ import ( "log" "net/http" "os" - "os/user" "path/filepath" "runtime" + "strings" "time" "github.com/atotto/clipboard" @@ -125,31 +125,9 @@ func (c *controller) download(url string) error { func (c *controller) upload(src string) error { // Configuration - konf := koanf.New(".") - - defaults := map[string]interface{}{ - "passphrase_length": 24, - "storage": "plik", - "clipboard": true, - "plik": map[string]interface{}{ - "url": "https://plik.root.gg", - "ttl": "24h", - "one_shot": false, - }, - } - if err := konf.Load(confmap.Provider(defaults, ""), nil); err != nil { - panic(err) // We need to parse defaults - } - - // Load user configuration - usr, err := user.Current() + konf, err := c.config() if err != nil { - return errors.Wrap(err, "could not get shell user") - } - cfg := filepath.Join(usr.HomeDir, runcom) - - if err := konf.Load(file.Provider(cfg), toml.Parser()); err != nil { - fmt.Println(err, "=> using defaults") + return errors.Wrap(err, "config") } // @@ -208,6 +186,61 @@ func (c *controller) upload(src string) error { return nil } +func (c *controller) config() (*koanf.Koanf, error) { + // Configuration + konf := koanf.New(".") + + defaults := map[string]interface{}{ + "passphrase_length": 24, + "storage": "plik", + "clipboard": true, + "plik": map[string]interface{}{ + "url": "https://plik.root.gg", + "ttl": "24h", + "one_shot": false, + }, + } + if err := konf.Load(confmap.Provider(defaults, ""), nil); err != nil { + panic(err) // We need to parse defaults + } + + // Load user configuration + cfg, err := lookup() + if err != nil { + return nil, errors.Wrap(err, "lookup") + } + fmt.Println("Use:", cfg) + + if err := konf.Load(file.Provider(cfg), toml.Parser()); err != nil { + return nil, errors.Wrap(err, "parsing") + } + + return konf, nil +} + +func lookup() (string, error) { + workdir, err := os.Getwd() + if err != nil { + return "", errors.Wrap(err, "current directory:") + } + + for workdir != "/" || runtime.GOOS == "windows" && strings.HasSuffix(workdir, ":\\") { + filename := filepath.Join(workdir, runcom) + _, err := os.Stat(filename) + if err == nil { + return filename, nil + } + if os.IsNotExist(err) { + workdir = filepath.Dir(workdir) + continue + } + + return "", err + } + + return "", errors.Errorf("no %s found", runcom) +} + func progress(size int64, caption string) *progressbar.ProgressBar { bar := progressbar.NewOptions64(size, progressbar.OptionSetDescription(caption),