github.com/cloudfoundry-attic/ltc@v0.0.0-20151123212628-098adc7919fc/cli_app_factory/app_help.go (about)

     1  package cli_app_factory
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"reflect"
     7  	"strings"
     8  	"text/tabwriter"
     9  	"text/template"
    10  	"unicode/utf8"
    11  
    12  	"github.com/codegangsta/cli"
    13  )
    14  
    15  type groupedCommands struct {
    16  	Name             string
    17  	CommandSubGroups [][]cmdPresenter
    18  }
    19  
    20  func (c groupedCommands) SubTitle(name string) string {
    21  	return (name + ":")
    22  }
    23  
    24  type cmdPresenter struct {
    25  	Name        string
    26  	Description string
    27  }
    28  
    29  func presentCmdName(cmd cli.Command) (name string) {
    30  	name = strings.Join(cmd.Names(), ", ")
    31  	return
    32  }
    33  
    34  type appPresenter struct {
    35  	cli.App
    36  	Commands []groupedCommands
    37  }
    38  
    39  func newAppPresenter(app *cli.App) (presenter appPresenter) {
    40  	maxNameLen := 0
    41  	for _, cmd := range app.Commands {
    42  		name := presentCmdName(cmd)
    43  		if utf8.RuneCountInString(name) > maxNameLen {
    44  			maxNameLen = len(name)
    45  		}
    46  	}
    47  
    48  	presentCommand := func(commandName string) (presenter cmdPresenter) {
    49  		cmd := app.Command(commandName)
    50  		presenter.Name = presentCmdName(*cmd)
    51  
    52  		padding := strings.Repeat(" ", maxNameLen-utf8.RuneCountInString(presenter.Name))
    53  		presenter.Name = presenter.Name + padding
    54  		presenter.Description = cmd.Usage
    55  
    56  		return
    57  	}
    58  
    59  	presenter.Name = app.Name
    60  	presenter.Flags = app.Flags
    61  	presenter.Usage = app.Usage
    62  	presenter.Version = app.Version
    63  	presenter.Authors = app.Authors
    64  	presenter.Commands = []groupedCommands{
    65  		{
    66  			Name: "TARGET LATTICE",
    67  			CommandSubGroups: [][]cmdPresenter{
    68  				{
    69  					presentCommand("target"),
    70  				},
    71  			},
    72  		}, {
    73  			Name: "LTC VERSION",
    74  			CommandSubGroups: [][]cmdPresenter{
    75  				{
    76  					presentCommand("version"),
    77  					presentCommand("sync"),
    78  				},
    79  			},
    80  		}, {
    81  			Name: "MANAGE DOCKER APPS",
    82  			CommandSubGroups: [][]cmdPresenter{
    83  				{
    84  					presentCommand("create"),
    85  				},
    86  			},
    87  		}, {
    88  			Name: "MANAGE DROPLETS",
    89  			CommandSubGroups: [][]cmdPresenter{
    90  				{
    91  					presentCommand("build-droplet"),
    92  					presentCommand("export-droplet"),
    93  					presentCommand("import-droplet"),
    94  					presentCommand("launch-droplet"),
    95  					presentCommand("list-droplets"),
    96  					presentCommand("remove-droplet"),
    97  				},
    98  			},
    99  		}, {
   100  			Name: "MANAGE ALL APPS",
   101  			CommandSubGroups: [][]cmdPresenter{
   102  				{
   103  					presentCommand("remove"),
   104  					presentCommand("scale"),
   105  					presentCommand("update"),
   106  				},
   107  			},
   108  		}, {
   109  			Name: "TASKS",
   110  			CommandSubGroups: [][]cmdPresenter{
   111  				{
   112  					presentCommand("cancel-task"),
   113  					presentCommand("delete-task"),
   114  					presentCommand("task"),
   115  				},
   116  			},
   117  		}, {
   118  			Name: "STREAM LOGS",
   119  			CommandSubGroups: [][]cmdPresenter{
   120  				{
   121  					presentCommand("logs"),
   122  				},
   123  			},
   124  		}, {
   125  			Name: "SEE WHATS RUNNING",
   126  			CommandSubGroups: [][]cmdPresenter{
   127  				{
   128  					presentCommand("cells"),
   129  					presentCommand("list"),
   130  					presentCommand("status"),
   131  					presentCommand("visualize"),
   132  				},
   133  			},
   134  		}, {
   135  			Name: "SSH INTO AN APP CONTAINER",
   136  			CommandSubGroups: [][]cmdPresenter{
   137  				{
   138  					presentCommand("ssh"),
   139  				},
   140  			},
   141  		}, {
   142  			Name: "ADVANCED",
   143  			CommandSubGroups: [][]cmdPresenter{
   144  				{
   145  					presentCommand("submit-lrp"),
   146  					presentCommand("submit-task"),
   147  				},
   148  			},
   149  		}, {
   150  			Name: "HELP AND DEBUG",
   151  			CommandSubGroups: [][]cmdPresenter{
   152  				{
   153  					presentCommand("debug-logs"),
   154  					presentCommand("help"),
   155  					presentCommand("test"),
   156  				},
   157  			},
   158  		},
   159  	}
   160  
   161  	return
   162  }
   163  
   164  func ShowHelp(w io.Writer, helpTemplate string, thingToPrint interface{}) {
   165  	translatedTemplatedHelp := strings.Replace(helpTemplate, "{{", "[[", -1)
   166  	translatedTemplatedHelp = strings.Replace(translatedTemplatedHelp, "[[", "{{", -1)
   167  
   168  	switch thing := thingToPrint.(type) {
   169  	case *cli.App:
   170  		showAppHelp(w, translatedTemplatedHelp, thing)
   171  	case cli.Command:
   172  		commandPrintHelp(w, translatedTemplatedHelp, thing)
   173  	default:
   174  		panic(fmt.Errorf("Help printer has received something that is neither app nor command! The beast (%s) looks like this: %s", reflect.TypeOf(thing), thing))
   175  	}
   176  }
   177  
   178  func showAppHelp(w io.Writer, helpTemplate string, appToPrint *cli.App) {
   179  	presenter := newAppPresenter(appToPrint)
   180  	tabWriter := tabwriter.NewWriter(w, 0, 8, 1, '\t', 0)
   181  	t := template.Must(template.New("help").Parse(helpTemplate))
   182  	err := t.Execute(tabWriter, presenter)
   183  	if err != nil {
   184  		panic(err)
   185  	}
   186  	tabWriter.Flush()
   187  }
   188  
   189  func commandPrintHelp(w io.Writer, templ string, data cli.Command) {
   190  	funcMap := template.FuncMap{
   191  		"join": strings.Join,
   192  	}
   193  
   194  	tabWriter := tabwriter.NewWriter(w, 0, 8, 1, '\t', 0)
   195  	t := template.Must(template.New("help").Funcs(funcMap).Parse(templ))
   196  	err := t.Execute(tabWriter, data)
   197  	if err != nil {
   198  		panic(err)
   199  	}
   200  	tabWriter.Flush()
   201  }