github.com/dancsecs/gotomd@v0.0.0-20240310162206-65c4805cf510/main.go (about)

     1  /*
     2     Golang To Github Markdown Utility: gotomd
     3     Copyright (C) 2023, 2024 Leslie Dancsecs
     4  
     5     This program is free software: you can redistribute it and/or modify
     6     it under the terms of the GNU General Public License as published by
     7     the Free Software Foundation, either version 3 of the License, or
     8     (at your option) any later version.
     9  
    10     This program is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13     GNU General Public License for more details.
    14  
    15     You should have received a copy of the GNU General Public License
    16     along with this program.  If not, see <https://www.gnu.org/licenses/>.
    17  */
    18  
    19  //nolint:lll // Ok.
    20  /*
    21  The gotomd utility provides for the maintenance of github README.MD style
    22  pages by permitting go files, go documentation and go test output to be
    23  included by reference into the github README.md file directly from the Go
    24  code permitting program documentation to be maintained in one place (the Go
    25  code.)
    26  
    27  It can use a template file (```*.md.gtm```) or can maintain a ```*.md``` file
    28  in place.
    29  
    30  Usage of gotomd [-c | -r] [-fvl] [-p perm] [-o outDir] [-U file] [-u uint] path [path...]
    31  
    32  The flags are:
    33  
    34    -c
    35        Reverse operation and remove generated markdown (Cannot be used
    36        with the -r option). Files with a .md extension are expected with
    37        a an .md.gtm file being produced.
    38    -f
    39        Do not confirm overwrite of destination.
    40    -l
    41        Display license before program exits.
    42    -o string
    43        Direct all output to the specified directory. (default ".")
    44    -p int
    45        Permissions to use when creating new file (can only set RW
    46        bits). (default 420)
    47    -r
    48        Replace the *.MD in place (Cannot be used with the -c flag).
    49    -U  string
    50        Collect cpu profile data into named file.
    51    -u  uint
    52        Number of iterations to run when collecting cpu profile information.
    53    -v
    54        Provide more information when processing.
    55  
    56  Directives are placed into the ```*.md.gtm``` file (or directly into the
    57  ```*.md``` document if the replace in place option is given.  These directives
    58  are embedded into HTML style comments.
    59  
    60  ```html
    61  <!--- gotomd::ACTION::PARAMETERS -->
    62  ```
    63  
    64  where ACTION can be one of the following:
    65  
    66  - gotomd::doc::./relativeDirectory/goObject
    67  
    68      Run the go doc command on the object listed from the directory
    69      specified.  The PARAMETER is formatted with the relative directory up
    70      to the last directory separator before the end of the string and the
    71      desired object.  A special object package returns the package
    72      comments.
    73  
    74  - gotomd::dcls::./relativeDirectory/declaredObject ListOfDeclaredGoObjects
    75  
    76      Pull out the declaration for the object and include as a single-line
    77      regardless of how declared in the go code.  The Parameter is a list of
    78      go functions, methods and constants (more object coming) to be included
    79      in a go code block. No comments are included.
    80  
    81  - gotomd::dcln::./relativeDirectory/declaredObject ListOfDeclaredGoObjects
    82  
    83      Pull the declaration and include exactly as declared in the go
    84      source including leading comments.
    85  
    86  - gotomd::dcl::./relativeDirectory/declaredObject ListOfDeclaredGoObjects
    87  
    88      Pull the declaration and include exactly as declared in the go
    89      source.  No Comments are included.
    90  
    91  - gotomd::tst::goTest::./relativeDirectory/testName
    92  
    93      Run go test with the tests listed (or package) to run all tests and
    94      included the output.
    95  
    96  - gotomd::file::./relativeDirectory/fName
    97  
    98      Include the specified file in a code block.
    99  
   100  
   101  When expanded in the target file the content will be framed by similar
   102  comments prefixed with 'Bgn' and 'End' as:
   103  
   104  const sztestBgnPrefix = sztestPrefix + "Bgn::"
   105  const sztestEndPrefix = sztestPrefix + "End::"
   106  
   107  A header prefixed with
   108  
   109  const szAutoPrefix = sztestPrefix + "Auto::"
   110  
   111  and a blank line following will be inserted into the output file.  If the
   112  action is not "replace in place" then an addition **DO NOT MODIFY**
   113  warning is included.
   114  */
   115  package main
   116  
   117  import (
   118  	"fmt"
   119  	"os"
   120  )
   121  
   122  const license = `
   123  Golang To Github Markdown: gotomd.
   124  Copyright (C) 2023, 2024  Leslie Dancsecs
   125  
   126  This program is free software: you can redistribute it and/or modify
   127  it under the terms of the GNU General Public License as published by
   128  the Free Software Foundation, either version 3 of the License, or
   129  (at your option) any later version.
   130  
   131  This program is distributed in the hope that it will be useful,
   132  but WITHOUT ANY WARRANTY; without even the implied warranty of
   133  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   134  GNU General Public License for more details.
   135  
   136  You should have received a copy of the GNU General Public License
   137  along with this program.  If not, see <https://www.gnu.org/licenses/>.
   138  `
   139  
   140  func main() {
   141  	var (
   142  		err            error
   143  		origWd         string
   144  		filesToProcess []string
   145  	)
   146  
   147  	// Restore original working directory on exit.
   148  	origWd, err = os.Getwd()
   149  	if err == nil {
   150  		defer func() {
   151  			_ = os.Chdir(origWd)
   152  		}()
   153  	}
   154  
   155  	processArgs()
   156  
   157  	if showLicense {
   158  		fmt.Print(license) //nolint:forbidigo // Ok.
   159  	}
   160  
   161  	filesToProcess, err = getFilesToProcess()
   162  
   163  	for i, mi := 0, len(filesToProcess); i < mi && err == nil; i++ {
   164  		switch {
   165  		case cleanOnly:
   166  			//   err = cleanMD(filesToProcess[i])
   167  			err = cleanMD(filesToProcess[i])
   168  		case replace:
   169  			err = replaceMDInPlace(filesToProcess[i])
   170  		default:
   171  			err = expandMD(filesToProcess[i])
   172  		}
   173  	}
   174  
   175  	if err != nil {
   176  		panic(err)
   177  	}
   178  }