refactor
This commit is contained in:
parent
9450d49e8c
commit
04807e032d
|
@ -19,8 +19,6 @@ import (
|
|||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/b3log/wide/conf"
|
||||
"github.com/b3log/wide/session"
|
||||
|
@ -51,8 +49,7 @@ func GoFmtHandler(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
filePath := args["file"].(string)
|
||||
|
||||
apiPath := runtime.GOROOT() + conf.PathSeparator + "src" + conf.PathSeparator + "pkg"
|
||||
if strings.HasPrefix(filePath, apiPath) { // if it is Go API source code
|
||||
if util.Go.IsAPI(filePath) {
|
||||
// ignore it
|
||||
return
|
||||
}
|
||||
|
|
|
@ -21,7 +21,6 @@ import (
|
|||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
|
@ -56,10 +55,7 @@ var apiNode *FileNode
|
|||
|
||||
// initAPINode builds the Go API file node.
|
||||
func initAPINode() {
|
||||
apiPath := runtime.GOROOT() + conf.PathSeparator + "src" + conf.PathSeparator + "pkg" // before Go 1.4
|
||||
if !util.File.IsExist(apiPath) {
|
||||
apiPath = runtime.GOROOT() + conf.PathSeparator + "src" // Go 1.4 and after
|
||||
}
|
||||
apiPath := util.Go.GetAPIPath()
|
||||
|
||||
apiNode = &FileNode{Name: "Go API", Path: apiPath, IconSkin: "ico-ztree-dir-api ", Type: "d",
|
||||
Creatable: false, Removable: false, FileNodes: []*FileNode{}}
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"path"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type mygo struct{}
|
||||
|
||||
// Go utilities.
|
||||
var Go = mygo{}
|
||||
|
||||
// GetAPIPath gets the Go source code path.
|
||||
//
|
||||
// 1. before Go 1.4: $GOROOT/src/pkg
|
||||
// 2. Go 1.4 and after: $GOROOT/src
|
||||
func (*mygo) GetAPIPath() string {
|
||||
ret := runtime.GOROOT() + "/src/pkg" // before Go 1.4
|
||||
if !File.IsExist(ret) {
|
||||
ret = runtime.GOROOT() + "/src" // Go 1.4 and after
|
||||
}
|
||||
|
||||
return path.Clean(ret)
|
||||
}
|
||||
|
||||
// IsAPI determines whether the specified path belongs to Go API.
|
||||
func (*mygo) IsAPI(path string) bool {
|
||||
apiPath := Go.GetAPIPath()
|
||||
|
||||
return strings.HasPrefix(path, apiPath)
|
||||
}
|
Loading…
Reference in New Issue