Skip to content

Commit

Permalink
Add cloud:env:debug (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Jan 29, 2022
1 parent 476fa67 commit 642a999
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 6 deletions.
111 changes: 111 additions & 0 deletions commands/cloud_env_debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package commands

import (
"fmt"
"strings"

"github.com/pkg/errors"
"github.com/symfony-cli/console"
"github.com/symfony-cli/terminal"
)

var cloudEnvDebugCmd = &console.Command{
Category: "cloud:environment",
Name: "debug",
Aliases: []*console.Alias{{Name: "environment:debug"}},
Usage: "Debug an environment by switching Symfony to the debug mode temporarily",
Flags: []console.Flag{
projectFlag,
environmentFlag,
&console.BoolFlag{Name: "off", Usage: "Disable debug mode"},
&console.BoolFlag{Name: "debug", Usage: "Display commands output"},
},
Action: func(c *console.Context) error {
spinner := terminal.NewSpinner(terminal.Stderr)
spinner.Start()
defer spinner.Stop()

projectID := c.String("project")
if projectID == "" {
out, ok := psh.RunInteractive(terminal.Logger, "", []string{"project:info", "id", "-y"}, c.Bool("debug"), nil)
if !ok {
return errors.New("Unable to detect the project")
}
projectID = strings.TrimSpace(out.String())
}

out, ok := psh.RunInteractive(terminal.Logger, "", []string{"project:info", "default_branch", "--project=" + projectID, "-y"}, c.Bool("debug"), nil)
if !ok {
return errors.New("Unable to detect the default branch name")
}
defaultEnvName := strings.TrimSpace(out.String())
envName := c.String("environment")
if envName == "" {
if out, ok := psh.RunInteractive(terminal.Logger, "", []string{"env:info", "id", "--project=" + projectID, "-y"}, false, nil); ok {
envName = strings.TrimSpace(out.String())
} else {
envName = defaultEnvName
}
}

ui := terminal.SymfonyStyle(terminal.Stdout, terminal.Stdin)

defaultArgs := []string{"--level=env", "--project=" + projectID, "-y"}
if c.String("environment") != "" {
defaultArgs = append(defaultArgs, c.String("environment"))
}

if c.Bool("off") {
terminal.Println("Deleting APP_ENV and APP_DEBUG (can take some time, --debug to tail commands)")
if out, ok := psh.RunInteractive(terminal.Logger, "", append(defaultArgs, "var:delete", "env:APP_ENV"), c.Bool("debug"), nil); !ok {
if !strings.Contains(out.String(), "Variable not found") {
return errors.New("An error occurred while removing APP_ENV")
}
}
if out, ok := psh.RunInteractive(terminal.Logger, "", append(defaultArgs, "var:delete", "env:APP_DEBUG"), c.Bool("debug"), nil); !ok {
if !strings.Contains(out.String(), "Variable not found") {
return errors.New("An error occurred while removing APP_DEBUG")
}
}
ui.Success(fmt.Sprintf("The \"%s\" environment has been switched back to production mode.", envName))
return nil
}

out, ok = psh.RunInteractive(terminal.Logger, "", []string{"project:info", "default_domain", "--project=" + projectID, "-y"}, c.Bool("debug"), nil)
if !ok {
return errors.New("Unable to detect the default domain")
}
defaultDomain := strings.TrimSpace(out.String())
if defaultDomain != "" {
if defaultEnvName == envName {
return errors.Errorf("You cannot use the cloud:environment:debug command on the production environment (%s branch) of a production project", defaultEnvName)
}
}

terminal.Println("Setting APP_ENV and APP_DEBUG to dev/debug (can take some time, --debug to tail commands)")
if out, ok := psh.RunInteractive(terminal.Logger, "", append(defaultArgs, "var:create", "--name=env:APP_ENV", "--value=dev"), c.Bool("debug"), nil); !ok {
if !strings.Contains(out.String(), "already exists on the environment") {
return errors.New("An error occurred while adding APP_ENV")
}
if out, ok := psh.RunInteractive(terminal.Logger, "", append(defaultArgs, "var:update", "--value=dev", "env:APP_ENV"), c.Bool("debug"), nil); !ok {
if !strings.Contains(out.String(), "No changes were provided") {
return errors.New("An error occurred while adding APP_ENV")
}
}
}
if out, ok := psh.RunInteractive(terminal.Logger, "", append(defaultArgs, "var:create", "--name=env:APP_DEBUG", "--value=1"), c.Bool("debug"), nil); !ok {
if !strings.Contains(out.String(), "already exists on the environment") {
return errors.New("An error occurred while adding APP_DEBUG")
}
if out, ok := psh.RunInteractive(terminal.Logger, "", append(defaultArgs, "var:update", "--value=1", "env:APP_DEBUG"), c.Bool("debug"), nil); !ok {
if !strings.Contains(out.String(), "No changes were provided") {
return errors.New("An error occurred while adding APP_DEBUG")
}
}
}

spinner.Stop()
ui.Success(fmt.Sprintf("The \"%s\" environment is now in debug mode\n Switch back via %s env:debug --off", envName, c.App.HelpName))
return nil
},
}
1 change: 0 additions & 1 deletion commands/local_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,6 @@ func runComposer(c *console.Context, dir string, args []string, debug bool) erro

if err := php.Composer(dir, args, env, out, err, os.Stderr, terminal.Logger); err.ExitCode() != 0 {
terminal.Println(buf.String())
terminal.Logger.Debug().Msg(buf.String())
return err
}
return nil
Expand Down
27 changes: 27 additions & 0 deletions commands/platformsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
package commands

import (
"bytes"
_ "embed"
"io"
"os"
"path/filepath"
"strings"

"github.com/mitchellh/go-homedir"
"github.com/rs/zerolog"
"github.com/symfony-cli/console"
"github.com/symfony-cli/symfony-cli/local/php"
"github.com/symfony-cli/symfony-cli/local/platformsh"
Expand Down Expand Up @@ -121,3 +124,27 @@ func (p *platformshCLI) executor(args []string) *php.Executor {
e.Paths = append([]string{filepath.Dir(p.path)}, e.Paths...)
return e
}

func (p *platformshCLI) RunInteractive(logger zerolog.Logger, projectDir string, args []string, debug bool, stdin io.Reader) (bytes.Buffer, bool) {
var buf bytes.Buffer

e := p.executor(args)
if projectDir != "" {
e.Dir = projectDir
}
if debug {
e.Stdout = io.MultiWriter(&buf, os.Stdout)
e.Stderr = io.MultiWriter(&buf, os.Stderr)
} else {
e.Stdout = &buf
e.Stderr = &buf
}
if stdin != nil {
e.Stdin = stdin
}
logger.Debug().Str("cmd", strings.Join(e.Args, " ")).Msg("Executing Platform.sh CLI command interactively")
if ret := e.Execute(false); ret != 0 {
return buf, false
}
return buf, true
}
1 change: 0 additions & 1 deletion commands/project_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"github.com/symfony-cli/terminal"
)

// initCmd represents the init command
var projectInitCmd = &console.Command{
Category: "project",
Name: "init",
Expand Down
8 changes: 4 additions & 4 deletions commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ var (
psh *platformshCLI
pshOnce sync.Once

dirFlag = &console.StringFlag{
Name: "dir",
Usage: "Project directory",
}
dirFlag = &console.StringFlag{Name: "dir", Usage: "Project directory"}
projectFlag = &console.StringFlag{Name: "project", Aliases: []string{"p"}, Usage: "The project ID or URL"}
environmentFlag = &console.StringFlag{Name: "environment", Aliases: []string{"e"}, Usage: "The environment ID"}
)

func CommonCommands() []*console.Command {
Expand All @@ -60,6 +59,7 @@ func CommonCommands() []*console.Command {
phpWrapper,
bookCheckReqsCmd,
bookCheckoutCmd,
cloudEnvDebugCmd,
localNewCmd,
localPhpListCmd,
localPhpRefreshCmd,
Expand Down

0 comments on commit 642a999

Please sign in to comment.