github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/utils/testing/files.go (about)

     1  /*
     2  Copyright 2017 Mirantis
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package testing
    18  
    19  import (
    20  	"fmt"
    21  	"io/ioutil"
    22  	"os"
    23  	"os/exec"
    24  	"path/filepath"
    25  	"strings"
    26  )
    27  
    28  // DirToMap converts directory to a map where keys are filenames
    29  // without full path and values are the contents of the files. Files
    30  // with '.iso' extensions are unpacked using 7z and then converted to
    31  // a map using DirToMap. Directories are handled recursively. If the
    32  // directory doesn't exist, dirToMap returns nil
    33  func DirToMap(dir string) (map[string]interface{}, error) {
    34  	if fi, err := os.Stat(dir); err != nil && os.IsNotExist(err) {
    35  		return nil, nil
    36  	} else if err != nil {
    37  		return nil, fmt.Errorf("os.Stat(): %v", err)
    38  	} else if !fi.IsDir() {
    39  		return nil, fmt.Errorf("%q expected to be a directory", dir)
    40  	}
    41  	paths, err := filepath.Glob(filepath.Join(dir, "*"))
    42  	if err != nil {
    43  		return nil, fmt.Errorf("filepath.Glob(): %v", err)
    44  	}
    45  	m := map[string]interface{}{}
    46  	for _, p := range paths {
    47  		filename := filepath.Base(p)
    48  		fi, err := os.Stat(p)
    49  		switch {
    50  		case err != nil:
    51  			err = fmt.Errorf("os.Stat(): %v", err)
    52  		case fi.IsDir():
    53  			m[filename], err = DirToMap(p)
    54  		case strings.HasSuffix(filename, ".iso"):
    55  			m[filename], err = IsoToMap(p)
    56  		default:
    57  			var bs []byte
    58  			if bs, err = ioutil.ReadFile(p); err != nil {
    59  				err = fmt.Errorf("ioutil.ReadFile(): %v", err)
    60  			} else {
    61  				m[filename] = string(bs)
    62  			}
    63  		}
    64  		if err != nil {
    65  			return nil, err
    66  		}
    67  	}
    68  	return m, nil
    69  }
    70  
    71  // IsoToMap converts an iso image to a map where keys are filenames
    72  // without full path and values are the contents of the files. It does
    73  // so by unpacking the image into a temporary directory and processing
    74  // it with DirToMap.
    75  func IsoToMap(isoPath string) (map[string]interface{}, error) {
    76  	tmpDir, err := ioutil.TempDir("", "iso-out")
    77  	if err != nil {
    78  		return nil, fmt.Errorf("ioutil.TempDir(): %v", err)
    79  	}
    80  	defer os.RemoveAll(tmpDir)
    81  	out, err := exec.Command("7z", "x", "-o"+tmpDir, isoPath).CombinedOutput()
    82  	if err != nil {
    83  		outStr := ""
    84  		if len(out) != 0 {
    85  			outStr = ". Output:\n" + string(out)
    86  		}
    87  		return nil, fmt.Errorf("error unpacking iso: %v%s", err, outStr)
    88  	}
    89  	return DirToMap(tmpDir)
    90  }