github.com/dancsecs/gotomd@v0.0.0-20240310162206-65c4805cf510/process.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  package main
    20  
    21  import (
    22  	"errors"
    23  	"flag"
    24  	"fmt"
    25  	"os"
    26  	"strings"
    27  )
    28  
    29  //nolint:forbidigo // Ok.
    30  func confirmOverwrite(fPath string, data string) (bool, error) {
    31  	var (
    32  		ok      bool
    33  		oldData []byte
    34  	)
    35  
    36  	_, err := os.Stat(fPath)
    37  	if errors.Is(err, os.ErrNotExist) {
    38  		return true, nil
    39  	}
    40  
    41  	if err == nil {
    42  		oldData, err = os.ReadFile(fPath) //nolint:gosec // Ok.
    43  	}
    44  
    45  	if err == nil && strings.TrimRight(string(oldData), "\n") == data {
    46  		fmt.Println("No change: " + fPath)
    47  
    48  		return false, nil
    49  	}
    50  
    51  	if err == nil {
    52  		fmt.Print("Confirm overwrite of ", fPath, " (Y to overwrite)? ")
    53  
    54  		var response string
    55  
    56  		if _, err = fmt.Scanln(&response); err == nil {
    57  			ok = response == "Y"
    58  			if !ok {
    59  				fmt.Println("overwrite cancelled")
    60  			}
    61  		}
    62  	}
    63  
    64  	return ok, err //nolint:wrapcheck // Caller will wrap error.
    65  }
    66  
    67  func writeFile(fPath string, data string) error {
    68  	var err error
    69  
    70  	data = strings.ReplaceAll(data, "\t", "    ")
    71  	okToOverwrite := forceOverwrite
    72  
    73  	if !okToOverwrite {
    74  		okToOverwrite, err = confirmOverwrite(fPath, data)
    75  	}
    76  
    77  	if err == nil && okToOverwrite {
    78  		var file *os.File
    79  
    80  		//nolint:gosec // Ok.
    81  		file, err = os.OpenFile(fPath,
    82  			os.O_TRUNC|os.O_WRONLY|os.O_CREATE,
    83  			os.FileMode(defaultPerm),
    84  		)
    85  		if err == nil {
    86  			_, err = file.WriteString(data + "\n")
    87  			if err == nil {
    88  				err = file.Close()
    89  			}
    90  		}
    91  	}
    92  
    93  	return err //nolint:wrapcheck // Caller will wrap error.
    94  }
    95  
    96  //nolint:cyclop,forbidigo // Ok.
    97  func getFilesToProcess() ([]string, error) {
    98  	var (
    99  		err            error
   100  		files          []os.DirEntry
   101  		stat           os.FileInfo
   102  		filesToProcess []string
   103  		filter         = ".md"
   104  	)
   105  
   106  	if !cleanOnly && !replace {
   107  		filter += ".gtm"
   108  	}
   109  
   110  	for i, mi := 0, flag.NArg(); i < mi && err == nil; i++ {
   111  		stat, err = os.Stat(flag.Arg(i))
   112  		if err == nil && stat.IsDir() {
   113  			files, err = os.ReadDir(flag.Arg(i))
   114  			for j, mj := 0, len(files); j < mj && err == nil; j++ {
   115  				fName := files[j].Name()
   116  				if strings.HasSuffix(fName, filter) {
   117  					filesToProcess = append(filesToProcess,
   118  						flag.Arg(i)+string(os.PathSeparator)+fName,
   119  					)
   120  
   121  					if verbose {
   122  						fmt.Println("filesToProcess: ",
   123  							flag.Arg(i)+string(os.PathSeparator)+fName,
   124  						)
   125  					}
   126  				}
   127  			}
   128  		}
   129  
   130  		if err == nil && !stat.IsDir() {
   131  			if !strings.HasSuffix(stat.Name(), filter) {
   132  				err = fmt.Errorf(
   133  					"%w: expected - %s",
   134  					ErrUnexpectedExtension, filter,
   135  				)
   136  			} else {
   137  				filesToProcess = append(filesToProcess, flag.Arg(i))
   138  
   139  				if verbose {
   140  					fmt.Println("filesToProcess: ", flag.Arg(i))
   141  				}
   142  			}
   143  		}
   144  	}
   145  
   146  	return filesToProcess, err
   147  }