github.com/wawandco/oxpecker@v1.5.7-0.20210910201653-5958d4afdd89/lifecycle/generate/generate.go (about)

     1  // generate package provides the base of the generate command
     2  // which allows to run generators for tools.
     3  package generate
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  	"os"
     9  	"text/tabwriter"
    10  
    11  	"github.com/wawandco/oxpecker/internal/log"
    12  	"github.com/wawandco/oxpecker/plugins"
    13  )
    14  
    15  //HelpText returns the help Text of build function
    16  
    17  var _ plugins.Command = (*Command)(nil)
    18  var _ plugins.PluginReceiver = (*Command)(nil)
    19  
    20  type Command struct {
    21  	generators      []Generator
    22  	aftergenerators []AfterGenerator
    23  }
    24  
    25  func (c Command) Name() string {
    26  	return "generate"
    27  }
    28  
    29  func (c Command) Alias() string {
    30  	return "g"
    31  }
    32  
    33  func (c Command) ParentName() string {
    34  	return ""
    35  }
    36  
    37  func (c Command) HelpText() string {
    38  	return "Allows to invoke registered generator plugins"
    39  }
    40  
    41  func (c *Command) Run(ctx context.Context, root string, args []string) error {
    42  	if len(args) < 2 {
    43  		c.list()
    44  
    45  		return fmt.Errorf("no generator name specified")
    46  	}
    47  
    48  	name := args[1]
    49  	var generator Generator
    50  	//Run each of the fixers registered.
    51  	for _, gen := range c.generators {
    52  		if gen.InvocationName() != name {
    53  			continue
    54  		}
    55  
    56  		generator = gen
    57  		break
    58  	}
    59  
    60  	if generator == nil {
    61  		c.list()
    62  		return fmt.Errorf("generator `%v` not found", name)
    63  	}
    64  
    65  	err := generator.Generate(ctx, root, args)
    66  	for _, ag := range c.aftergenerators {
    67  		aerr := ag.AfterGenerate(ctx, root, args)
    68  		if aerr != nil {
    69  			log.Errorf("Error running after generator %v: %v", ag.Name(), aerr)
    70  		}
    71  	}
    72  
    73  	return err
    74  }
    75  
    76  func (c Command) list() {
    77  	w := new(tabwriter.Writer)
    78  	defer w.Flush()
    79  
    80  	// minwidth, tabwidth, padding, padchar, flags
    81  	w.Init(os.Stdout, 8, 8, 3, '\t', 0)
    82  	fmt.Printf("Available Generators:\n\n")
    83  	fmt.Fprintf(w, "  Name\tPlugin\n")
    84  	fmt.Fprintf(w, "  ----\t------\n")
    85  	for _, plugin := range c.generators {
    86  		helpText := ""
    87  		if ht, ok := plugin.(plugins.HelpTexter); ok {
    88  			helpText = ht.HelpText()
    89  		}
    90  
    91  		fmt.Fprintf(w, "  %v\t%v\t%v\n", plugin.InvocationName(), plugin.Name(), helpText)
    92  	}
    93  
    94  	fmt.Fprintf(w, "\n")
    95  }
    96  
    97  func (c *Command) Receive(plugins []plugins.Plugin) {
    98  	for _, plugin := range plugins {
    99  		ptool, ok := plugin.(Generator)
   100  		if !ok {
   101  			continue
   102  		}
   103  
   104  		c.generators = append(c.generators, ptool)
   105  	}
   106  
   107  	for _, plugin := range plugins {
   108  		ptool, ok := plugin.(AfterGenerator)
   109  		if !ok {
   110  			continue
   111  		}
   112  
   113  		c.aftergenerators = append(c.aftergenerators, ptool)
   114  	}
   115  }