mirror of https://github.com/pulumi/pulumi.git
54 lines
1.3 KiB
Go
54 lines
1.3 KiB
Go
// Copyright 2017, Pulumi Corporation. All rights reserved.
|
|
|
|
package engine
|
|
|
|
import (
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/pulumi/pulumi/pkg/pack"
|
|
)
|
|
|
|
type Pkginfo struct {
|
|
Pkg *pack.Package
|
|
Root string
|
|
}
|
|
|
|
// GetPwdMain returns the working directory and main entrypoint to use for this package.
|
|
func (pkginfo *Pkginfo) GetPwdMain() (string, string, error) {
|
|
pwd := pkginfo.Root
|
|
main := pkginfo.Pkg.Main
|
|
if main != "" {
|
|
// The path must be relative from the package root.
|
|
if filepath.IsAbs(main) {
|
|
return "", "", errors.New("project 'main' must be a relative path")
|
|
}
|
|
|
|
// Check that main is a subdirectory.
|
|
cleanPwd := filepath.Clean(pwd)
|
|
main = filepath.Clean(path.Join(cleanPwd, main))
|
|
if !strings.HasPrefix(main, cleanPwd) {
|
|
return "", "", errors.New("project 'main' must be a subfolder")
|
|
}
|
|
|
|
// So that any relative paths inside of the program are correct, we still need to pass the pwd
|
|
// of the main program's parent directory. How we do this depends on if the target is a dir or not.
|
|
maininfo, err := os.Stat(main)
|
|
if err != nil {
|
|
return "", "", errors.Wrapf(err, "project 'main' could not be read")
|
|
}
|
|
if maininfo.IsDir() {
|
|
pwd = main
|
|
main = ""
|
|
} else {
|
|
pwd = filepath.Dir(main)
|
|
main = filepath.Base(main)
|
|
}
|
|
}
|
|
return pwd, main, nil
|
|
}
|