github.com/argoproj/argo-cd/v3@v3.2.1/cmd/main.go (about)

     1  package main
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  	"os/exec"
     7  	"path/filepath"
     8  
     9  	"github.com/spf13/cobra"
    10  	"k8s.io/klog/v2"
    11  
    12  	appcontroller "github.com/argoproj/argo-cd/v3/cmd/argocd-application-controller/commands"
    13  	applicationset "github.com/argoproj/argo-cd/v3/cmd/argocd-applicationset-controller/commands"
    14  	cmpserver "github.com/argoproj/argo-cd/v3/cmd/argocd-cmp-server/commands"
    15  	commitserver "github.com/argoproj/argo-cd/v3/cmd/argocd-commit-server/commands"
    16  	dex "github.com/argoproj/argo-cd/v3/cmd/argocd-dex/commands"
    17  	gitaskpass "github.com/argoproj/argo-cd/v3/cmd/argocd-git-ask-pass/commands"
    18  	k8sauth "github.com/argoproj/argo-cd/v3/cmd/argocd-k8s-auth/commands"
    19  	notification "github.com/argoproj/argo-cd/v3/cmd/argocd-notification/commands"
    20  	reposerver "github.com/argoproj/argo-cd/v3/cmd/argocd-repo-server/commands"
    21  	apiserver "github.com/argoproj/argo-cd/v3/cmd/argocd-server/commands"
    22  	cli "github.com/argoproj/argo-cd/v3/cmd/argocd/commands"
    23  	"github.com/argoproj/argo-cd/v3/util/log"
    24  )
    25  
    26  const (
    27  	binaryNameEnv = "ARGOCD_BINARY_NAME"
    28  )
    29  
    30  func init() {
    31  	// Make sure klog uses the configured log level and format.
    32  	klog.SetLogger(log.NewLogrusLogger(log.NewWithCurrentConfig()))
    33  }
    34  
    35  func main() {
    36  	var command *cobra.Command
    37  
    38  	binaryName := filepath.Base(os.Args[0])
    39  	if val := os.Getenv(binaryNameEnv); val != "" {
    40  		binaryName = val
    41  	}
    42  
    43  	isArgocdCLI := false
    44  
    45  	switch binaryName {
    46  	case "argocd", "argocd-linux-amd64", "argocd-darwin-amd64", "argocd-windows-amd64.exe":
    47  		command = cli.NewCommand()
    48  		isArgocdCLI = true
    49  	case "argocd-server":
    50  		command = apiserver.NewCommand()
    51  	case "argocd-application-controller":
    52  		command = appcontroller.NewCommand()
    53  	case "argocd-repo-server":
    54  		command = reposerver.NewCommand()
    55  	case "argocd-cmp-server":
    56  		command = cmpserver.NewCommand()
    57  		isArgocdCLI = true
    58  	case "argocd-commit-server":
    59  		command = commitserver.NewCommand()
    60  	case "argocd-dex":
    61  		command = dex.NewCommand()
    62  	case "argocd-notifications":
    63  		command = notification.NewCommand()
    64  	case "argocd-git-ask-pass":
    65  		command = gitaskpass.NewCommand()
    66  		isArgocdCLI = true
    67  	case "argocd-applicationset-controller":
    68  		command = applicationset.NewCommand()
    69  	case "argocd-k8s-auth":
    70  		command = k8sauth.NewCommand()
    71  		isArgocdCLI = true
    72  	default:
    73  		command = cli.NewCommand()
    74  		isArgocdCLI = true
    75  	}
    76  
    77  	if isArgocdCLI {
    78  		// silence errors and usages since we'll be printing them manually.
    79  		// This is because if we execute a plugin, the initial
    80  		// errors and usage are always going to get printed that we don't want.
    81  		command.SilenceErrors = true
    82  		command.SilenceUsage = true
    83  	}
    84  
    85  	err := command.Execute()
    86  	// if the err is non-nil, try to look for various scenarios
    87  	// such as if the error is from the execution of a normal argocd command,
    88  	// unknown command error or any other.
    89  	if err != nil {
    90  		pluginHandler := cli.NewDefaultPluginHandler([]string{"argocd"})
    91  		pluginErr := pluginHandler.HandleCommandExecutionError(err, isArgocdCLI, os.Args)
    92  		if pluginErr != nil {
    93  			var exitErr *exec.ExitError
    94  			if errors.As(pluginErr, &exitErr) {
    95  				// Return the actual plugin exit code
    96  				os.Exit(exitErr.ExitCode())
    97  			}
    98  			// Fallback to exit code 1 if the error isn't an exec.ExitError
    99  			os.Exit(1)
   100  		}
   101  	}
   102  }