// Copyright 2017, Pulumi Corporation. All rights reserved. package engine import ( "os" "path" "path/filepath" "strings" "github.com/pkg/errors" "github.com/pulumi/pulumi/pkg/workspace" ) type Projinfo struct { Proj *workspace.Project Root string } // GetPwdMain returns the working directory and main entrypoint to use for this package. func (projinfo *Projinfo) GetPwdMain() (string, string, error) { pwd := projinfo.Root main := projinfo.Proj.Main if main == "" { main = "." } else { // 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) } } // Now turn both pwd and main into absolute directories. var err error pwd, err = filepath.Abs(pwd) if err != nil { return "", "", err } main, err = filepath.Abs(main) if err != nil { return "", "", err } return pwd, main, nil }