Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 48 additions & 8 deletions cmd/registry/cmd/config/configurations/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,25 @@ package configurations

import (
"fmt"
"os/exec"
"strings"

"github.com/apigee/registry/pkg/config"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

func createCommand() *cobra.Command {
var gcloud bool

cmd := &cobra.Command{
Use: "create CONFIGURATION",
Short: "Creates and activates a new named configuration",
Long: "Creates and activates a new named configuration. Values will be populated from active " +
"configuration (if any) and any passed property flags.",
Example: "registry config configurations create localhost --registry.address='locahost:8080'",
Args: cobra.ExactArgs(1),
"configuration (if any) and any passed property flags (unless using --gcloud).",
Example: "registry config configurations create localhost --registry.address='locahost:8080'\n" +
"registry config configurations create hosted --gcloud",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
name := args[0]
if err := config.ValidateName(name); err != nil {
Expand All @@ -39,11 +45,33 @@ func createCommand() *cobra.Command {
return fmt.Errorf("cannot create config %q, it already exists", name)
}

// attempt to clone the active config or at least the current flags
s, err := config.Active()
if err != nil {
if s, err = config.ReadValid(""); err != nil {
s = config.Configuration{}
s := config.Configuration{}
var err error
if gcloud {
cmd.Flags().Visit(func(f *pflag.Flag) {
if strings.HasPrefix(f.Name, "registry.") || f.Name == "address" {
err = fmt.Errorf("--gcloud is mutually exclusive from --address and --registry.* flags")
}
})
if err != nil {
return err
}

project, err := activeGCloudProject()
if err != nil {
return fmt.Errorf("cannot execute `gcloud`: %v", err)
}
s.Registry.Project = project
s.Registry.Address = "apigeeregistry.googleapis.com:443"
s.Registry.Insecure = false
s.TokenSource = "gcloud auth print-access-token"
} else {
// attempt to clone the active config or at least the current flags
s, err = config.Active()
if err != nil {
if s, err = config.ReadValid(""); err != nil {
s = config.Configuration{}
}
}
}

Expand All @@ -61,5 +89,17 @@ func createCommand() *cobra.Command {
return nil
},
}

cmd.Flags().BoolVar(&gcloud, "gcloud", false, "if specified, uses values from active gcloud config")
return cmd
}

func activeGCloudProject() (string, error) {
cmdArgs := strings.Split("gcloud config get project", " ")
execCmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
out, err := execCmd.Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(out)), nil
}