Ejemplo n.º 1
0
func cliSetup() *cliOptions {

	options := newCliOptions()

	var httpVerb = goopt.StringWithLabel([]string{"-X", "--command"}, options.httpVerb,
		"COMMAND", fmt.Sprintf("HTTP verb for request: %s", HttpVerbs))
	var httpHeaders = goopt.Strings([]string{"-H", "--header"},
		"KEY:VALUE", "Custom HTTP Headers to be sent with request (can pass multiple times)")
	var postData = goopt.StringWithLabel([]string{"-d", "--data"}, options.postData,
		"DATA", "HTTP Data for POST")
	var timeOut = goopt.IntWithLabel([]string{"-t", "--timeout"}, options.timeout,
		"TIMEOUT", "Timeout in seconds for request")
	var shouldRedirect = goopt.Flag([]string{"-r", "--redirect"}, []string{}, "Follow redirects", "")
	var isVerbose = goopt.Flag([]string{"-v", "--verbose"}, []string{}, "Verbose output", "")
	var hasColor = goopt.Flag([]string{"-c", "--color"}, []string{}, "Colored output", "")
	var isInsecureSSL = goopt.Flag([]string{"-k", "--insecure"}, []string{}, "Allow insecure https connections", "")

	goopt.Summary = "Golang based http client program"
	goopt.Parse(nil)

	options.httpVerb = *httpVerb
	options.httpHeaders = *httpHeaders
	options.postData = *postData
	options.timeout = *timeOut
	options.verbose = *isVerbose
	options.redirect = *shouldRedirect
	options.color = *hasColor
	options.sslInsecure = *isInsecureSSL
	options.arguments = goopt.Args

	exitWithMessageIfNonZero(validateOptions(options))
	exitWithMessageIfNonZero(validateArguments(options))

	return options
}
Ejemplo n.º 2
0
func main() {
	goopt.Summary = "Command line tool to merge minimaps from custom clients in Hafen."
	var sessionFodler = goopt.StringWithLabel([]string{"-d", "--sessions-dir"}, "sessions", "<path>",
		"Specify input folder (instead of default \"sessions\")")
	var mode = goopt.Alternatives([]string{"-m", "--mode"}, []string{"merger", "zoomer", "picture"},
		"Specify mode (instead of default \"merger\")")
	var zoomPath = goopt.StringWithLabel([]string{"-z", "--zoom"}, "", "<session>",
		"Create zoom layers for specific <session> and place them into \"zoommap\" folder")
	var zoomSize = goopt.IntWithLabel([]string{"--zoom-tile-size"}, 100, "<size>",
		"Specify generated tiles size (instead of default 100)")
	var zoomMax = goopt.IntWithLabel([]string{"--zoom-max"}, 5, "<num>",
		"Specify zoom max (instead of default 5)")
	var zoomMin = goopt.IntWithLabel([]string{"--zoom-min"}, 1, "<num>",
		"Specify zoom min (instead of default 1)")
	var picturePath = goopt.StringWithLabel([]string{"-p", "--picture"}, "", "<session>",
		"Create single map picture for specific <session>")
	var outputFodler = goopt.StringWithLabel([]string{"-o", "--output-dir"}, "zoommap", "<path>",
		"Specify output folder for zoom mode (instead of default \"zoommap\")")
	var trimSessions = goopt.IntWithLabel([]string{"-t", "--trim"}, -1, "<count>",
		"Remove sessions with tiles < <count> from result (good for removing cave sessions)")
	var removeNonStandard = goopt.Flag([]string{"-c", "--clean-non-standard"}, []string{},
		"Remove all non-standard maps (size != 100x100)", "")
	var hashCode = goopt.Alternatives([]string{"--hash-method"}, []string{"simple", "border"},
		"Specify hash method (instead of default \"simple\")")

	// Parse CMD
	goopt.Parse(nil)

	// Force change mode for backward compatibility
	if *picturePath != "" && *mode == "merger" {
		*mode = "picture"
	}
	if *zoomPath != "" && *mode == "merger" {
		*mode = "zoomer"
	}

	SESSION_FOLDER = *sessionFodler

	var hashMethod HashMethod
	switch *hashCode {
	case "simple":
		hashMethod = HashMethod{CodeName: *hashCode, Func: generateSimpleHash}
		break
	case "border":
		hashMethod = HashMethod{CodeName: *hashCode, Func: generateBorderHash}
		break
	default:
		panic("Unrecognized hash method!") // this should never happen!
	}

	workingDirectory, _ := filepath.Abs(SESSION_FOLDER)

	if *zoomSize%100 != 0 {
		fmt.Println("Tile size must be in multiples of 100")
		return
	}
	var composeCount = int(*zoomSize / 100)

	// Generate zoom levels for specific session
	if *mode == "zoomer" {
		generateTiles(workingDirectory, *zoomPath, *outputFodler, composeCount, hashMethod, *zoomMin, *zoomMax)
		return
	}

	// Generate single picture for specific session
	if *mode == "picture" {
		generatePicture(workingDirectory, *picturePath)
		return
	}

	// Otherwise, let's make cross-merge
	files, _ := ioutil.ReadDir(workingDirectory)
	if len(files) < 2 {
		fmt.Println("No folders found")
		return
	}

	if *removeNonStandard == true {
		// Remove all sessions with tile size != 100x100
		for j := 0; j < len(files); j++ {
			tiles, _ := ioutil.ReadDir(filepath.Join(workingDirectory, files[j].Name()))
			for i := 0; i < len(tiles); i++ {
				if strings.Contains(tiles[i].Name(), "tile_") {
					sx, sy := getImageDimension(filepath.Join(workingDirectory, files[j].Name(), tiles[i].Name()))
					if sx != 100 || sy != 100 {
						fmt.Printf("Old session removed: %s\n", files[j].Name())
						os.RemoveAll(filepath.Join(workingDirectory, files[j].Name()))
					}
					break
				}
			}
		}
	}

	files, _ = ioutil.ReadDir(workingDirectory)
	if len(files) < 2 {
		fmt.Println("No folders found")
		return
	}
	for j := 0; j < len(files); j++ {
		info, err := os.Stat(filepath.Join(workingDirectory, files[j].Name()))
		if err != nil {
			continue
		}
		if info.IsDir() == false {
			continue
		}

		coreFolder := files[j]
		for i := 1; i < len(files); i++ {
			if i == j {
				continue
			}
			dirInfo, err := os.Stat(filepath.Join(workingDirectory, files[i].Name()))
			if err != nil {
				continue
			}
			if dirInfo.IsDir() == false {
				continue
			}

			res := mergeFolders(filepath.Join(workingDirectory, files[i].Name()), filepath.Join(workingDirectory, coreFolder.Name()), hashMethod)
			if res == true {
				fmt.Printf("Merged (%s, %s)\n", coreFolder.Name(), files[i].Name())
			} else {
				fmt.Printf("Sessions are not mergeable (%s, %s)\n", coreFolder.Name(), files[i].Name())
			}
		}
	}
	files, _ = ioutil.ReadDir(workingDirectory)
	var sessionsJS string = "var sessionsJS = ["
	for j := 0; j < len(files); j++ {
		tiles, _ := ioutil.ReadDir(filepath.Join(workingDirectory, files[j].Name()))
		if *trimSessions > 0 {
			if len(tiles) < *trimSessions {
				err := os.RemoveAll(filepath.Join(workingDirectory, files[j].Name()))
				if err != nil {
					fmt.Printf("Cannot trim session %s: %s\n", files[j].Name(), err.Error())
					continue
				} else {
					fmt.Printf("Trimmed session %s\n", files[j].Name())
					continue
				}
			}
		}
		sessionsJS += "\"" + SESSION_FOLDER + "/" + files[j].Name() + "\", "
	}
	sessionsJS += "];"
	ioutil.WriteFile("session.js", []byte(sessionsJS), 0777)
}