Ejemplo n.º 1
0
func runOracle(cmd *command.Command, args []string) error {
	if len(args) < 2 {
		cmd.Usage()
		return os.ErrInvalid
	}
	if os.Getenv("GOMAXPROCS") == "" {
		n := runtime.NumCPU()
		if n < 4 {
			n = 4
		}
		runtime.GOMAXPROCS(n)
	}
	mode := args[0]
	args = args[1:]
	if args[0] == "." {
		pkgPath, err := os.Getwd()
		if err != nil {
			log.Fatalln(err)
		}
		pkg, err := build.Default.ImportDir(pkgPath, 0)
		if err != nil {
			log.Fatalln(err)
		}
		args = pkg.GoFiles
		//log.Println(pkg.ImportPath)
		if pkg.ImportPath != "." && pkg.ImportPath != "" {
			args = []string{pkg.ImportPath}
		}
	}
	query := oracle.Query{
		Mode:       mode,
		Pos:        oraclePos,
		Build:      &build.Default,
		Scope:      args,
		PTALog:     nil,
		Reflection: oracleReflect,
	}

	if err := oracle.Run(&query); err != nil {
		fmt.Fprintf(os.Stderr, "oracle: %s.\n", err)
		return err
	}

	if mode == "referrers" {
		ref := query.Serial().Referrers
		if ref != nil {
			fmt.Fprintln(os.Stdout, ref.Desc)
			fmt.Fprintln(os.Stdout, ref.ObjPos)
			for _, v := range ref.Refs {
				fmt.Fprintln(os.Stdout, v)
			}
		}
	} else {
		query.WriteTo(os.Stdout)
	}
	return nil
}
Ejemplo n.º 2
0
func runPkgs(cmd *command.Command, args []string) error {
	runtime.GOMAXPROCS(runtime.NumCPU())
	if len(args) != 0 {
		cmd.Usage()
		return os.ErrInvalid
	}
	//pkgIndexOnce.Do(loadPkgsList)
	var pp PathPkgsIndex
	pp.LoadIndex()
	pp.Sort()
	if pkgsList {
		for _, pi := range pp.indexs {
			for _, pkg := range pi.pkgs {
				if pkgsPkgOnly && pkg.IsCommand() {
					continue
				}
				if pkgsJson {
					var p GoPackage
					p.copyBuild(pkg)
					b, err := json.MarshalIndent(&p, "", "\t")
					if err == nil {
						cmd.Stdout.Write(b)
						cmd.Stdout.Write([]byte{'\n'})
					}
				} else {
					cmd.Println(pkg.ImportPath)
				}
			}
		}
	} else if pkgsFind != "" {
		for _, pi := range pp.indexs {
			for _, pkg := range pi.pkgs {
				if pkg.Name == pkgsFind {
					if pkgsPkgOnly && pkg.IsCommand() {
						continue
					}
					if pkgsJson {
						var p GoPackage
						p.copyBuild(pkg)
						b, err := json.MarshalIndent(p, "", "\t")
						if err == nil {
							cmd.Stdout.Write(b)
							cmd.Stdout.Write([]byte{'\n'})
						}
					} else {
						cmd.Println(pkg.Name)
					}
					break
				}
			}
		}
	}
	return nil
}
Ejemplo n.º 3
0
func runCmd(cmd *command.Command, args []string) error {
	if len(args) == 0 {
		cmd.Usage()
		return os.ErrInvalid
	}
	if execWorkPath == "" {
		var err error
		execWorkPath, err = os.Getwd()
		if err != nil {
			fmt.Fprintf(os.Stderr, "liteide_stub exec: os.Getwd() false\n")
			command.SetExitStatus(3)
			command.Exit()
			return err
		}
	}
	fileName := args[0]

	filePath, err := exec.LookPath(fileName)
	if err != nil {
		filePath, err = exec.LookPath("./" + fileName)
	}
	if err != nil {
		fmt.Fprintf(os.Stderr, "liteide_stub exec: file %s not found\n", fileName)
		command.SetExitStatus(3)
		command.Exit()
	}

	fmt.Println("Starting Process", filePath, strings.Join(args[1:], " "), "...")

	command := exec.Command(filePath, args[1:]...)
	command.Dir = execWorkPath
	command.Stdin = os.Stdin
	command.Stdout = os.Stdout
	command.Stderr = os.Stderr

	err = command.Run()

	if err != nil {
		fmt.Println("\nEnd Process", err)
	} else {
		fmt.Println("\nEnd Process", "exit status 0")
	}

	exitWaitEnter()
	return nil
}
Ejemplo n.º 4
0
func runDoc(cmd *command.Command, args []string) error {
	if !(constantFlag || functionFlag || interfaceFlag || methodFlag || packageFlag || structFlag || typeFlag || variableFlag) { // none set
		constantFlag = true
		functionFlag = true
		methodFlag = true
		// Not package! It's special.
		typeFlag = true
		variableFlag = true
	}
	if !(docFlag || srcFlag || urlFlag) {
		docFlag = true
		srcFlag = true
		urlFlag = true
	}
	var pkg, name string
	switch len(args) {
	case 1:
		if packageFlag {
			pkg = args[0]
		} else if regexpFlag {
			name = args[0]
		} else if strings.Contains(args[0], ".") {
			pkg, name = split(args[0])
		} else {
			name = args[0]
		}
	case 2:
		if packageFlag {
			cmd.Usage()
		}
		pkg, name = args[0], args[1]
	default:
		cmd.Usage()
		return os.ErrInvalid
	}
	if strings.Contains(pkg, "/") {
		fmt.Fprintf(os.Stderr, "doc: package name cannot contain slash (TODO)\n")
		os.Exit(2)
	}
	for _, path := range Paths(pkg) {
		lookInDirectory(path, name)
	}
	return nil
}
Ejemplo n.º 5
0
func runPresent(cmd *command.Command, args []string) error {
	if presentInput == "" || !isDoc(presentInput) {
		cmd.Usage()
		return os.ErrInvalid
	}

	if presentVerifyOnly {
		err := VerifyDoc(presentInput)
		if err != nil {
			fmt.Fprintf(os.Stderr, "present:%s", err)
			command.SetExitStatus(3)
			command.Exit()
		}
		return nil
	}
	w := os.Stdout
	if !presentStdout {
		if presentOutput == "" {
			presentOutput = presentInput + ".html"
		}
		ext := filepath.Ext(presentOutput)
		if ext != ".htm" && ext != ".html" {
			presentOutput += ".html"
		}
		var err error
		w, err = os.Create(presentOutput)
		if err != nil {
			fmt.Fprintf(os.Stderr, "present:%s", err)
			command.SetExitStatus(3)
			command.Exit()
		}
	}
	err := RenderDoc(w, presentInput)
	if err != nil {
		fmt.Fprintf(os.Stderr, "present:%s", err)
		command.SetExitStatus(3)
		command.Exit()
	}
	return nil
}
Ejemplo n.º 6
0
func runAstView(cmd *command.Command, args []string) error {
	if len(args) == 0 {
		cmd.Usage()
		return os.ErrInvalid
	}
	if astViewStdin {
		view, err := NewFilePackageSource(args[0], os.Stdin, true)
		if err != nil {
			fmt.Fprintf(os.Stderr, "astview: %s", err)
			command.SetExitStatus(3)
			command.Exit()
		}
		view.PrintTree(os.Stdout)
	} else {
		err := PrintFilesTree(args, os.Stdout, true)
		if err != nil {
			fmt.Fprintf(os.Stderr, "astview:%s", err)
			command.SetExitStatus(3)
			command.Exit()
		}
	}
	return nil
}
Ejemplo n.º 7
0
func runDocView(cmd *command.Command, args []string) error {
	if docViewFind == "" && docViewList == "" {
		cmd.Usage()
		return os.ErrInvalid
	}

	var template string
	var info *Info
	if len(docViewList) > 0 {
		pkgPath := filepath.Join(goroot, "src", docViewList)
		if docViewList == "pkg" {
			_, err := os.Stat(pkgPath)
			if err != nil {
				pkgPath = filepath.Join(goroot, "src")
			}
		}
		info = NewListInfo(pkgPath)
		if info != nil {
			if docViewList == "pkg" {
				var filterList []DirEntry
				for _, v := range info.Dirs.List {
					if v.Path == "cmd" {
						continue
					}
					if strings.HasPrefix(v.Path, "cmd/") {
						continue
					}
					if strings.Contains(v.Path, "/testdata") {
						continue
					}
					filterList = append(filterList, v)
				}
				info.Dirs.List = filterList
			} else if docViewList == "cmd" {
				var filterList []DirEntry
				for _, v := range info.Dirs.List {
					if strings.Contains(v.Path, "/") {
						continue
					}
					if strings.Contains(v.Path, "internal") {
						continue
					}
					filterList = append(filterList, v)
				}
				info.Dirs.List = filterList
			}
		}
		switch docViewMode {
		case "html":
			template = listHTML
		case "lite":
			template = listLite
		case "text":
			template = listText
		default:
			template = listText
		}
	} else if len(docViewFind) > 0 {
		dir := NewSourceDir(goroot)
		info = dir.FindInfo(docViewFind)
		switch docViewMode {
		case "html":
			template = findHTML
		case "lite":
			template = findLite
		case "text":
			template = findText
		default:
			template = findText
		}
	}
	if info == nil {
		fmt.Fprintf(os.Stderr, "<error>\n")
		command.SetExitStatus(3)
		command.Exit()
	}
	contents := info.GetPkgList(docViewMode, template)
	fmt.Fprintf(os.Stdout, "%s", contents)
	return nil
}
Ejemplo n.º 8
0
func runTypes(cmd *command.Command, args []string) error {
	if len(args) < 1 {
		cmd.Usage()
		return nil
	}
	if typesVerbose {
		now := time.Now()
		defer func() {
			log.Println("time", time.Now().Sub(now))
		}()
	}
	w := NewPkgWalker(&build.Default)

	var cursor *FileCursor
	if typesFilePos != "" {
		var cursorInfo FileCursor
		pos := strings.Index(typesFilePos, ":")
		if pos != -1 {
			cursorInfo.fileName = typesFilePos[:pos]
			if i, err := strconv.Atoi(typesFilePos[pos+1:]); err == nil {
				cursorInfo.cursorPos = i
			}
		}
		if typesFileStdin {
			src, err := ioutil.ReadAll(os.Stdin)
			if err == nil {
				cursorInfo.src = src
			}
		}
		cursor = &cursorInfo
	}

	for _, pkgName := range args {
		if pkgName == "." {
			pkgPath, err := os.Getwd()
			if err != nil {
				log.Fatalln(err)
			}
			pkgName = pkgPath
			w.CurrPackage = pkgName
		}

		conf := &PkgConfig{IgnoreFuncBodies: true, AllowBinary: true, WithTestFiles: true}
		if cursor != nil {
			conf.Cursor = cursor
			conf.IgnoreFuncBodies = false
			conf.Info = &types.Info{
				Uses:       make(map[*ast.Ident]types.Object),
				Defs:       make(map[*ast.Ident]types.Object),
				Selections: make(map[*ast.SelectorExpr]*types.Selection),
				//Types:      make(map[ast.Expr]types.TypeAndValue),
				//Scopes: make(map[ast.Node]*types.Scope),
				//Implicits : make(map[ast.Node]types.Object)
			}
			conf.XInfo = &types.Info{
				Uses:       make(map[*ast.Ident]types.Object),
				Defs:       make(map[*ast.Ident]types.Object),
				Selections: make(map[*ast.SelectorExpr]*types.Selection),
			}
		}

		pkg, err := w.Import("", pkgName, conf)

		if pkg == nil {
			log.Fatalln("error import path", err)
		}

		if cursor != nil && (typesFindInfo || typesFindDef || typesFindUse) {
			w.LookupCursor(pkg, conf, cursor)
		}
	}
	return nil
}