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