github.com/jfrog/jfrog-cli-core/v2@v2.51.0/plugins/pluginmain.go (about)

     1  package plugins
     2  
     3  import (
     4  	"os"
     5  
     6  	jfrogclicore "github.com/jfrog/jfrog-cli-core/v2"
     7  	"github.com/jfrog/jfrog-cli-core/v2/plugins/components"
     8  	"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
     9  	"github.com/jfrog/jfrog-cli-core/v2/utils/log"
    10  	"github.com/jfrog/jfrog-client-go/utils"
    11  	"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
    12  	clientLog "github.com/jfrog/jfrog-client-go/utils/log"
    13  	"github.com/urfave/cli"
    14  )
    15  
    16  const commandHelpTemplate = `{{.HelpName}}{{if .UsageText}}
    17  Arguments:
    18  {{.UsageText}}
    19  {{end}}{{if .VisibleFlags}}
    20  Options:
    21  	{{range .VisibleFlags}}{{.}}
    22  	{{end}}{{end}}{{if .ArgsUsage}}
    23  Environment Variables:
    24  {{.ArgsUsage}}{{end}}
    25  
    26  `
    27  
    28  const appHelpTemplate = `NAME:
    29     {{.Name}} - {{.Description}}
    30  
    31  USAGE:
    32     {{if .UsageText}}{{.UsageText}}{{else}}{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} [arguments...]{{end}}
    33     {{if .Version}}
    34  VERSION:
    35     {{.Version}}
    36     {{end}}{{if len .Authors}}
    37  AUTHOR(S):
    38     {{range .Authors}}{{ . }}{{end}}
    39     {{end}}{{if .VisibleCommands}}
    40  COMMANDS:
    41     {{range .VisibleCommands}}{{join .Names ", "}}{{ "\t" }}{{if .Description}}{{.Description}}{{else}}{{.Usage}}{{end}}
    42     {{end}}{{end}}{{if .VisibleFlags}}
    43  GLOBAL OPTIONS:
    44     {{range .VisibleFlags}}{{.}}
    45     {{end}}
    46  {{end}}
    47  
    48  `
    49  
    50  func PluginMain(jfrogApp components.App) {
    51  	coreutils.ExitOnErr(RunCliWithPlugin(jfrogApp)())
    52  }
    53  
    54  // Use os.Args to pass the command name and arguments to the plugin before running the function to run a specific command.
    55  func RunCliWithPlugin(jfrogApp components.App) func() error {
    56  	return func() error {
    57  		log.SetDefaultLogger()
    58  
    59  		// Set the plugin's user-agent as the jfrog-cli-core's.
    60  		utils.SetUserAgent(jfrogclicore.GetUserAgent())
    61  
    62  		cli.CommandHelpTemplate = commandHelpTemplate
    63  		cli.AppHelpTemplate = appHelpTemplate
    64  
    65  		baseApp, err := components.ConvertApp(jfrogApp)
    66  		if err != nil {
    67  			coreutils.ExitOnErr(err)
    68  		}
    69  		addHiddenPluginSignatureCommand(baseApp)
    70  
    71  		args := os.Args
    72  		err = baseApp.Run(args)
    73  
    74  		if cleanupErr := fileutils.CleanOldDirs(); cleanupErr != nil {
    75  			clientLog.Warn(cleanupErr)
    76  		}
    77  
    78  		return err
    79  	}
    80  }