diff options
author | William Carroll <wpcarro@gmail.com> | 2020-02-11T17·04+0000 |
---|---|---|
committer | William Carroll <wpcarro@gmail.com> | 2020-02-11T17·11+0000 |
commit | 3afb56a5e5274611c3c5d0f7e2f4a8965575ff40 (patch) | |
tree | 34f883eb1f1342ddb6c93d5f0ab6d4e59739d8b9 /run/main.go | |
parent | a50153c1419dd7dd1a7e3e79a016fa84e42accbb (diff) |
Begin supporting run
I'd like to be able to just call `run file.py` and have a program DWIM. I'm working on run as a step in this direction. Define a simple configuration that maps file extensions to template strings where "$file" is replaced with the argv[1]. It basically works but there are outstanding TODOs. See the README and source code for more information.
Diffstat (limited to 'run/main.go')
-rw-r--r-- | run/main.go | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/run/main.go b/run/main.go new file mode 100644 index 000000000000..04906ece91f7 --- /dev/null +++ b/run/main.go @@ -0,0 +1,49 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "os" + "os/exec" + "path/filepath" + "strings" + "utils" +) + +func main() { + if len(os.Args) != 2 { + log.Fatal("You can only call run with a single file at a time.") + } + + rulesPath := utils.Resolve("run.json", []string{"/home/wpcarro/.config/run/run.json"}) + b, err := ioutil.ReadFile(rulesPath) + if err != nil { + log.Fatal("Could not locate a run.json file: ", err) + } + rules := map[string]string{} + err = json.Unmarshal(b, &rules) + if err != nil { + log.Fatal("Could not decode run.json as JSON: ", err) + } + + fileName := os.Args[1] + ext := filepath.Ext(fileName) + cmd, ok := rules[ext] + + if !ok { + log.Fatalf("No rules for extension, %s, have been defined.", ext) + } + + // TODO(wpcarro): Support more sophisticated parsing than just string + // splitting. To handle 'cases like this'. + tokens := strings.Split(strings.Replace(cmd, "$file", fileName, 1), " ") + c := exec.Command(tokens[0], tokens[1:]...) + err = c.Start() + // TODO(wpcarro): Forward STDERR and STDOUT. + if err != nil { + log.Fatal(err) + } + fmt.Println(c.Wait()) +} |