pulumi/pkg/util/fsutil/walkup.go
Matt Ellis 818246a708 Allow control of uploaded archive root in Pulumi.yaml
Previously, when uploading a projectm to the service, we would only
upload the folder rooted by the Pulumi.yaml for that project. This
worked well, but it meant that customers needed to structure their
code in a way such that Pulumi.yaml was always as the root of their
project, and if they wanted to share common files between two projects
there was no good solution for doing this.

This change introduces an optional piece of metadata, named context,
that can be added to Pulumi.yaml, which allows controlling the root
folder used for computing the root folder to archive from.  When it is
set, it is combined with the location of the Pulumi.yaml file for the
project we are uploading and that folder is uses as the root of what
we upload to the service.

Fixes: #574
2018-01-31 16:22:58 -08:00

64 lines
1.5 KiB
Go

// Copyright 2016-2017, Pulumi Corporation. All rights reserved.
package fsutil
import (
"io/ioutil"
"os"
"path/filepath"
)
// WalkUp walks each file in path, passing the full path to `walkFn`. If walkFn returns true,
// this method returns the path that was passed to walkFn. Before visiting the parent directory,
// visitParentFn is called, if that returns false, WalkUp stops its search
func WalkUp(path string, walkFn func(string) bool, visitParentFn func(string) bool) (string, error) {
if visitParentFn == nil {
visitParentFn = func(dir string) bool { return true }
}
curr := pathDir(path)
for {
// visit each file
files, err := ioutil.ReadDir(curr)
if err != nil {
return "", err
}
for _, file := range files {
name := file.Name()
path := filepath.Join(curr, name)
if walkFn(path) {
return path, nil
}
}
// If we are at the root, stop walking
if isTop(curr) {
break
}
if !visitParentFn(curr) {
break
}
// visit the parent
curr = filepath.Dir(curr)
}
return "", nil
}
// pathDir returns the nearest directory to the given path (identity if a directory; parent otherwise).
func pathDir(path string) string {
// If the path is a file, we want the directory it is in
info, err := os.Stat(path)
if err != nil || info.IsDir() {
return path
}
return filepath.Dir(path)
}
// isTop returns true if the path represents the top of the filesystem.
func isTop(path string) bool {
return os.IsPathSeparator(path[len(path)-1])
}