コード例 #1
0
ファイル: example_test.go プロジェクト: NHOrus/prompt
func ExampleBasic() (string, error) {
	email, err := prompt.Basic("Email", true)
	if err != nil {
		return "", err
	}

	_, err = mail.ParseAddress(email)
	return email, err
}
コード例 #2
0
ファイル: main.go プロジェクト: rqlite/rqlite
func main() {
	cli.SetUsageStyle(cli.ManualStyle)
	cli.Run(new(argT), func(ctx *cli.Context) error {
		argv := ctx.Argv().(*argT)
		if argv.Help {
			ctx.WriteUsage()
			return nil
		}

		prefix := fmt.Sprintf("%s:%d> ", argv.Host, argv.Port)
	FOR_READ:
		for {
			line, err := prompt.Basic(prefix, false)
			if err != nil {
				return err
			}
			line = strings.TrimSpace(line)
			if line == "" {
				continue
			}
			var (
				index = strings.Index(line, " ")
				cmd   = line
			)
			if index >= 0 {
				cmd = line[:index]
			}
			cmd = strings.ToUpper(cmd)
			switch cmd {
			case "QUIT", "EXIT":
				break FOR_READ
			case "SELECT":
				err = query(ctx, cmd, line, argv)
			default:
				err = execute(ctx, cmd, line, argv)
			}
			if err != nil {
				ctx.String("%s %v\n", ctx.Color().Red("ERR!"), err)
			}
		}
		ctx.String("bye~\n")
		return nil
	})
}
コード例 #3
0
ファイル: speedtest.go プロジェクト: wwek/ncgo
func Run() {
	fmt.Printf("开始测速...\n")
	fmt.Printf("获取客户端信息和服务器列表开始...\n")
	cc, err := GetClientInfo()
	if err != nil {
		fmt.Printf("错误: %v\n", err)
		return
	}
	//fmt.Printf("%s %s", cc, err)
	srvs, err := GetServerLists()
	if err != nil {
		fmt.Printf("错误: %v\n", err)
		return
	}
	fmt.Printf("获取客户端信息和服务器列表完成...\n")
	cfg := Config{
		LicenseKey: cc.License,
		IP:         net.ParseIP(cc.ClientConfig.Ip),
		Lat:        cc.ClientConfig.Lat,
		Long:       cc.ClientConfig.Long,
		ISP:        cc.ClientConfig.ISP,
	}
	ignoreIDs := make(map[uint]bool, 1)
	strIDs := strings.Split(cc.ServerConfig.IgnoreIDs, ",")
	for i := range strIDs {
		x, err := strconv.ParseUint(strIDs[i], 10, 32)
		if err != nil {
			continue
		}
		ignoreIDs[uint(x)] = false
	}
	if err := populateServers(&cfg, srvs.Servers, ignoreIDs); err != nil {
		fmt.Printf("错误: %v\n", err)
		return
	}

	if len(cfg.Servers) <= 0 {
		fmt.Printf("没找到合适的测速服务器!\n")
		return
	}
	var headers []string
	var data [][]string
	var testServers []Testserver
	//	if *search == "" {
	fmt.Printf("获取距离最近的服务器...\n")
	if testServers, err = autoGetTestServers(&cfg); err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", err)
		os.Exit(-1)
	}
	fmt.Printf("%d Closest responding servers:\n", len(testServers))
	for i := range testServers {
		data = append(data, []string{fmt.Sprintf("%d", i),
			testServers[i].Name, testServers[i].Sponsor,
			fmt.Sprintf("%.02f", testServers[i].Distance),
			fmt.Sprintf("%s", testServers[i].Latency)})
	}
	headers = []string{"ID", "Name", "Sponsor", "Distance (km)", "Latency (ms)"}
	//	} else {
	//		if testServers, err = getSearchServers(cfg, *search); err != nil {
	//			fmt.Fprintf(os.Stderr, "%s\n", err)
	//			os.Exit(-1)
	//		}
	//		headers = []string{"ID", "Name", "Sponsor", "Distance (km)"}
	//		fmt.Printf("%d Matching servers:\n", len(testServers))
	//		for i := range testServers {
	//			data = append(data, []string{fmt.Sprintf("%d", i),
	//				testServers[i].Name, testServers[i].Sponsor,
	//				fmt.Sprintf("%.02f", testServers[i].Distance)})
	//		}

	//	}
	t := gotabulate.Create(data)
	t.SetHeaders(headers)
	t.SetWrapStrings(false)
	fmt.Printf("%s", t.Render(tableFormat))
	fmt.Printf("Enter server ID for bandwidth test, or \"quit\" to exit\n")
	for {
		s, err := prompt.Basic("ID> ", true)
		if err != nil {
			fmt.Printf("input failure \"%v\"\n", err)
			os.Exit(-1)
		}
		//be REALLY forgiving on exit logic
		if strings.HasPrefix(strings.ToLower(s), "exit") {
			os.Exit(0)
		}
		if strings.HasPrefix(strings.ToLower(s), "quit") {
			os.Exit(0)
		}

		//try to convert the string to a number
		id, err := strconv.ParseUint(s, 10, 64)
		if err != nil {
			fmt.Fprintf(os.Stderr, "\"%s\" is not a valid id\n", s)
			continue
		}
		if id > uint64(len(testServers)) {
			fmt.Fprintf(os.Stderr, "No server with ID \"%d\" available\n", id)
			continue
		}
		if err = fullTest(testServers[id]); err != nil {
			if err == io.EOF {
				fmt.Fprintf(os.Stderr, "Error, the remote server kicked us.\n")
				fmt.Fprintf(os.Stderr, "Maximum request size may have changed\n")
			} else {
				fmt.Fprintf(os.Stderr, "Test failed with unknown 错误: %v\n", err)
			}
			os.Exit(-1)
		} else {
			break //we are done
		}
	}
}
コード例 #4
0
ファイル: main.go プロジェクト: elico/speedtest
func main() {
	cfg, err := stdn.GetConfig()
	if err != nil {
		fmt.Printf("ERROR: %v\n", err)
		return
	}
	if len(cfg.Servers) <= 0 {
		fmt.Printf("No acceptable servers found\n")
		return
	}
	var headers []string
	var data [][]string
	var testServers []stdn.Testserver
	if *search == "" {
		fmt.Printf("Gathering server list and testing...\n")
		if testServers, err = autoGetTestServers(cfg); err != nil {
			fmt.Fprintf(os.Stderr, "%s\n", err)
			os.Exit(-1)
		}
		fmt.Printf("%d Closest responding servers:\n", len(testServers))
		for i := range testServers {
			data = append(data, []string{fmt.Sprintf("%d", i),
				testServers[i].Name, testServers[i].Sponsor,
				fmt.Sprintf("%.02f", testServers[i].Distance),
				fmt.Sprintf("%s", testServers[i].Latency)})
		}
		headers = []string{"ID", "Name", "Sponsor", "Distance (km)", "Latency (ms)"}
	} else {
		if testServers, err = getSearchServers(cfg, *search); err != nil {
			fmt.Fprintf(os.Stderr, "%s\n", err)
			os.Exit(-1)
		}
		headers = []string{"ID", "Name", "Sponsor", "Distance (km)"}
		fmt.Printf("%d Matching servers:\n", len(testServers))
		for i := range testServers {
			data = append(data, []string{fmt.Sprintf("%d", i),
				testServers[i].Name, testServers[i].Sponsor,
				fmt.Sprintf("%.02f", testServers[i].Distance)})
		}

	}
	t := gotabulate.Create(data)
	t.SetHeaders(headers)
	t.SetWrapStrings(false)
	fmt.Printf("%s", t.Render(tableFormat))
	fmt.Printf("Enter server ID for bandwidth test, or \"quit\" to exit\n")
	for {
		s, err := prompt.Basic("ID> ", true)
		if err != nil {
			fmt.Printf("input failure \"%v\"\n", err)
			os.Exit(-1)
		}
		//be REALLY forgiving on exit logic
		if strings.HasPrefix(strings.ToLower(s), "exit") {
			os.Exit(0)
		}
		if strings.HasPrefix(strings.ToLower(s), "quit") {
			os.Exit(0)
		}

		//try to convert the string to a number
		id, err := strconv.ParseUint(s, 10, 64)
		if err != nil {
			fmt.Fprintf(os.Stderr, "\"%s\" is not a valid id\n", s)
			continue
		}
		if id > uint64(len(testServers)) {
			fmt.Fprintf(os.Stderr, "No server with ID \"%d\" available\n", id)
			continue
		}
		if err = fullTest(testServers[id]); err != nil {
			if err == io.EOF {
				fmt.Fprintf(os.Stderr, "Error, the remote server kicked us.\n")
				fmt.Fprintf(os.Stderr, "Maximum request size may have changed\n")
			} else {
				fmt.Fprintf(os.Stderr, "Test failed with unknown error: %v\n", err)
			}
			os.Exit(-1)
		} else {
			break //we are done
		}
	}
	//Pausing to allow the windows to stay open
	_, err = prompt.Basic("Enter \"quit\" to exit> ", true)
	if err != nil {
		fmt.Printf("input failure \"%v\"\n", err)
		os.Exit(-1)
	}

}