From 9a6d92eefd70b3a25f6df1208dfb9224749eccd5 Mon Sep 17 00:00:00 2001 From: vsariola <5684185+vsariola@users.noreply.github.com> Date: Sat, 10 Apr 2021 16:10:27 +0300 Subject: [PATCH] feat(cmd): make current directory the default output directory in command line tools --- cmd/sointu-compile/main.go | 10 +++++++++- cmd/sointu-play/main.go | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/cmd/sointu-compile/main.go b/cmd/sointu-compile/main.go index 4b801fc..77ed1fa 100644 --- a/cmd/sointu-compile/main.go +++ b/cmd/sointu-compile/main.go @@ -68,7 +68,8 @@ func main() { fmt.Print(string(contents)) return nil } - dir, name := filepath.Split(filename) + _, name := filepath.Split(filename) + var dir string if *outPath != "" { // check if it's an already existing directory and the user just forgot trailing slash if info, err := os.Stat(*outPath); err == nil && info.IsDir() { @@ -83,6 +84,13 @@ func main() { } } } + if dir == "" { + var err error + dir, err = os.Getwd() + if err != nil { + return fmt.Errorf("could not get working directory, specify the output directory explicitly: %v", err) + } + } name = strings.TrimSuffix(name, filepath.Ext(name)) + extension f := filepath.Join(dir, name) original, err := ioutil.ReadFile(f) diff --git a/cmd/sointu-play/main.go b/cmd/sointu-play/main.go index da00820..68d7080 100644 --- a/cmd/sointu-play/main.go +++ b/cmd/sointu-play/main.go @@ -56,10 +56,18 @@ func main() { fmt.Print(contents) return nil } - dir, name := filepath.Split(filename) + _, name := filepath.Split(filename) + var dir string if *directory != "" { dir = *directory } + if dir == "" { + var err error + dir, err = os.Getwd() + if err != nil { + return fmt.Errorf("could not get working directory, specify the output directory explicitly: %v", err) + } + } name = strings.TrimSuffix(name, filepath.Ext(name)) + extension f := filepath.Join(dir, name) if dir != "" {