From 5f29933993cf04be458aef0137cc295b21dab78b Mon Sep 17 00:00:00 2001 From: michaeljguarino Date: Mon, 5 Feb 2024 15:27:40 -0500 Subject: [PATCH] Add ability to `plural up` in pre-init'ed repos (#486) Currently the command will trip because the console context entry has likely not been set up, all we need is the pull cred, so setting that explicitly in a backfill function here. --- cmd/plural/up.go | 4 +++ pkg/bundle/surveys.go | 4 +-- pkg/up/context.go | 72 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/cmd/plural/up.go b/cmd/plural/up.go index d25dcd51..ece81ba4 100644 --- a/cmd/plural/up.go +++ b/cmd/plural/up.go @@ -32,6 +32,10 @@ func (p *Plural) handleUp(c *cli.Context) error { return err } + if err := ctx.Backfill(); err != nil { + return err + } + if err := ctx.Generate(); err != nil { return err } diff --git a/pkg/bundle/surveys.go b/pkg/bundle/surveys.go index b8ba7f65..5c63604a 100644 --- a/pkg/bundle/surveys.go +++ b/pkg/bundle/surveys.go @@ -101,14 +101,14 @@ func fileSurvey(def string, item *api.ConfigurationItem) (prompt survey.Prompt, if err != nil { path = toComplete } - files, _ := filepath.Glob(cleanPath(path) + "*") + files, _ := filepath.Glob(CleanPath(path) + "*") return files }, } return } -func cleanPath(path string) string { +func CleanPath(path string) string { if strings.HasSuffix(path, "/") { return path } diff --git a/pkg/up/context.go b/pkg/up/context.go index 484b74cd..3b77ef12 100644 --- a/pkg/up/context.go +++ b/pkg/up/context.go @@ -3,9 +3,14 @@ package up import ( "path/filepath" + "github.com/AlecAivazis/survey/v2" + "github.com/pluralsh/plural-cli/pkg/bundle" "github.com/pluralsh/plural-cli/pkg/config" "github.com/pluralsh/plural-cli/pkg/manifest" "github.com/pluralsh/plural-cli/pkg/provider" + "github.com/pluralsh/plural-cli/pkg/utils" + + "github.com/mitchellh/go-homedir" ) type Context struct { @@ -14,6 +19,24 @@ type Context struct { Config *config.Config } +func (ctx *Context) Backfill() error { + context, err := manifest.FetchContext() + if err != nil { + return backfillConsoleContext(ctx.Manifest) + } + + console, ok := context.Configuration["console"] + if !ok { + return backfillConsoleContext(ctx.Manifest) + } + + if _, ok = console["private_key"]; !ok { + return backfillConsoleContext(ctx.Manifest) + } + + return nil +} + func Build() (*Context, error) { projPath, _ := filepath.Abs("workspace.yaml") project, err := manifest.ReadProject(projPath) @@ -33,3 +56,52 @@ func Build() (*Context, error) { Manifest: project, }, nil } + +func backfillConsoleContext(man *manifest.ProjectManifest) error { + path := manifest.ContextPath() + ctx, err := manifest.FetchContext() + if err != nil { + ctx = manifest.NewContext() + } + + console, ok := ctx.Configuration["console"] + if !ok { + console = map[string]interface{}{} + } + + utils.Highlight("It looks like you cloned this repo before running plural up, we just need you to generate and give us a deploy key to continue\n") + utils.Highlight("If you want, you can use `plural crypto ssh-keygen` to generate a keypair to use as a deploy key as well\n\n") + + var deployKey string + prompt := &survey.Input{ + Message: "Select a file containing a read-only deploy key for this repo (use tab to list files in the directory):", + Default: "~/.ssh", + Suggest: func(toComplete string) []string { + path, err := homedir.Expand(toComplete) + if err != nil { + path = toComplete + } + files, _ := filepath.Glob(bundle.CleanPath(path) + "*") + return files + }, + } + + opts := []survey.AskOpt{survey.WithValidator(survey.Required)} + if err := survey.AskOne(prompt, &deployKey, opts...); err != nil { + return err + } + + keyPath, err := homedir.Expand(deployKey) + if err != nil { + return err + } + + contents, err := utils.ReadFile(keyPath) + if err != nil { + return err + } + + console["private_key"] = contents + ctx.Configuration["console"] = console + return ctx.Write(path) +}