github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/help/help.go (about)

     1  package help
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  	"strings"
     8  	"text/tabwriter"
     9  	"text/template"
    10  	"unicode/utf8"
    11  
    12  	"path/filepath"
    13  
    14  	"code.cloudfoundry.org/cli/cf/commandregistry"
    15  	"code.cloudfoundry.org/cli/cf/configuration"
    16  	"code.cloudfoundry.org/cli/cf/configuration/confighelpers"
    17  	"code.cloudfoundry.org/cli/cf/configuration/pluginconfig"
    18  	. "code.cloudfoundry.org/cli/cf/i18n"
    19  	"code.cloudfoundry.org/cli/cf/terminal"
    20  	"code.cloudfoundry.org/cli/version"
    21  )
    22  
    23  type appPresenter struct {
    24  	Name     string
    25  	Usage    string
    26  	Version  string
    27  	Compiled string
    28  	Commands []groupedCommands
    29  }
    30  
    31  type groupedCommands struct {
    32  	Name             string
    33  	CommandSubGroups [][]cmdPresenter
    34  }
    35  
    36  type cmdPresenter struct {
    37  	Name        string
    38  	Description string
    39  }
    40  
    41  func ShowHelp(writer io.Writer, helpTemplate string) {
    42  	translatedTemplatedHelp := T(strings.Replace(helpTemplate, "{{", "[[", -1))
    43  	translatedTemplatedHelp = strings.Replace(translatedTemplatedHelp, "[[", "{{", -1)
    44  
    45  	showAppHelp(writer, translatedTemplatedHelp)
    46  }
    47  
    48  func showAppHelp(writer io.Writer, helpTemplate string) {
    49  	presenter := newAppPresenter()
    50  
    51  	w := tabwriter.NewWriter(writer, 0, 8, 1, '\t', 0)
    52  	t := template.Must(template.New("help").Parse(helpTemplate))
    53  	err := t.Execute(w, presenter)
    54  	if err != nil {
    55  		fmt.Println("error", err)
    56  	}
    57  	_ = w.Flush()
    58  }
    59  
    60  func newAppPresenter() appPresenter {
    61  	var presenter appPresenter
    62  
    63  	pluginPath := filepath.Join(confighelpers.PluginRepoDir(), ".cf", "plugins")
    64  
    65  	pluginConfig := pluginconfig.NewPluginConfig(
    66  		func(err error) {
    67  			//fail silently when running help
    68  		},
    69  		configuration.NewDiskPersistor(filepath.Join(pluginPath, "config.json")),
    70  		pluginPath,
    71  	)
    72  
    73  	plugins := pluginConfig.Plugins()
    74  
    75  	maxNameLen := commandregistry.Commands.MaxCommandNameLength()
    76  	maxNameLen = maxPluginCommandNameLength(plugins, maxNameLen)
    77  
    78  	presentCommand := func(commandName string) (presenter cmdPresenter) {
    79  		cmd := commandregistry.Commands.FindCommand(commandName)
    80  		presenter.Name = cmd.MetaData().Name
    81  		padding := strings.Repeat(" ", maxNameLen-utf8.RuneCountInString(presenter.Name))
    82  		presenter.Name = presenter.Name + padding
    83  		presenter.Description = cmd.MetaData().Description
    84  		return
    85  	}
    86  
    87  	presentPluginCommands := func() []cmdPresenter {
    88  		var presenters []cmdPresenter
    89  		var pluginPresenter cmdPresenter
    90  
    91  		for _, pluginMetadata := range plugins {
    92  			for _, cmd := range pluginMetadata.Commands {
    93  				pluginPresenter.Name = cmd.Name
    94  
    95  				padding := strings.Repeat(" ", maxNameLen-utf8.RuneCountInString(pluginPresenter.Name))
    96  				pluginPresenter.Name = pluginPresenter.Name + padding
    97  				pluginPresenter.Description = cmd.HelpText
    98  				presenters = append(presenters, pluginPresenter)
    99  			}
   100  		}
   101  
   102  		return presenters
   103  	}
   104  
   105  	presenter.Name = os.Args[0]
   106  	presenter.Usage = T("A command line tool to interact with Cloud Foundry")
   107  	presenter.Version = version.VersionString()
   108  	presenter.Commands = []groupedCommands{
   109  		{
   110  			Name: T("GETTING STARTED"),
   111  			CommandSubGroups: [][]cmdPresenter{
   112  				{
   113  					presentCommand("help"),
   114  					presentCommand("version"),
   115  					presentCommand("login"),
   116  					presentCommand("logout"),
   117  					presentCommand("passwd"),
   118  					presentCommand("target"),
   119  				}, {
   120  					presentCommand("api"),
   121  					presentCommand("auth"),
   122  				},
   123  			},
   124  		}, {
   125  			Name: T("APPS"),
   126  			CommandSubGroups: [][]cmdPresenter{
   127  				{
   128  					presentCommand("apps"),
   129  					presentCommand("app"),
   130  				}, {
   131  					presentCommand("push"),
   132  					presentCommand("scale"),
   133  					presentCommand("delete"),
   134  					presentCommand("rename"),
   135  				}, {
   136  					presentCommand("start"),
   137  					presentCommand("stop"),
   138  					presentCommand("restart"),
   139  					presentCommand("restage"),
   140  					presentCommand("restart-app-instance"),
   141  				}, {
   142  					presentCommand("events"),
   143  					presentCommand("files"),
   144  					presentCommand("logs"),
   145  				}, {
   146  					presentCommand("env"),
   147  					presentCommand("set-env"),
   148  					presentCommand("unset-env"),
   149  				}, {
   150  					presentCommand("stacks"),
   151  					presentCommand("stack"),
   152  				}, {
   153  					presentCommand("copy-source"),
   154  				}, {
   155  					presentCommand("create-app-manifest"),
   156  				}, {
   157  					presentCommand("get-health-check"),
   158  					presentCommand("set-health-check"),
   159  					presentCommand("enable-ssh"),
   160  					presentCommand("disable-ssh"),
   161  					presentCommand("ssh-enabled"),
   162  					presentCommand("ssh"),
   163  				},
   164  			},
   165  		}, {
   166  			Name: T("SERVICES"),
   167  			CommandSubGroups: [][]cmdPresenter{
   168  				{
   169  					presentCommand("marketplace"),
   170  					presentCommand("services"),
   171  					presentCommand("service"),
   172  				}, {
   173  					presentCommand("create-service"),
   174  					presentCommand("update-service"),
   175  					presentCommand("delete-service"),
   176  					presentCommand("rename-service"),
   177  				}, {
   178  					presentCommand("create-service-key"),
   179  					presentCommand("service-keys"),
   180  					presentCommand("service-key"),
   181  					presentCommand("delete-service-key"),
   182  				}, {
   183  					presentCommand("bind-service"),
   184  					presentCommand("unbind-service"),
   185  				}, {
   186  					presentCommand("bind-route-service"),
   187  					presentCommand("unbind-route-service"),
   188  				}, {
   189  					presentCommand("create-user-provided-service"),
   190  					presentCommand("update-user-provided-service"),
   191  				},
   192  			},
   193  		}, {
   194  			Name: T("ORGS"),
   195  			CommandSubGroups: [][]cmdPresenter{
   196  				{
   197  					presentCommand("orgs"),
   198  					presentCommand("org"),
   199  				}, {
   200  					presentCommand("create-org"),
   201  					presentCommand("delete-org"),
   202  					presentCommand("rename-org"),
   203  				},
   204  			},
   205  		}, {
   206  			Name: T("SPACES"),
   207  			CommandSubGroups: [][]cmdPresenter{
   208  				{
   209  					presentCommand("spaces"),
   210  					presentCommand("space"),
   211  				}, {
   212  					presentCommand("create-space"),
   213  					presentCommand("delete-space"),
   214  					presentCommand("rename-space"),
   215  				}, {
   216  					presentCommand("allow-space-ssh"),
   217  					presentCommand("disallow-space-ssh"),
   218  					presentCommand("space-ssh-allowed"),
   219  				},
   220  			},
   221  		}, {
   222  			Name: T("DOMAINS"),
   223  			CommandSubGroups: [][]cmdPresenter{
   224  				{
   225  					presentCommand("domains"),
   226  					presentCommand("create-domain"),
   227  					presentCommand("delete-domain"),
   228  					presentCommand("create-shared-domain"),
   229  					presentCommand("delete-shared-domain"),
   230  				},
   231  				{
   232  					presentCommand("router-groups"),
   233  				},
   234  			},
   235  		}, {
   236  			Name: T("ROUTES"),
   237  			CommandSubGroups: [][]cmdPresenter{
   238  				{
   239  					presentCommand("routes"),
   240  					presentCommand("create-route"),
   241  					presentCommand("check-route"),
   242  					presentCommand("map-route"),
   243  					presentCommand("unmap-route"),
   244  					presentCommand("delete-route"),
   245  					presentCommand("delete-orphaned-routes"),
   246  				},
   247  			},
   248  		}, {
   249  			Name: T("BUILDPACKS"),
   250  			CommandSubGroups: [][]cmdPresenter{
   251  				{
   252  					presentCommand("buildpacks"),
   253  					presentCommand("create-buildpack"),
   254  					presentCommand("update-buildpack"),
   255  					presentCommand("rename-buildpack"),
   256  					presentCommand("delete-buildpack"),
   257  				},
   258  			},
   259  		}, {
   260  			Name: T("USER ADMIN"),
   261  			CommandSubGroups: [][]cmdPresenter{
   262  				{
   263  					presentCommand("create-user"),
   264  					presentCommand("delete-user"),
   265  				}, {
   266  					presentCommand("org-users"),
   267  					presentCommand("set-org-role"),
   268  					presentCommand("unset-org-role"),
   269  				}, {
   270  					presentCommand("space-users"),
   271  					presentCommand("set-space-role"),
   272  					presentCommand("unset-space-role"),
   273  				},
   274  			},
   275  		}, {
   276  			Name: T("ORG ADMIN"),
   277  			CommandSubGroups: [][]cmdPresenter{
   278  				{
   279  					presentCommand("quotas"),
   280  					presentCommand("quota"),
   281  					presentCommand("set-quota"),
   282  				}, {
   283  					presentCommand("create-quota"),
   284  					presentCommand("delete-quota"),
   285  					presentCommand("update-quota"),
   286  				},
   287  				{
   288  					presentCommand("share-private-domain"),
   289  					presentCommand("unshare-private-domain"),
   290  				},
   291  			},
   292  		}, {
   293  			Name: T("SPACE ADMIN"),
   294  			CommandSubGroups: [][]cmdPresenter{
   295  				{
   296  					presentCommand("space-quotas"),
   297  					presentCommand("space-quota"),
   298  					presentCommand("create-space-quota"),
   299  					presentCommand("update-space-quota"),
   300  					presentCommand("delete-space-quota"),
   301  					presentCommand("set-space-quota"),
   302  					presentCommand("unset-space-quota"),
   303  				},
   304  			},
   305  		}, {
   306  			Name: T("SERVICE ADMIN"),
   307  			CommandSubGroups: [][]cmdPresenter{
   308  				{
   309  					presentCommand("service-auth-tokens"),
   310  					presentCommand("create-service-auth-token"),
   311  					presentCommand("update-service-auth-token"),
   312  					presentCommand("delete-service-auth-token"),
   313  				}, {
   314  					presentCommand("service-brokers"),
   315  					presentCommand("create-service-broker"),
   316  					presentCommand("update-service-broker"),
   317  					presentCommand("delete-service-broker"),
   318  					presentCommand("rename-service-broker"),
   319  				}, {
   320  					presentCommand("migrate-service-instances"),
   321  					presentCommand("purge-service-offering"),
   322  					presentCommand("purge-service-instance"),
   323  				}, {
   324  					presentCommand("service-access"),
   325  					presentCommand("enable-service-access"),
   326  					presentCommand("disable-service-access"),
   327  				},
   328  			},
   329  		}, {
   330  			Name: T("SECURITY GROUP"),
   331  			CommandSubGroups: [][]cmdPresenter{
   332  				{
   333  					presentCommand("security-group"),
   334  					presentCommand("security-groups"),
   335  					presentCommand("create-security-group"),
   336  					presentCommand("update-security-group"),
   337  					presentCommand("delete-security-group"),
   338  					presentCommand("bind-security-group"),
   339  					presentCommand("unbind-security-group"),
   340  				}, {
   341  					presentCommand("bind-staging-security-group"),
   342  					presentCommand("staging-security-groups"),
   343  					presentCommand("unbind-staging-security-group"),
   344  				}, {
   345  					presentCommand("bind-running-security-group"),
   346  					presentCommand("running-security-groups"),
   347  					presentCommand("unbind-running-security-group"),
   348  				},
   349  			},
   350  		}, {
   351  			Name: T("ENVIRONMENT VARIABLE GROUPS"),
   352  			CommandSubGroups: [][]cmdPresenter{
   353  				{
   354  					presentCommand("running-environment-variable-group"),
   355  					presentCommand("staging-environment-variable-group"),
   356  					presentCommand("set-staging-environment-variable-group"),
   357  					presentCommand("set-running-environment-variable-group"),
   358  				},
   359  			},
   360  		},
   361  		{
   362  			Name: T("FEATURE FLAGS"),
   363  			CommandSubGroups: [][]cmdPresenter{
   364  				{
   365  					presentCommand("feature-flags"),
   366  					presentCommand("feature-flag"),
   367  					presentCommand("enable-feature-flag"),
   368  					presentCommand("disable-feature-flag"),
   369  				},
   370  			},
   371  		}, {
   372  			Name: T("ADVANCED"),
   373  			CommandSubGroups: [][]cmdPresenter{
   374  				{
   375  					presentCommand("curl"),
   376  					presentCommand("config"),
   377  					presentCommand("oauth-token"),
   378  					presentCommand("ssh-code"),
   379  				},
   380  			},
   381  		}, {
   382  			Name: T("ADD/REMOVE PLUGIN REPOSITORY"),
   383  			CommandSubGroups: [][]cmdPresenter{
   384  				{
   385  					presentCommand("add-plugin-repo"),
   386  					presentCommand("remove-plugin-repo"),
   387  					presentCommand("list-plugin-repos"),
   388  					presentCommand("repo-plugins"),
   389  				},
   390  			},
   391  		}, {
   392  			Name: T("ADD/REMOVE PLUGIN"),
   393  			CommandSubGroups: [][]cmdPresenter{
   394  				{
   395  					presentCommand("plugins"),
   396  					presentCommand("install-plugin"),
   397  					presentCommand("uninstall-plugin"),
   398  				},
   399  			},
   400  		}, {
   401  			Name: T("INSTALLED PLUGIN COMMANDS"),
   402  			CommandSubGroups: [][]cmdPresenter{
   403  				presentPluginCommands(),
   404  			},
   405  		},
   406  	}
   407  
   408  	return presenter
   409  }
   410  
   411  func (p appPresenter) Title(name string) string {
   412  	return terminal.HeaderColor(name)
   413  }
   414  
   415  func (c groupedCommands) SubTitle(name string) string {
   416  	return terminal.HeaderColor(name + ":")
   417  }
   418  
   419  func maxPluginCommandNameLength(plugins map[string]pluginconfig.PluginMetadata, maxNameLen int) int {
   420  	for _, pluginMetadata := range plugins {
   421  		for _, cmd := range pluginMetadata.Commands {
   422  			if nameLen := utf8.RuneCountInString(cmd.Name); nameLen > maxNameLen {
   423  				maxNameLen = nameLen
   424  			}
   425  		}
   426  	}
   427  
   428  	return maxNameLen
   429  }