|
| 1 | +package components |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "github.com/codegangsta/cli" |
| 7 | + "github.com/jfrog/jfrog-cli-core/docs/common" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +func ConvertApp(jfrogApp App) (*cli.App, error) { |
| 12 | + var err error |
| 13 | + app := cli.NewApp() |
| 14 | + app.Name = jfrogApp.Name |
| 15 | + app.Usage = jfrogApp.Description |
| 16 | + app.Version = jfrogApp.Version |
| 17 | + app.Commands, err = convertCommands(jfrogApp) |
| 18 | + if err != nil { |
| 19 | + return nil, err |
| 20 | + } |
| 21 | + // Defaults: |
| 22 | + app.EnableBashCompletion = true |
| 23 | + return app, nil |
| 24 | +} |
| 25 | + |
| 26 | +func convertCommands(jfrogApp App) ([]cli.Command, error) { |
| 27 | + var converted []cli.Command |
| 28 | + for _, cmd := range jfrogApp.Commands { |
| 29 | + cur, err := convertCommand(cmd, jfrogApp.Name) |
| 30 | + if err != nil { |
| 31 | + return converted, err |
| 32 | + } |
| 33 | + converted = append(converted, cur) |
| 34 | + } |
| 35 | + return converted, nil |
| 36 | +} |
| 37 | + |
| 38 | +func convertCommand(cmd Command, appName string) (cli.Command, error) { |
| 39 | + convertedFlags, err := convertFlags(cmd) |
| 40 | + if err != nil { |
| 41 | + return cli.Command{}, err |
| 42 | + } |
| 43 | + return cli.Command{ |
| 44 | + Name: cmd.Name, |
| 45 | + Flags: convertedFlags, |
| 46 | + Aliases: cmd.Aliases, |
| 47 | + Usage: cmd.Description, |
| 48 | + HelpName: common.CreateUsage(appName+" "+cmd.Name, cmd.Description, []string{createCommandUsage(cmd, appName)}), |
| 49 | + UsageText: createArgumentsSummary(cmd), |
| 50 | + ArgsUsage: createEnvVarsSummary(cmd), |
| 51 | + BashComplete: common.CreateBashCompletionFunc(), |
| 52 | + // Passing any other interface than 'cli.ActionFunc' will fail the command. |
| 53 | + Action: getActionFunc(cmd), |
| 54 | + }, nil |
| 55 | +} |
| 56 | + |
| 57 | +func createCommandUsage(cmd Command, appName string) string { |
| 58 | + usage := fmt.Sprintf("jfrog %s %s", appName, cmd.Name) |
| 59 | + if len(cmd.Flags) > 0 { |
| 60 | + usage += " [command options]" |
| 61 | + } |
| 62 | + for _, argument := range cmd.Arguments { |
| 63 | + usage += fmt.Sprintf(" <%s>", argument.Name) |
| 64 | + } |
| 65 | + return usage |
| 66 | +} |
| 67 | + |
| 68 | +func createArgumentsSummary(cmd Command) string { |
| 69 | + summary := "" |
| 70 | + for i, argument := range cmd.Arguments { |
| 71 | + if i > 0 { |
| 72 | + summary += "\n" |
| 73 | + } |
| 74 | + summary += "\t" + argument.Name + "\n\t\t" + argument.Description + "\n" |
| 75 | + } |
| 76 | + return summary |
| 77 | +} |
| 78 | + |
| 79 | +func createEnvVarsSummary(cmd Command) string { |
| 80 | + var envVarsSummary []string |
| 81 | + for i, env := range cmd.EnvVars { |
| 82 | + summary := "" |
| 83 | + if i > 0 { |
| 84 | + summary += "\n" |
| 85 | + } |
| 86 | + summary = "\t" + env.Name + "\n" |
| 87 | + if env.Default != "" { |
| 88 | + summary += "\t\t[Default: " + env.Default + "]\n" |
| 89 | + } |
| 90 | + summary += "\t\t" + env.Description |
| 91 | + envVarsSummary = append(envVarsSummary, summary) |
| 92 | + } |
| 93 | + return strings.Join(envVarsSummary[:], "\n\n") |
| 94 | +} |
| 95 | + |
| 96 | +func convertFlags(cmd Command) ([]cli.Flag, error) { |
| 97 | + var convertedFlags []cli.Flag |
| 98 | + for _, flag := range cmd.Flags { |
| 99 | + converted, err := convertByType(flag) |
| 100 | + if err != nil { |
| 101 | + return convertedFlags, err |
| 102 | + } |
| 103 | + if converted != nil { |
| 104 | + convertedFlags = append(convertedFlags, converted) |
| 105 | + } |
| 106 | + } |
| 107 | + return convertedFlags, nil |
| 108 | +} |
| 109 | + |
| 110 | +func convertByType(flag Flag) (cli.Flag, error) { |
| 111 | + if f, ok := flag.(StringFlag); ok { |
| 112 | + return convertStringFlag(f), nil |
| 113 | + } |
| 114 | + if f, ok := flag.(BoolFlag); ok { |
| 115 | + return convertBoolFlag(f), nil |
| 116 | + } |
| 117 | + return nil, errors.New(fmt.Sprintf("Flag '%s' does not match any known flag type.", flag.GetName())) |
| 118 | +} |
| 119 | + |
| 120 | +func convertStringFlag(f StringFlag) cli.Flag { |
| 121 | + stringFlag := cli.StringFlag{ |
| 122 | + Name: f.Name, |
| 123 | + Usage: f.Description + "` `", |
| 124 | + } |
| 125 | + // If default is set, add its value and return. |
| 126 | + if f.DefaultValue != "" { |
| 127 | + stringFlag.Usage = fmt.Sprintf("[Default: %s] %s", f.DefaultValue, stringFlag.Usage) |
| 128 | + return stringFlag |
| 129 | + } |
| 130 | + // Otherwise, mark as mandatory/optional accordingly. |
| 131 | + if f.Mandatory { |
| 132 | + stringFlag.Usage = "[Mandatory] " + stringFlag.Usage |
| 133 | + } else { |
| 134 | + stringFlag.Usage = "[Optional] " + stringFlag.Usage |
| 135 | + } |
| 136 | + return stringFlag |
| 137 | +} |
| 138 | + |
| 139 | +func convertBoolFlag(f BoolFlag) cli.Flag { |
| 140 | + if f.DefaultValue { |
| 141 | + return cli.BoolTFlag{ |
| 142 | + Name: f.Name, |
| 143 | + Usage: "[Default: true] " + f.Description + "` `", |
| 144 | + } |
| 145 | + } |
| 146 | + return cli.BoolFlag{ |
| 147 | + Name: f.Name, |
| 148 | + Usage: "[Default: false] " + f.Description + "` `", |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +// Wrap the base's ActionFunc with our own, while retrieving needed information from the Context. |
| 153 | +func getActionFunc(cmd Command) cli.ActionFunc { |
| 154 | + return func(baseContext *cli.Context) error { |
| 155 | + pluginContext := &Context{} |
| 156 | + pluginContext.Arguments = baseContext.Args() |
| 157 | + err := fillFlagMaps(pluginContext, baseContext, cmd.Flags) |
| 158 | + if err != nil { |
| 159 | + return err |
| 160 | + } |
| 161 | + return cmd.Action(pluginContext) |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +func fillFlagMaps(c *Context, baseContext *cli.Context, originalFlags []Flag) error { |
| 166 | + c.stringFlags = make(map[string]string) |
| 167 | + c.boolFlags = make(map[string]bool) |
| 168 | + |
| 169 | + // Loop over all plugin's known flags. |
| 170 | + for _, flag := range originalFlags { |
| 171 | + if stringFlag, ok := flag.(StringFlag); ok { |
| 172 | + finalValue, err := getValueForStringFlag(stringFlag, baseContext.String(stringFlag.Name)) |
| 173 | + if err != nil { |
| 174 | + return err |
| 175 | + } |
| 176 | + c.stringFlags[stringFlag.Name] = finalValue |
| 177 | + continue |
| 178 | + } |
| 179 | + |
| 180 | + if boolFlag, ok := flag.(BoolFlag); ok { |
| 181 | + c.boolFlags[boolFlag.Name] = getValueForBoolFlag(boolFlag, baseContext) |
| 182 | + } |
| 183 | + } |
| 184 | + return nil |
| 185 | +} |
| 186 | + |
| 187 | +func getValueForStringFlag(f StringFlag, receivedValue string) (finalValue string, err error) { |
| 188 | + if receivedValue != "" { |
| 189 | + return receivedValue, nil |
| 190 | + } |
| 191 | + // Empty but has a default value defined. |
| 192 | + if f.DefaultValue != "" { |
| 193 | + return f.DefaultValue, nil |
| 194 | + } |
| 195 | + // Empty but mandatory. |
| 196 | + if f.Mandatory { |
| 197 | + return "", errors.New("Mandatory flag '" + f.Name + "' is missing") |
| 198 | + } |
| 199 | + return "", nil |
| 200 | +} |
| 201 | + |
| 202 | +func getValueForBoolFlag(f BoolFlag, baseContext *cli.Context) bool { |
| 203 | + if f.DefaultValue { |
| 204 | + return baseContext.BoolT(f.Name) |
| 205 | + } |
| 206 | + return baseContext.Bool(f.Name) |
| 207 | +} |
0 commit comments