Quellcode durchsuchen

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 vor 11 Jahren
Ursprung
Commit
cc879e1b96
2 geänderte Dateien mit 27 neuen und 9 gelöschten Zeilen
  1. 7 0
      go/fileutil/fileutil.go
  2. 20 9
      go/qst.go

+ 7 - 0
go/fileutil/fileutil.go

44
	isExecutable := info.Mode() & 0111
44
	isExecutable := info.Mode() & 0111
45
	return isExecutable != 0 && !info.IsDir()
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
	"strings"
12
	"strings"
13
	"syscall"
13
	"syscall"
14
	"time"
14
	"time"
15
16
	"./detect"
17
	"./fileutil"
15
)
18
)
16
19
17
/*
20
/*
63
	}
66
	}
64
67
65
	file := args[0]
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
	var cmd string
74
	var cmd string
72
	if command != nil && strings.TrimSpace(*command) != "" {
75
	if command != nil && strings.TrimSpace(*command) != "" {
73
		cmd = strings.Replace(*command, "{file}", file, -1)
76
		cmd = *command
74
	} else {
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
		if !found {
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
	log.Printf("command to run: `%s'", cmd)
93
	log.Printf("command to run: `%s'", cmd)
83
94