Exemplo n.º 1
0
func TestCleanString(t *testing.T) {
	clean := util.CleanString("  spaces \t\n ")
	if clean != "spaces" {
		t.Error("Expected to receive string 'spaces'")
	}
	clean = util.CleanString("  ' embedded spaces 1 '   ")
	if clean != " embedded spaces 1 " {
		t.Error("Expected to receive string ' embedded spaces 1 '")
	}
	clean = util.CleanString("  \" embedded spaces 2 \"   ")
	if clean != " embedded spaces 2 " {
		t.Error("Expected to receive string ' embedded spaces '")
	}
}
Exemplo n.º 2
0
// Expand the tilde in the data_path setting to an absolute path.
// Returns the expanded path, or an error.
func expandedDataDir(configFile string) (string, error) {
	file, err := os.Open(configFile)
	if err != nil {
		return "", fmt.Errorf("Cannot open config file: %v\n", err)
	}
	defer file.Close()
	bufReader := bufio.NewReader(file)
	for {
		line, err := bufReader.ReadString('\n')
		if err == io.EOF {
			break
		} else if err != nil {
			return "", err
		}
		cleanLine := strings.TrimSpace(line)
		if strings.HasPrefix(cleanLine, "data_path") {
			parts := strings.SplitN(cleanLine, "=", 2)
			if len(parts) < 2 {
				return "", fmt.Errorf("Config file setting for data_path is missing or malformed.")
			}
			expanded, err := fileutil.ExpandTilde(util.CleanString(parts[1]))
			if err != nil {
				return "", fmt.Errorf("Cannot expand data_dir setting '%s': %v", parts[1], err)
			}
			if !fileutil.FileExists(expanded) {
				fmt.Printf("Creating NSQ data directory %s \n", expanded)
				os.MkdirAll(expanded, 0755)
			}
			return expanded, nil
		}
	}
	return "", nil
}