github.com/awslabs/clencli@v0.0.0-20210514234156-7ecf17182a20/cobra/aid/render.go (about)

     1  package aid
     2  
     3  import (
     4  	"bufio"
     5  	"fmt"
     6  	"os"
     7  	"strings"
     8  
     9  	"github.com/awslabs/clencli/cobra/model"
    10  	"github.com/awslabs/clencli/helper"
    11  	gomplateV3 "github.com/hairyhenderson/gomplate/v3"
    12  	"github.com/sirupsen/logrus"
    13  )
    14  
    15  // BuildTemplate build the given template located under clencli/ directory (without the .tmpl extension)
    16  func BuildTemplate(name string) error {
    17  	var inputFiles = []string{}
    18  	var outputFiles = []string{}
    19  
    20  	sep := string(os.PathSeparator)
    21  
    22  	if helper.FileExists("clencli" + sep + name + ".tmpl") {
    23  		inputFiles = append(inputFiles, "clencli"+sep+name+".tmpl")
    24  		outputFiles = append(outputFiles, strings.ToUpper(name)+".md")
    25  	}
    26  
    27  	var config gomplateV3.Config
    28  	config.InputFiles = inputFiles
    29  	config.OutputFiles = outputFiles
    30  
    31  	dataSources := []string{}
    32  	if helper.FileExists("clencli" + sep + name + ".yaml") {
    33  		dataSources = append(dataSources, "db=."+sep+"clencli"+sep+name+".yaml")
    34  	}
    35  
    36  	config.DataSources = dataSources
    37  
    38  	err := gomplateV3.RunTemplates(&config)
    39  	if err != nil {
    40  		logrus.Fatalf("Gomplate.RunTemplates() failed with %s\n", err)
    41  	}
    42  
    43  	return err
    44  }
    45  
    46  func writeInputs() error {
    47  	variables, err := os.Open("variables.tf")
    48  	if err != nil {
    49  		logrus.Fatal(err)
    50  	}
    51  	defer variables.Close()
    52  
    53  	// create INPUTS.md
    54  	inputs, err := os.OpenFile("INPUTS.md", os.O_CREATE|os.O_WRONLY, 0644)
    55  	if err != nil {
    56  		logrus.Println(err)
    57  	}
    58  	defer inputs.Close()
    59  
    60  	if _, err := inputs.WriteString("| Name | Description | Type | Default | Required |\n|------|-------------|:----:|:-----:|:-----:|\n"); err != nil {
    61  		logrus.Println(err)
    62  	}
    63  
    64  	var varName, varType, varDescription, varDefault string
    65  	varRequired := "no"
    66  
    67  	// startBlock := false
    68  	scanner := bufio.NewScanner(variables)
    69  	for scanner.Scan() {
    70  		line := scanner.Text()
    71  
    72  		// skip empty lines
    73  		if len(line) > 0 {
    74  			if strings.Contains(line, "variable") && strings.Contains(line, "{") {
    75  				out, found := helper.GetStringBetweenDoubleQuotes(line)
    76  				if found {
    77  					varName = out
    78  				}
    79  
    80  			}
    81  
    82  			if strings.Contains(line, "type") && strings.Contains(line, "=") {
    83  				slc := helper.GetStringTrimmed(line, "=")
    84  				if slc[0] == "type" {
    85  					varType = slc[1]
    86  					if strings.Contains(varType, "({") {
    87  						slc = helper.GetStringTrimmed(varType, "({")
    88  						varType = slc[0]
    89  					}
    90  				}
    91  			}
    92  
    93  			if strings.Contains(line, "description") && strings.Contains(line, "=") {
    94  				slc := helper.GetStringTrimmed(line, "=")
    95  				if slc[0] == "description" {
    96  					out, found := helper.GetStringBetweenDoubleQuotes(slc[1])
    97  					if found {
    98  						varDescription = out
    99  					}
   100  				}
   101  			}
   102  
   103  			if strings.Contains(line, "default") && strings.Contains(line, "=") {
   104  				slc := helper.GetStringTrimmed(line, "=")
   105  				if slc[0] == "default" {
   106  					varDefault = slc[1]
   107  					if strings.Contains(varDefault, "{") {
   108  						varDefault = "<map>"
   109  					}
   110  				}
   111  			}
   112  
   113  			// end of the variable declaration
   114  			if strings.Contains(line, "}") && len(line) == 1 {
   115  				if len(varName) > 0 && len(varType) > 0 && len(varDescription) > 0 {
   116  
   117  					var result string
   118  					if len(varDefault) == 0 {
   119  						varRequired = "yes"
   120  						result = fmt.Sprintf("| %s | %s | %s | %s | %s |\n", varName, varDescription, varType, varDefault, varRequired)
   121  					} else {
   122  						result = fmt.Sprintf("| %s | %s | %s | `%s` | %s |\n", varName, varDescription, varType, varDefault, varRequired)
   123  					}
   124  
   125  					if _, err := inputs.WriteString(result); err != nil {
   126  						logrus.Println(err)
   127  					}
   128  					varName, varType, varDescription, varDefault, varRequired = "", "", "", "", "no"
   129  				}
   130  			}
   131  
   132  		}
   133  
   134  	}
   135  
   136  	if err := scanner.Err(); err != nil {
   137  		logrus.Fatal(err)
   138  
   139  	}
   140  	return err
   141  }
   142  
   143  func writeOutputs() error {
   144  	outputs, err := os.Open("outputs.tf")
   145  	if err != nil {
   146  		logrus.Fatal(err)
   147  	}
   148  	defer outputs.Close()
   149  
   150  	// create INPUTS.md
   151  	outs, err := os.OpenFile("OUTPUTS.md", os.O_CREATE|os.O_WRONLY, 0644)
   152  	if err != nil {
   153  		logrus.Println(err)
   154  	}
   155  	defer outs.Close()
   156  
   157  	if _, err := outs.WriteString("| Name | Description |\n|------|-------------|\n"); err != nil {
   158  		logrus.Println(err)
   159  	}
   160  
   161  	var outName, outDescription string
   162  
   163  	scanner := bufio.NewScanner(outputs)
   164  	for scanner.Scan() {
   165  		line := scanner.Text()
   166  
   167  		// skip empty lines
   168  		if len(line) > 0 {
   169  			if strings.Contains(line, "output") && strings.Contains(line, "{") {
   170  				out, found := helper.GetStringBetweenDoubleQuotes(line)
   171  				if found {
   172  					outName = out
   173  				}
   174  			}
   175  
   176  			if strings.Contains(line, "description") && strings.Contains(line, "=") {
   177  				slc := helper.GetStringTrimmed(line, "=")
   178  				if slc[0] == "description" {
   179  					out, found := helper.GetStringBetweenDoubleQuotes(slc[1])
   180  					if found {
   181  						outDescription = out
   182  					}
   183  				}
   184  			}
   185  
   186  			// end of the output declaration
   187  			if strings.Contains(line, "}") && len(line) == 1 {
   188  				if len(outName) > 0 && len(outDescription) > 0 {
   189  
   190  					result := fmt.Sprintf("| %s | %s | |\n", outName, outDescription)
   191  
   192  					if _, err := outs.WriteString(result); err != nil {
   193  						logrus.Println(err)
   194  					}
   195  					outName, outDescription = "", ""
   196  				}
   197  			}
   198  
   199  		}
   200  
   201  	}
   202  
   203  	if err := scanner.Err(); err != nil {
   204  		logrus.Fatal(err)
   205  
   206  	}
   207  	return err
   208  }
   209  
   210  // UpdateReadMeLogoURL TODO ...
   211  func UpdateReadMeLogoURL(readme model.ReadMe, response model.UnsplashRandomPhotoResponse) error {
   212  	readme.Logo.Label = "Photo by [" + response.User.Name + "](https://unsplash.com/" + response.User.Username + ") on [Unsplash](https://unsplash.com)"
   213  	readme.Logo.URL = response.Urls.Regular
   214  	err := WriteInterfaceToFile(readme, "clencli/readme.yaml")
   215  	if err != nil {
   216  		return fmt.Errorf("unable to save new readme template\n%v", err)
   217  	}
   218  
   219  	return nil
   220  }