From 6d6cc66bb0ff496cd29b3ff518cb5151d0f56a51 Mon Sep 17 00:00:00 2001 From: Pavel Milkevich Date: Tue, 5 Mar 2024 14:14:53 +0100 Subject: [PATCH] project sandbox command should output current sandbox #524 --- internal/app/commands/common/common.go | 13 ++++++++- internal/app/commands/project/project.go | 8 +++++- internal/app/commands/project/sandbox.go | 23 ++++++++++++++-- internal/app/commands/sandbox/copy.go | 6 +++- internal/app/commands/sandbox/delete.go | 6 +++- internal/app/commands/sandbox/sandbox.go | 35 ++++++++++++++++-------- internal/app/commands/sandbox/start.go | 9 +++++- internal/app/commands/sandbox/upgrade.go | 6 +++- internal/app/util/util.go | 2 +- 9 files changed, 88 insertions(+), 20 deletions(-) diff --git a/internal/app/commands/common/common.go b/internal/app/commands/common/common.go index 269ea209..4394429e 100644 --- a/internal/app/commands/common/common.go +++ b/internal/app/commands/common/common.go @@ -119,14 +119,25 @@ func ReadProjectData(prjPath string) *ProjectData { return &data } +func ReadGradlePropertiesFile(path string) (*properties.Properties, error) { + return properties.LoadFile(filepath.Join(path, "gradle.properties"), properties.UTF8) +} + func ReadProjectDistroVersion(prjPath string) string { - if props, _ := properties.LoadFile(filepath.Join(prjPath, "gradle.properties"), properties.UTF8); props != nil { + if props, _ := ReadGradlePropertiesFile(prjPath); props != nil { return props.GetString("xpVersion", MIN_XP_VERSION) } else { return MIN_XP_VERSION } } +func ReadProjectName(prjPath string) string { + if props, _ := ReadGradlePropertiesFile(prjPath); props != nil { + return props.GetString("projectName", "") + } + return "" +} + func WriteProjectData(data *ProjectData, prjPath string) { file := util.OpenOrCreateDataFile(filepath.Join(prjPath, ".enonic"), false) defer file.Close() diff --git a/internal/app/commands/project/project.go b/internal/app/commands/project/project.go index 358b606a..7bb16c0b 100644 --- a/internal/app/commands/project/project.go +++ b/internal/app/commands/project/project.go @@ -75,7 +75,13 @@ func ensureProjectDataExists(c *cli.Context, prjPath, sandboxName, noBoxMessage // allow project without a sandbox in force mode return projectData } else if badSandbox || sandboxName != "" { - sBox, newBox = sandbox.EnsureSandboxExists(c, minDistroVersion, sandboxName, noBoxMessage, "A sandbox is required for your project, select one or create new", false, true) + sBox, newBox = sandbox.EnsureSandboxExists(c, sandbox.EnsureSandboxOptions{ + MinDistroVersion: minDistroVersion, + Name: sandboxName, + NoBoxMessage: noBoxMessage, + SelectBoxMessage: "A sandbox is required for your project, select one or create new", + ShowCreateOption: true, + }) if sBox == nil { return nil } diff --git a/internal/app/commands/project/sandbox.go b/internal/app/commands/project/sandbox.go index 7e60d946..866e6358 100644 --- a/internal/app/commands/project/sandbox.go +++ b/internal/app/commands/project/sandbox.go @@ -3,6 +3,7 @@ package project import ( "cli-enonic/internal/app/commands/common" "cli-enonic/internal/app/commands/sandbox" + "cli-enonic/internal/app/util" "fmt" "github.com/urfave/cli" "os" @@ -17,13 +18,31 @@ var Sandbox = cli.Command{ Action: func(c *cli.Context) error { ensureValidProjectFolder(".") + pData := common.ReadProjectData(".") var sandboxName string if c.NArg() > 0 { sandboxName = c.Args().First() + } else { + fmt.Fprint(os.Stdout, "\n") + projectName := common.ReadProjectName(".") + question := fmt.Sprintf("\"%s\" is using sandbox \"%s\". Change the project's sandbox", projectName, pData.Sandbox) + answer := util.PromptBool(question, false) + fmt.Fprint(os.Stdout, "\n") + if !answer { + return nil + } } - minDistroVersion := common.ReadProjectDistroVersion(".") - sandbox, _ := sandbox.EnsureSandboxExists(c, minDistroVersion, sandboxName, "No sandboxes found, do you want to create one", "Select sandbox to use as default for this project", true, true) + + sandbox, _ := sandbox.EnsureSandboxExists(c, sandbox.EnsureSandboxOptions{ + MinDistroVersion: common.ReadProjectDistroVersion("."), + Name: sandboxName, + NoBoxMessage: "No sandboxes found, do you want to create one", + SelectBoxMessage: "Select sandbox to use as default for this project", + ShowSuccessMessage: true, + ShowCreateOption: true, + ExcludeSandboxes: []string{pData.Sandbox}, + }) if sandbox == nil { os.Exit(1) } diff --git a/internal/app/commands/sandbox/copy.go b/internal/app/commands/sandbox/copy.go index 78203315..798f3faf 100644 --- a/internal/app/commands/sandbox/copy.go +++ b/internal/app/commands/sandbox/copy.go @@ -30,7 +30,11 @@ var Copy = cli.Command{ originalName = c.Args().First() targetName = c.Args().Get(1) } - sandbox, _ := EnsureSandboxExists(c, "", originalName, "", "Select source sandbox", true, false) + sandbox, _ := EnsureSandboxExists(c, EnsureSandboxOptions{ + Name: originalName, + SelectBoxMessage: "Select sandbox to copy", + ShowSuccessMessage: true, + }) if sandbox == nil { os.Exit(1) } diff --git a/internal/app/commands/sandbox/delete.go b/internal/app/commands/sandbox/delete.go index 3dbf3c67..1d36c528 100644 --- a/internal/app/commands/sandbox/delete.go +++ b/internal/app/commands/sandbox/delete.go @@ -20,7 +20,11 @@ var Delete = cli.Command{ if c.NArg() > 0 { sandboxName = c.Args().First() } - sandbox, _ := EnsureSandboxExists(c, "", sandboxName, "No sandboxes found, do you want to create one", "Select sandbox to delete", true, false) + sandbox, _ := EnsureSandboxExists(c, EnsureSandboxOptions{ + Name: sandboxName, + SelectBoxMessage: "Select sandbox to delete", + ShowSuccessMessage: true, + }) force := common.IsForceMode(c) if sandbox == nil || !acceptToDeleteSandbox(sandbox.Name, force) { os.Exit(1) diff --git a/internal/app/commands/sandbox/sandbox.go b/internal/app/commands/sandbox/sandbox.go index 713a5714..99333b4d 100644 --- a/internal/app/commands/sandbox/sandbox.go +++ b/internal/app/commands/sandbox/sandbox.go @@ -200,8 +200,18 @@ func AskToStopSandbox(rData common.RuntimeData, force bool) bool { } } -func EnsureSandboxExists(c *cli.Context, minDistroVersion, name string, noBoxMessage, selectBoxMessage string, showSuccessMessage, showCreateOption bool) (*Sandbox, bool) { - existingBoxes := listSandboxes(minDistroVersion) +type EnsureSandboxOptions struct { + MinDistroVersion string + Name string + NoBoxMessage string + SelectBoxMessage string + ShowSuccessMessage bool + ShowCreateOption bool + ExcludeSandboxes []string +} + +func EnsureSandboxExists(c *cli.Context, options EnsureSandboxOptions) (*Sandbox, bool) { + existingBoxes := listSandboxes(options.MinDistroVersion) force := common.IsForceMode(c) if len(existingBoxes) == 0 { @@ -209,22 +219,22 @@ func EnsureSandboxExists(c *cli.Context, minDistroVersion, name string, noBoxMes fmt.Fprintln(os.Stderr, "No sandboxes found. Create one using 'enonic sandbox create' first.") os.Exit(1) } - if showCreateOption == false || !util.PromptBool(noBoxMessage, true) { + if options.ShowCreateOption == false || !util.PromptBool(options.NoBoxMessage, true) { return nil, false } - newBox := SandboxCreateWizard(c, "", "", minDistroVersion, false, showSuccessMessage, force) + newBox := SandboxCreateWizard(c, "", "", options.MinDistroVersion, false, options.ShowSuccessMessage, force) return newBox, true } - if name != "" { - lowerName := strings.ToLower(name) + if options.Name != "" { + lowerName := strings.ToLower(options.Name) for _, existingBox := range existingBoxes { if strings.ToLower(existingBox.Name) == lowerName { return existingBox, false } } if force { - fmt.Fprintf(os.Stderr, "Sandbox with name '%s' can not be found\n", name) + fmt.Fprintf(os.Stderr, "Sandbox with name '%s' can not be found\n", options.Name) os.Exit(1) } } @@ -234,12 +244,15 @@ func EnsureSandboxExists(c *cli.Context, minDistroVersion, name string, noBoxMes } var selectOptions []string - if showCreateOption { + if options.ShowCreateOption { selectOptions = append(selectOptions, CREATE_NEW_BOX) } var boxName, defaultBox string osWithArch := util.GetCurrentOsWithArch() for i, box := range existingBoxes { + if util.IndexOf(box.Name, options.ExcludeSandboxes) >= 0 { + continue + } version := parseDistroVersion(box.Distro, false) boxName = formatSandboxListItemName(box.Name, version, osWithArch) if i == 0 { @@ -249,7 +262,7 @@ func EnsureSandboxExists(c *cli.Context, minDistroVersion, name string, noBoxMes } name, optionIndex, err := util.PromptSelect(&util.SelectOptions{ - Message: selectBoxMessage, + Message: options.SelectBoxMessage, Options: selectOptions, Default: defaultBox, PageSize: len(selectOptions), @@ -257,11 +270,11 @@ func EnsureSandboxExists(c *cli.Context, minDistroVersion, name string, noBoxMes util.Fatal(err, "Could not select sandbox: ") if name == CREATE_NEW_BOX { - newBox := SandboxCreateWizard(c, "", "", minDistroVersion, false, showSuccessMessage, force) + newBox := SandboxCreateWizard(c, "", "", options.MinDistroVersion, false, options.ShowSuccessMessage, force) return newBox, true } - if showCreateOption { + if options.ShowCreateOption { optionIndex -= 1 // subtract 1 because of 'new sandbox' option } return existingBoxes[optionIndex], false diff --git a/internal/app/commands/sandbox/start.go b/internal/app/commands/sandbox/start.go index f579e249..d4ae64d7 100644 --- a/internal/app/commands/sandbox/start.go +++ b/internal/app/commands/sandbox/start.go @@ -58,7 +58,14 @@ func ReadSandboxFromProjectOrAsk(c *cli.Context, useArguments bool) *Sandbox { if useArguments && c.NArg() > 0 { sandboxName = c.Args().First() } - sandbox, _ = EnsureSandboxExists(c, minDistroVersion, sandboxName, "No sandboxes found, create one", "Select sandbox to start", true, true) + sandbox, _ = EnsureSandboxExists(c, EnsureSandboxOptions{ + MinDistroVersion: minDistroVersion, + Name: sandboxName, + NoBoxMessage: "No sandboxes found, do you want to create one", + SelectBoxMessage: "Select sandbox to start", + ShowSuccessMessage: true, + ShowCreateOption: true, + }) if sandbox == nil { os.Exit(1) } diff --git a/internal/app/commands/sandbox/upgrade.go b/internal/app/commands/sandbox/upgrade.go index bab46a02..2d4d40b5 100644 --- a/internal/app/commands/sandbox/upgrade.go +++ b/internal/app/commands/sandbox/upgrade.go @@ -29,7 +29,11 @@ var Upgrade = cli.Command{ if c.NArg() > 0 { sandboxName = c.Args().First() } - sandbox, _ := EnsureSandboxExists(c, "", sandboxName, "No sandboxes found, do you want to create one", "Select sandbox", true, false) + sandbox, _ := EnsureSandboxExists(c, EnsureSandboxOptions{ + Name: sandboxName, + SelectBoxMessage: "Select sandbox to upgrade", + ShowSuccessMessage: true, + }) if sandbox == nil { os.Exit(1) } diff --git a/internal/app/util/util.go b/internal/app/util/util.go index 861e424c..86751986 100644 --- a/internal/app/util/util.go +++ b/internal/app/util/util.go @@ -504,7 +504,7 @@ func ReadOrCreateDir(path string) ([]os.DirEntry, error) { } func DecodeTomlFile(file *os.File, data interface{}) { - if _, err := toml.DecodeReader(bufio.NewReader(file), data); err != nil { + if _, err := toml.NewDecoder(bufio.NewReader(file)).Decode(data); err != nil { fmt.Fprintln(os.Stderr, "Could not parse toml file: ", err) os.Exit(1) }