github.com/kngu9/glide@v0.0.0-20160505135211-e73500c73591/path/strip.go (about)

     1  package path
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  
     7  	"github.com/Masterminds/glide/godep/strip"
     8  	"github.com/Masterminds/glide/msg"
     9  )
    10  
    11  // StripVcs removes VCS metadata (.git, .hg, .bzr, .svn) from the vendor/
    12  // directory.
    13  func StripVcs() error {
    14  	if _, err := os.Stat(VendorDir); err != nil {
    15  		if os.IsNotExist(err) {
    16  			msg.Debug("Vendor directory does not exist.")
    17  		}
    18  
    19  		return err
    20  	}
    21  	return filepath.Walk(VendorDir, stripHandler)
    22  }
    23  
    24  func stripHandler(path string, info os.FileInfo, err error) error {
    25  
    26  	name := info.Name()
    27  	if name == ".git" || name == ".bzr" || name == ".svn" || name == ".hg" {
    28  		if _, err := os.Stat(path); err == nil {
    29  			if info.IsDir() {
    30  				msg.Info("Removing: %s", path)
    31  				return os.RemoveAll(path)
    32  			}
    33  
    34  			msg.Debug("%s is not a directory. Skipping removal", path)
    35  			return nil
    36  		}
    37  	}
    38  	return nil
    39  }
    40  
    41  // StripVendor removes nested vendor and Godeps/_workspace/ directories.
    42  func StripVendor() error {
    43  	if _, err := os.Stat(VendorDir); err != nil {
    44  		if os.IsNotExist(err) {
    45  			msg.Debug("Vendor directory does not exist.")
    46  		}
    47  
    48  		return err
    49  	}
    50  
    51  	err := filepath.Walk(VendorDir, stripVendorHandler)
    52  	if err != nil {
    53  		return err
    54  	}
    55  
    56  	err = strip.GodepWorkspace(VendorDir)
    57  
    58  	return err
    59  }
    60  
    61  func stripVendorHandler(path string, info os.FileInfo, err error) error {
    62  	// Skip the base vendor directory
    63  	if path == VendorDir {
    64  		return nil
    65  	}
    66  
    67  	name := info.Name()
    68  	if name == "vendor" {
    69  		if _, err := os.Stat(path); err == nil {
    70  			if info.IsDir() {
    71  				msg.Info("Removing: %s", path)
    72  				return os.RemoveAll(path)
    73  			}
    74  
    75  			msg.Debug("%s is not a directory. Skipping removal", path)
    76  			return nil
    77  		}
    78  	}
    79  	return nil
    80  }