about summary refs log tree commit diff
path: root/users/wpcarro/tools/run/main.go
blob: 04906ece91f7fe89297116e1ee9f7266ee17fe54 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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())
}