// 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 }
func TestExpandTilde(t *testing.T) { expanded, err := fileutil.ExpandTilde("~/tmp") if err != nil { t.Error(err) } // Testing this cross-platform is pain. Different home dirs // on Windows, Linux, Mac. Different separators ("/" vs "\"). if len(expanded) <= 5 || !strings.HasSuffix(expanded, "tmp") { t.Errorf("~/tmp expanded to unexpected value %s", expanded) } expanded, err = fileutil.ExpandTilde("/nothing/to/expand") if err != nil { t.Error(err) } if expanded != "/nothing/to/expand" { t.Errorf("/nothing/to/expand expanded to unexpected value %s", expanded) } }
// Expands ~ file paths and bag validation config file relative // paths to absolute paths. func (config *Config) ExpandFilePaths() { expanded, err := fileutil.ExpandTilde(config.TarDirectory) if err == nil { config.TarDirectory = expanded } expanded, err = fileutil.ExpandTilde(config.LogDirectory) if err == nil { config.LogDirectory = expanded } expanded, err = fileutil.ExpandTilde(config.RestoreDirectory) if err == nil { config.RestoreDirectory = expanded } expanded, err = fileutil.ExpandTilde(config.ReplicationDirectory) if err == nil { config.ReplicationDirectory = expanded } expanded, err = fileutil.ExpandTilde(config.DPN.StagingDirectory) if err == nil { config.DPN.StagingDirectory = expanded } expanded, err = fileutil.ExpandTilde(config.DPN.RemoteNodeHomeDirectory) if err == nil { config.DPN.RemoteNodeHomeDirectory = expanded } expanded, err = fileutil.ExpandTilde(config.DPN.LogDirectory) if err == nil { config.DPN.LogDirectory = expanded } // Convert bag validation config files from relative to absolute paths. absPath, _ := filepath.Abs(config.BagValidationConfigFile) if absPath != config.BagValidationConfigFile { expanded, err = fileutil.RelativeToAbsPath(config.BagValidationConfigFile) if err == nil { config.BagValidationConfigFile = expanded } } absPath, _ = filepath.Abs(config.DPN.BagValidationConfigFile) if absPath != config.DPN.BagValidationConfigFile { expanded, err = fileutil.RelativeToAbsPath(config.DPN.BagValidationConfigFile) if err == nil { config.DPN.BagValidationConfigFile = expanded } } }
// Returns two Stats objects: the expected stats, from our test data dir, // and the actual stats, from the JSON file that the bucket reader dumped // out last time it ran func getBucketReaderOutputs(t *testing.T) (expected *stats.APTBucketReaderStats, actual *stats.APTBucketReaderStats) { configFile := filepath.Join("config", "integration.json") appConfig, err := models.LoadConfigFile(configFile) require.Nil(t, err) // This JSON file is part of our code repo. // It contains the output we expect from a success test run. pathToExpectedJson, err := fileutil.RelativeToAbsPath(filepath.Join("testdata", "integration_results", "bucket_reader_stats.json")) require.Nil(t, err) expected, err = stats.APTBucketReaderStatsLoadFromFile(pathToExpectedJson) require.Nil(t, err) // This JSON file is recreated every time we run apt_bucket_reader // as part of the integration tests. It contains the output of the // actual test run. pathToActualJson, err := fileutil.ExpandTilde(filepath.Join(appConfig.LogDirectory, "bucket_reader_stats.json")) require.Nil(t, err) actual, err = stats.APTBucketReaderStatsLoadFromFile(pathToActualJson) require.Nil(t, err) return expected, actual }