agones.dev/agones@v1.53.0/build/scripts/feature-shortcode-update/main.go (about)

     1  // Copyright 2023 Google LLC All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //	http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package main implements a program that removes the contents of feature expiry version and publish version shortcodes in .md files within the site/content/en/docs directory.
    16  package main
    17  
    18  import (
    19  	"bufio"
    20  	"flag"
    21  	"log"
    22  	"os"
    23  	"path/filepath"
    24  	"strings"
    25  )
    26  
    27  func main() {
    28  	dirPath := "site/content/en/docs"
    29  
    30  	version := flag.String("version", "", "Expiry version to remove")
    31  	flag.Parse()
    32  
    33  	// Check if the version is provided
    34  	if *version == "" {
    35  		log.Fatal("Version not specified. Please provide a value for the -version flag in CLI.")
    36  	}
    37  
    38  	modifiedFiles := 0
    39  
    40  	err := filepath.WalkDir(dirPath, func(filePath string, d os.DirEntry, err error) error {
    41  		if err != nil {
    42  			log.Fatal(err)
    43  		}
    44  
    45  		if d.IsDir() || !strings.HasSuffix(d.Name(), ".md") {
    46  			return nil
    47  		}
    48  
    49  		file, err := os.Open(filePath)
    50  		if err != nil {
    51  			log.Fatal(err)
    52  		}
    53  		defer func() {
    54  			if err := file.Close(); err != nil {
    55  				log.Fatal(err)
    56  			}
    57  		}()
    58  
    59  		scanner := bufio.NewScanner(file)
    60  		modifiedContent := removeBlocks(scanner, *version)
    61  
    62  		// Only write the modified content back to the .md file if there are changes
    63  		if modifiedContent != "" {
    64  			outputFile, err := os.Create(filePath)
    65  			if err != nil {
    66  				log.Fatal(err)
    67  			}
    68  			defer func() {
    69  				if err := outputFile.Close(); err != nil {
    70  					log.Fatal(err)
    71  				}
    72  			}()
    73  
    74  			writer := bufio.NewWriter(outputFile)
    75  			_, err = writer.WriteString(modifiedContent)
    76  			if err != nil {
    77  				log.Fatal(err)
    78  			}
    79  
    80  			// Flush the writer to ensure all content is written
    81  			if err := writer.Flush(); err != nil {
    82  				log.Fatal(err)
    83  			}
    84  
    85  			log.Printf("Processed file: %s\n", filePath)
    86  			modifiedFiles++
    87  		}
    88  
    89  		return nil
    90  	})
    91  
    92  	if err != nil {
    93  		log.Fatal(err)
    94  	}
    95  
    96  	if modifiedFiles == 0 {
    97  		log.Println("There are no files with feature expiryVersion or publishVersion shortcodes")
    98  	}
    99  }
   100  
   101  func removeBlocks(scanner *bufio.Scanner, version string) string {
   102  	var sb strings.Builder
   103  	expiryBlock := false
   104  	publishBlock := false
   105  	preserveLines := true
   106  	modified := false
   107  
   108  	for scanner.Scan() {
   109  		line := scanner.Text()
   110  
   111  		// Check if the line contains the starting of the expiryVersion shortcode with the specified version
   112  		if strings.Contains(line, "{{% feature expiryVersion=\""+version+"\" %}}") ||
   113  			strings.Contains(line, "{{< feature expiryVersion=\""+version+"\" >}}") {
   114  			expiryBlock = true
   115  			preserveLines = false
   116  			modified = true
   117  			continue
   118  		}
   119  
   120  		// Check if the line contains the ending of the expiryVersion shortcode
   121  		if (strings.Contains(line, "{{% /feature %}}") && expiryBlock) ||
   122  			(strings.Contains(line, "{{< /feature >}}") && expiryBlock) {
   123  			expiryBlock = false
   124  			preserveLines = true
   125  			modified = true
   126  			continue
   127  		}
   128  
   129  		// Check if the line contains the starting of the publishVersion shortcode with the specified version
   130  		if strings.Contains(line, "{{% feature publishVersion=\""+version+"\" %}}") ||
   131  			strings.Contains(line, "{{< feature publishVersion=\""+version+"\" >}}") {
   132  			publishBlock = true
   133  			modified = true
   134  			continue
   135  		}
   136  
   137  		// Check if the line contains the ending of the publishVersion shortcode
   138  		if (strings.Contains(line, "{{% /feature %}}") && publishBlock) ||
   139  			(strings.Contains(line, "{{< /feature >}}") && publishBlock) {
   140  			publishBlock = false
   141  			modified = true
   142  			continue
   143  		}
   144  
   145  		// Preserve the line if it is not within an expiryVersion or publishVersion block
   146  		if preserveLines {
   147  			sb.WriteString(line)
   148  			sb.WriteString("\n")
   149  		}
   150  	}
   151  
   152  	// If no modifications were made, return an empty string
   153  	if !modified {
   154  		return ""
   155  	}
   156  
   157  	return sb.String()
   158  }