示例#1
0
文件: config.go 项目: nelhage/pw
func LoadConfig() (*Config, error) {
	if err := config.LoadConfig(flag.CommandLine, "pw"); err != nil {
		return nil, err
	}
	theConfig.RootDir = expandTilde(theConfig.RootDir)
	return &theConfig, nil
}
示例#2
0
文件: main.go 项目: hobinjk/livegrep
func main() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: %s [flags] REGEX\n", os.Args[0])
		flag.PrintDefaults()
	}
	if err := config.LoadConfig(flag.CommandLine, "lgrc"); err != nil {
		fmt.Fprintf(os.Stderr, "Loading config: %s\n", err)
		os.Exit(1)
	}
	flag.Parse()

	if len(flag.Args()) != 1 {
		flag.Usage()
		os.Exit(1)
	}

	var uri *url.URL
	var err error

	if strings.Contains(*server, ":") {
		if uri, err = url.Parse(*server); err != nil {
			fmt.Fprintf(os.Stderr, "Parsing server %s: %s\n", *server, err.Error())
			os.Exit(1)
		}
	} else {
		uri = &url.URL{Scheme: "http", Host: *server}
	}

	uri.Path = "/api/v1/search/"
	uri.RawQuery = url.Values{"line": []string{flag.Arg(0)}}.Encode()

	resp, err := http.Get(uri.String())

	if err != nil {
		fmt.Fprintf(os.Stderr, "Requesting %s: %s\n", uri.String(), err.Error())
		os.Exit(1)
	}

	if resp.StatusCode != 200 {
		var reply api.ReplyError
		if e := json.NewDecoder(resp.Body).Decode(&reply); e != nil {
			fmt.Fprintf(os.Stderr,
				"Error reading reply (status=%d): %s\n", resp.StatusCode, e.Error())
		} else {
			fmt.Fprintf(os.Stderr, "Error: %s: %s", reply.Err.Code, reply.Err.Message)
		}
		os.Exit(1)
	}

	var reply api.ReplySearch

	if e := json.NewDecoder(resp.Body).Decode(&reply); e != nil {
		fmt.Fprintf(os.Stderr,
			"Error reading reply (status=%d): %s\n", resp.StatusCode, e.Error())
		os.Exit(1)
	}

	for _, r := range reply.Results {
		ctx := r.Contexts[0]
		p := ctx.Paths[0]
		if p.Repo != "" {
			fmt.Printf("%s:", p.Repo)
		}
		fmt.Printf("%s:%s:%d: ", p.Ref, p.Path, ctx.LineNumber)
		fmt.Printf("%s\n", r.Line)
	}
}