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 (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2017-09-22 04:18:21 +02:00
|
|
|
"github.com/pulumi/pulumi/pkg/encoding"
|
|
|
|
"github.com/pulumi/pulumi/pkg/pack"
|
|
|
|
"github.com/pulumi/pulumi/pkg/workspace"
|
2017-08-23 01:56:15 +02:00
|
|
|
)
|
|
|
|
|
2017-08-30 03:24:12 +02:00
|
|
|
type pkginfo struct {
|
|
|
|
Pkg *pack.Package
|
|
|
|
Root string
|
2017-08-23 01:56:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// readPackageFromArg reads a package from an argument value. It can be "-" to request reading from Stdin, and is
|
|
|
|
// interpreted as a path otherwise. If an error occurs, it is printed to Stderr, and the returned value will be nil.
|
|
|
|
// In addition to the package, a root directory is returned that the compiler should be formed over, if any.
|
2017-08-30 03:24:12 +02:00
|
|
|
func (eng *Engine) readPackageFromArg(arg string) (*pkginfo, error) {
|
|
|
|
// If the arg is "-", read from stdin.
|
2017-08-23 01:56:15 +02:00
|
|
|
if arg == "-" {
|
2017-08-30 03:24:12 +02:00
|
|
|
return eng.readPackageFromStdin()
|
2017-08-23 01:56:15 +02:00
|
|
|
}
|
|
|
|
|
2017-08-30 03:24:12 +02:00
|
|
|
// If the path is empty, we need to detect it based on the current working directory.
|
|
|
|
var path string
|
|
|
|
if arg == "" {
|
|
|
|
pwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
path = pwd
|
|
|
|
// Now that we got here, we have a path, so we will try to load it.
|
2017-10-03 00:25:22 +02:00
|
|
|
pkgpath, err := workspace.DetectPackage(path)
|
2017-08-30 03:24:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Errorf("could not locate a package to load: %v", err)
|
|
|
|
} else if pkgpath == "" {
|
2017-08-31 19:45:15 +02:00
|
|
|
return nil, errors.Errorf("no package found by searching upwards from '%v'", path)
|
2017-08-30 03:24:12 +02:00
|
|
|
}
|
|
|
|
path = pkgpath
|
|
|
|
} else {
|
|
|
|
path = arg
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, go ahead and load the package directly from the path that we ended up with.
|
|
|
|
return eng.readPackage(path)
|
2017-08-23 01:56:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// readPackageFromStdin attempts to read a package from Stdin; if an error occurs, it will be printed to Stderr, and
|
|
|
|
// the returned value will be nil.
|
2017-08-30 03:24:12 +02:00
|
|
|
func (eng *Engine) readPackageFromStdin() (*pkginfo, error) {
|
2017-08-23 01:56:15 +02:00
|
|
|
// If stdin, read the package from text, and then create a compiler using the working directory.
|
|
|
|
b, err := ioutil.ReadAll(os.Stdin)
|
|
|
|
if err != nil {
|
2017-08-30 03:24:12 +02:00
|
|
|
return nil, errors.Wrapf(err, "could not read package from stdin")
|
2017-08-23 01:56:15 +02:00
|
|
|
}
|
|
|
|
|
2017-08-30 03:24:12 +02:00
|
|
|
m := encoding.Marshalers[".json"]
|
2017-10-17 20:27:54 +02:00
|
|
|
var pkg pack.Package
|
|
|
|
err = m.Unmarshal(b, &pkg)
|
2017-08-30 03:24:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "a problem occurred when unmarshaling stdin into a package")
|
|
|
|
}
|
2017-08-31 19:21:17 +02:00
|
|
|
if err = pkg.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-08-30 03:24:12 +02:00
|
|
|
return &pkginfo{
|
2017-10-17 20:27:54 +02:00
|
|
|
Pkg: &pkg,
|
2017-08-30 03:24:12 +02:00
|
|
|
Root: "",
|
|
|
|
}, nil
|
2017-08-23 01:56:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// readPackage attempts to read a package from the given path; if an error occurs, it will be printed to Stderr, and
|
|
|
|
// the returned value will be nil. If the path is a directory, nil is returned.
|
2017-08-30 03:24:12 +02:00
|
|
|
func (eng *Engine) readPackage(path string) (*pkginfo, error) {
|
|
|
|
// If the path refers to a directory, bail early.
|
2017-08-23 01:56:15 +02:00
|
|
|
info, err := os.Stat(path)
|
|
|
|
if err != nil {
|
2017-08-30 03:24:12 +02:00
|
|
|
return nil, errors.Wrapf(err, "could not read path '%v'", path)
|
|
|
|
} else if info.IsDir() {
|
|
|
|
return nil, errors.Wrapf(err, "path '%v' is a directory and not a path to package file", path)
|
2017-08-23 01:56:15 +02:00
|
|
|
}
|
|
|
|
|
2017-10-17 20:27:54 +02:00
|
|
|
pkg, err := pack.Load(path)
|
2017-08-23 01:56:15 +02:00
|
|
|
if err != nil {
|
2017-08-31 19:21:17 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-08-30 03:24:12 +02:00
|
|
|
return &pkginfo{
|
|
|
|
Pkg: pkg,
|
|
|
|
Root: filepath.Dir(path),
|
|
|
|
}, nil
|
2017-08-23 01:56:15 +02:00
|
|
|
}
|