Ver Código Fonte

use detect in qst.

this also changes the behaviour to chdir into the directory of the file
given, which may or may not be good. but i think it makes sense for a
lot of projects.

yet, we might want to cd to the *root* of the project sometimes, but
specify a file in it. not now, but probably should keep this in mind.
Lucas Stadler 11 anos atrás
pai
commit
cc879e1b96
2 arquivos alterados com 27 adições e 9 exclusões
  1. 7 0
      go/fileutil/fileutil.go
  2. 20 9
      go/qst.go

+ 7 - 0
go/fileutil/fileutil.go

@ -44,3 +44,10 @@ func IsExecutable(file string) bool {
44 44
	isExecutable := info.Mode() & 0111
45 45
	return isExecutable != 0 && !info.IsDir()
46 46
}
47
48
func Dir(file string) string {
49
	if IsDir(file) {
50
		return file
51
	}
52
	return path.Dir(file)
53
}

+ 20 - 9
go/qst.go

@ -12,6 +12,9 @@ import (
12 12
	"strings"
13 13
	"syscall"
14 14
	"time"
15
16
	"./detect"
17
	"./fileutil"
15 18
)
16 19
17 20
/*
@ -63,21 +66,29 @@ func main() {
63 66
	}
64 67
65 68
	file := args[0]
66
	if !isFile(file) {
67
		fmt.Fprintf(os.Stderr, "Error: %s is not a file.\n", file)
68
		os.Exit(1)
69
	}
69
	// if !isFile(file) {
70
	// 	fmt.Fprintf(os.Stderr, "Error: %s is not a file.\n", file)
71
	// 	os.Exit(1)
72
	// }
70 73
71 74
	var cmd string
72 75
	if command != nil && strings.TrimSpace(*command) != "" {
73
		cmd = strings.Replace(*command, "{file}", file, -1)
76
		cmd = *command
74 77
	} else {
75
		ext := path.Ext(file)
76
		fn, found := mappings[ext]
78
		project, err := detect.Detect(file)
79
		if err != nil {
80
			log.Fatal("error: ", err)
81
		}
82
		log.Printf("detected a %s project", project.Id)
83
		projectCmd, found := project.Commands["run"]
77 84
		if !found {
78
			log.Fatalf("error: no mapping found for `%s'", file)
85
			log.Fatalf("%s doesn't support `run'", project.Id)
79 86
		}
80
		cmd = fn(file)
87
		cmd = projectCmd
88
	}
89
	cmd = strings.Replace(cmd, "{file}", file, -1)
90
	if err := os.Chdir(fileutil.Dir(file)); err != nil {
91
		log.Fatal(err)
81 92
	}
82 93
	log.Printf("command to run: `%s'", cmd)
83 94