Skip to content

Commit

Permalink
Add ability to plural up in pre-init'ed repos (#486)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
michaeljguarino authored Feb 5, 2024
1 parent 8d53ef3 commit 5f29933
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
4 changes: 4 additions & 0 deletions cmd/plural/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/bundle/surveys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
72 changes: 72 additions & 0 deletions pkg/up/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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)
}

0 comments on commit 5f29933

Please sign in to comment.