Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Commit

Permalink
Support exclude patterns, keep conf format, retain old metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
karalabe committed Oct 27, 2016
1 parent 2634e3b commit 3834051
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 8 deletions.
35 changes: 28 additions & 7 deletions conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import (
type Conf struct {
Package string `yaml:"package,omitempty"`
Imports []Import `yaml:"import,omitempty"`
Excludes []string `yaml:"exclude,omitempty"`
importMap map[string]Import
confFile string
yamlType bool
}

type Import struct {
Expand All @@ -32,7 +34,8 @@ func Parse(path string) (*Conf, error) {
defer file.Close()

trashConf := &Conf{confFile: path}
if err := yaml.NewDecoder(file).Decode(trashConf); err == nil {
if yaml.NewDecoder(file).Decode(trashConf) == nil {
trashConf.yamlType = true
trashConf.Dedupe()
return trashConf, nil
}
Expand Down Expand Up @@ -60,6 +63,12 @@ func Parse(path string) (*Conf, error) {
continue
}

// If we have a `-` suffix, it's an exclude pattern
if fields[0][0] == '-' {
trashConf.Excludes = append(trashConf.Excludes, strings.TrimSpace(fields[0][1:]))
continue
}
// Otherwise it's an import pattern
packageImport := Import{}
packageImport.Package = fields[0] // at least 1 field at this point: trimmed the line and skipped empty
if len(fields) > 2 {
Expand Down Expand Up @@ -104,23 +113,35 @@ func (t *Conf) Get(pkg string) (Import, bool) {

func (t *Conf) Dump(path string) error {
file, err := os.Create(path)
defer file.Close()
if err != nil {
return err
}
defer file.Close()

// If a previous version was in yaml format, preserve it
if t.yamlType {
return yaml.NewEncoder(file).Encode(t)
}
// Otherwise create a flat config file
w := bufio.NewWriter(file)
defer w.Flush()

fmt.Fprintln(w, "# package")
fmt.Fprintln(w, t.Package)
fmt.Fprintln(w)

for _, i := range t.Imports {
s := fmt.Sprintf("%s\t%s\t%s", i.Package, i.Version, i.Repo)
fmt.Fprintln(w, strings.TrimSpace(s))
if len(t.Imports) > 0 {
fmt.Fprintln(w, "\n# import")
for _, i := range t.Imports {
s := fmt.Sprintf("%s\t%s\t%s", i.Package, i.Version, i.Repo)
fmt.Fprintln(w, strings.TrimSpace(s))
}
}
if len(t.Excludes) > 0 {
fmt.Fprintln(w, "\n# exclude")
for _, pkg := range t.Excludes {
fmt.Fprintln(w, "-"+strings.TrimSpace(pkg))
}
}

return nil
}

Expand Down
38 changes: 37 additions & 1 deletion trash.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func updateTrash(trashDir, dir, trashFile string, trashConf *conf.Conf) error {
imports = collectImports(rootPackage, libRoot)
}

trashConf = &conf.Conf{Package: rootPackage}
trashConf.Imports = nil // Retains all previous fields, including old importMap used by Get below
for pkg := range imports {
if pkg == rootPackage || strings.HasPrefix(pkg, rootPackage+"/") {
continue
Expand Down Expand Up @@ -662,6 +662,39 @@ func removeUnusedImports(imports util.Packages) error {
})
}

func removeExcludes(excludes []string) error {
exclude := make(map[string]bool)
for _, dir := range excludes {
exclude[dir] = true
}
return filepath.Walk("vendor", func(path string, info os.FileInfo, err error) error {
logrus.Debugf("removeExcludes, path: '%s', err: '%v'", path, err)
if os.IsNotExist(err) {
return filepath.SkipDir
}
if err != nil {
return err
}
if path == "vendor" {
return nil
}
pkg := path[len("vendor/"):]
if exclude[pkg] {
logrus.Infof("Removing excluded dir: '%s'", path)
err := os.RemoveAll(path)
if err == nil {
return filepath.SkipDir
}
if os.IsNotExist(err) {
return filepath.SkipDir
}
logrus.Errorf("Error removing excluded dir, path: '%s', err: '%v'", path, err)
return err
}
return nil
})
}

func removeEmptyDirs() error {
for count := 1; count != 0; {
count = 0
Expand Down Expand Up @@ -726,6 +759,9 @@ func cleanup(dir string, trashConf *conf.Conf) error {
if err := removeUnusedImports(imports); err != nil {
logrus.Errorf("Error removing unused dirs: %v", err)
}
if err := removeExcludes(trashConf.Excludes); err != nil {
logrus.Errorf("Error removing excluded dirs: %v", err)
}
if err := removeEmptyDirs(); err != nil {
logrus.Errorf("Error removing empty dirs: %v", err)
}
Expand Down

0 comments on commit 3834051

Please sign in to comment.