Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

project sandbox command should output current sandbox #524 #527

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
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
13 changes: 12 additions & 1 deletion internal/app/commands/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 7 additions & 1 deletion internal/app/commands/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
23 changes: 21 additions & 2 deletions internal/app/commands/project/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
Expand Down
6 changes: 5 additions & 1 deletion internal/app/commands/sandbox/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 5 additions & 1 deletion internal/app/commands/sandbox/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 24 additions & 11 deletions internal/app/commands/sandbox/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,31 +200,41 @@ 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 {
if force {
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)
}
}
Expand All @@ -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 {
Expand All @@ -249,19 +262,19 @@ 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),
})
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
Expand Down
9 changes: 8 additions & 1 deletion internal/app/commands/sandbox/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 5 additions & 1 deletion internal/app/commands/sandbox/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/app/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Loading