agones.dev/agones@v1.54.0/build/scripts/update-navbar-version/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  // The main package implements a program that removes the old version of the link in `navbarDropdownMenuLink` found in the `site/layouts/partials/navbar.html` file
    16  package main
    17  
    18  import (
    19  	"flag"
    20  	"log"
    21  	"os"
    22  	"regexp"
    23  	"strings"
    24  )
    25  
    26  func removeLastDropdownItem(input string) string {
    27  	re := regexp.MustCompile(`(?s)(<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">)(.*?)(</div>)`)
    28  	matches := re.FindStringSubmatch(input)
    29  
    30  	// Determine the indentation before the <div>
    31  	divStartPos := strings.Index(input, matches[1])
    32  	divIndentation := ""
    33  	for i := divStartPos - 1; i >= 0 && (input[i] == ' ' || input[i] == '\t'); i-- {
    34  		divIndentation = string(input[i]) + divIndentation
    35  	}
    36  
    37  	// Split links inside the dropdown
    38  	links := strings.Split(matches[2], "</a>")
    39  
    40  	updatedLinks := strings.Join(links[:len(links)-2], "</a>") + "</a>\n" + divIndentation
    41  
    42  	return strings.Replace(input, matches[2], updatedLinks, 1)
    43  }
    44  
    45  func main() {
    46  	filePath := flag.String("file", "", "HTML File Path")
    47  	flag.Parse()
    48  
    49  	if *filePath == "" {
    50  		log.Println("Please provide the path to the HTML file using the FILENAME flag.")
    51  		return
    52  	}
    53  
    54  	content, err := os.ReadFile(*filePath)
    55  	if err != nil {
    56  		log.Println("Error reading the file:", err)
    57  		return
    58  	}
    59  
    60  	updatedContent := removeLastDropdownItem(string(content))
    61  
    62  	err = os.WriteFile(*filePath, []byte(updatedContent), 0o644)
    63  	if err != nil {
    64  		log.Println("Error writing to the file:", err)
    65  		return
    66  	}
    67  
    68  	log.Println("Successfully updated the file.")
    69  }