2017-08-30 03:24:12 +02:00
|
|
|
// Copyright 2017, Pulumi Corporation. All rights reserved.
|
|
|
|
|
2017-08-23 01:56:15 +02:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2017-11-30 17:14:47 +01:00
|
|
|
"path"
|
2017-08-23 01:56:15 +02:00
|
|
|
"path/filepath"
|
2017-11-30 17:14:47 +01:00
|
|
|
"strings"
|
2017-08-23 01:56:15 +02:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2017-11-16 16:49:07 +01:00
|
|
|
|
2017-09-22 04:18:21 +02:00
|
|
|
"github.com/pulumi/pulumi/pkg/pack"
|
2017-08-23 01:56:15 +02:00
|
|
|
)
|
|
|
|
|
2017-11-16 16:49:07 +01:00
|
|
|
type Pkginfo struct {
|
2017-08-30 03:24:12 +02:00
|
|
|
Pkg *pack.Package
|
|
|
|
Root string
|
2017-08-23 01:56:15 +02:00
|
|
|
}
|
|
|
|
|
2017-11-16 16:49:07 +01:00
|
|
|
// 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 != "" {
|
2017-11-30 17:14:47 +01:00
|
|
|
// 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")
|
2017-11-16 16:49:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|