github.com/symfony-cli/symfony-cli@v0.0.0-20240514161054-ece2df437dfa/main.go (about)

     1  /*
     2   * Copyright (c) 2021-present Fabien Potencier <fabien@symfony.com>
     3   *
     4   * This file is part of Symfony CLI project
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU Affero General Public License as
     8   * published by the Free Software Foundation, either version 3 of the
     9   * License, or (at your option) any later version.
    10   *
    11   * This program is distributed in the hope that it will be useful,
    12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    14   * GNU Affero General Public License for more details.
    15   *
    16   * You should have received a copy of the GNU Affero General Public License
    17   * along with this program. If not, see <http://www.gnu.org/licenses/>.
    18   */
    19  
    20  package main
    21  
    22  //go:generate go run ./local/platformsh/generator/...
    23  
    24  import (
    25  	"fmt"
    26  	"io"
    27  	"os"
    28  	"time"
    29  
    30  	"github.com/rs/zerolog"
    31  	"github.com/symfony-cli/console"
    32  	"github.com/symfony-cli/symfony-cli/commands"
    33  	"github.com/symfony-cli/symfony-cli/local/php"
    34  	"github.com/symfony-cli/symfony-cli/local/platformsh"
    35  	"github.com/symfony-cli/terminal"
    36  )
    37  
    38  var (
    39  	// version is overridden at linking time
    40  	version = "dev"
    41  	// channel is overridden at linking time
    42  	channel = "dev"
    43  	// overridden at linking time
    44  	buildDate string
    45  )
    46  
    47  func getCliExtraEnv() []string {
    48  	return []string{
    49  		"SYMFONY_CLI_VERSION=" + version,
    50  		"SYMFONY_CLI_BINARY_NAME=" + console.CurrentBinaryName(),
    51  	}
    52  }
    53  
    54  func main() {
    55  	args := os.Args
    56  	name := console.CurrentBinaryName()
    57  	// called via "php"?
    58  	if php.IsBinaryName(name) {
    59  		fmt.Printf(`Using the Symfony wrappers to call PHP is not possible anymore; remove the wrappers and use "symfony %s" instead.`, name)
    60  		fmt.Println()
    61  		os.Exit(1)
    62  	}
    63  	// called via "symfony php"?
    64  	if len(args) >= 2 && php.IsBinaryName(args[1]) {
    65  		e := &php.Executor{
    66  			BinName:  args[1],
    67  			Args:     args[1:],
    68  			ExtraEnv: getCliExtraEnv(),
    69  		}
    70  		os.Exit(e.Execute(true))
    71  	}
    72  	// called via "symfony console"?
    73  	if len(args) >= 2 && args[1] == "console" {
    74  		args[1] = "bin/console"
    75  		if _, err := os.Stat("app/console"); err == nil {
    76  			args[1] = "app/console"
    77  		}
    78  		e := &php.Executor{
    79  			BinName:  "php",
    80  			Args:     args,
    81  			ExtraEnv: getCliExtraEnv(),
    82  		}
    83  		os.Exit(e.Execute(false))
    84  	}
    85  	// called via "symfony composer"?
    86  	if len(args) >= 2 && args[1] == "composer" {
    87  		res := php.Composer("", args[2:], getCliExtraEnv(), os.Stdout, os.Stderr, io.Discard, zerolog.Nop())
    88  		terminal.Eprintln(res.Error())
    89  		os.Exit(res.ExitCode())
    90  	}
    91  
    92  	for _, env := range []string{"BRANCH", "ENV", "APPLICATION_NAME"} {
    93  		if os.Getenv("SYMFONY_"+env) != "" {
    94  			continue
    95  		}
    96  
    97  		if v := os.Getenv("PLATFORM_" + env); v != "" {
    98  			os.Setenv("SYMFONY_"+env, v)
    99  			continue
   100  		}
   101  	}
   102  
   103  	cmds := commands.CommonCommands()
   104  	psh, err := platformsh.Get()
   105  	if err != nil {
   106  		fmt.Fprintln(os.Stderr, err.Error())
   107  		os.Exit(1)
   108  	}
   109  	cmds = append(cmds, psh.Commands...)
   110  	console.HelpPrinter = psh.WrapHelpPrinter()
   111  	app := &console.Application{
   112  		Name:          "Symfony CLI",
   113  		Usage:         "Symfony CLI helps developers manage projects, from local code to remote infrastructure",
   114  		Copyright:     fmt.Sprintf("(c) 2021-%d Fabien Potencier", time.Now().Year()),
   115  		FlagEnvPrefix: []string{"SYMFONY", "PLATFORM"},
   116  		Commands:      cmds,
   117  		Action: func(ctx *console.Context) error {
   118  			if ctx.Args().Len() == 0 {
   119  				return commands.WelcomeAction(ctx)
   120  			}
   121  			return console.ShowAppHelpAction(ctx)
   122  		},
   123  		Before:    commands.InitAppFunc,
   124  		Version:   version,
   125  		Channel:   channel,
   126  		BuildDate: buildDate,
   127  	}
   128  	app.Run(args)
   129  }