pulumi/cmd/init.go
Matt Ellis 3f1197ef84 Move .pulumi to root of a repository
Now, instead of having a .pulumi folder next to each project, we have
a single .pulumi folder in the root of the repository. This is created
by running `pulumi init`.

When run in a git repository, `pulumi init` will place the .pulumi
file next to the .git folder, so it can be shared across all projects
in a repository. When not in a git repository, it will be created in
the current working directory.

We also start tracking information about the repository itself, in a
new `repo.json` file stored in the root of the .pulumi folder. The
information we track are "owner" and "name" which map to information
we use on pulumi.com.

When run in a git repository with a remote named origin pointing to a
GitHub project, we compute the owner and name by deconstructing
information from the remote's URL. Otherwise, we just use the current
user's username and the name of the current working directory as the
owner and name, respectively.
2017-10-27 11:46:21 -07:00

72 lines
1.6 KiB
Go

// Copyright 2016-2017, Pulumi Corporation. All rights reserved.
package cmd
import (
"fmt"
"os"
"github.com/pulumi/pulumi/pkg/util/cmdutil"
"github.com/pulumi/pulumi/pkg/workspace"
"github.com/spf13/cobra"
)
func newInitCmd() *cobra.Command {
var owner string
var name string
cmd := &cobra.Command{
Use: "init",
Short: "Initialize a new Pulumi repository",
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
cwd, err := os.Getwd()
if err != nil {
return err
}
repo, err := workspace.GetRepository(cwd)
if err != nil && err != workspace.ErrNoRepository {
return err
}
if err == workspace.ErrNoRepository {
// No existing repository, so we'll need to create one
repo = workspace.NewRepository(cwd)
detectedOwner, detectedName, detectErr := detectOwnerAndName(cwd)
if detectErr != nil {
return detectErr
}
repo.Owner = detectedOwner
repo.Name = detectedName
}
// explicit command line arguments should overwrite any existing values
if owner != "" {
repo.Owner = owner
}
if name != "" {
repo.Name = name
}
err = repo.Save()
if err != nil {
return err
}
fmt.Printf("Initialized Pulumi repository in %s\n", repo.Root)
return nil
}),
}
cmd.PersistentFlags().StringVar(
&owner, "owner", "",
"Override the repository owner; default is taken from current Git repository or username")
cmd.PersistentFlags().StringVar(
&name, "name", "",
"Override the repository name; default is taken from current Git repository or current working directory")
return cmd
}