agones.dev/agones@v1.54.0/build/scripts/remove-data-proofer-ignore/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 to remove data-proofer-ignore attribute from previous release blog
    16  package main
    17  
    18  import (
    19  	"flag"
    20  	"log"
    21  	"os"
    22  	"strings"
    23  )
    24  
    25  func main() {
    26  	// Define a flag to accept the file name
    27  	fileName := flag.String("file", "", "Path to the file")
    28  	flag.Parse()
    29  
    30  	if *fileName == "" {
    31  		log.Println("Please provide the file name using the -file flag")
    32  	}
    33  
    34  	filePath := "site/content/en/blog/releases/" + *fileName
    35  
    36  	file, err := os.OpenFile(filePath, os.O_RDWR, 0o644)
    37  	if err != nil {
    38  		log.Println(err)
    39  	}
    40  	defer func() {
    41  		if cerr := file.Close(); cerr != nil {
    42  			log.Println(cerr)
    43  		}
    44  	}()
    45  
    46  	// Read the file content
    47  	stat, err := file.Stat()
    48  	if err != nil {
    49  		log.Println(err)
    50  		return
    51  	}
    52  	content := make([]byte, stat.Size())
    53  	_, err = file.Read(content)
    54  	if err != nil {
    55  		log.Println(err)
    56  		return
    57  	}
    58  
    59  	contentStr := string(content)
    60  
    61  	// Remove the "data-proofer-ignore" word from the content
    62  	modifiedContent := strings.ReplaceAll(contentStr, "data-proofer-ignore", "")
    63  
    64  	// Truncate the file before writing the modified content
    65  	err = file.Truncate(0)
    66  	if err != nil {
    67  		log.Println(err)
    68  		return
    69  	}
    70  
    71  	// Move the file offset to the beginning
    72  	_, err = file.Seek(0, 0)
    73  	if err != nil {
    74  		log.Println(err)
    75  		return
    76  	}
    77  
    78  	// Write the modified content back to the file
    79  	_, err = file.WriteString(modifiedContent)
    80  	if err != nil {
    81  		log.Println(err)
    82  		return
    83  	}
    84  
    85  	log.Println("File successfully modified:", filePath)
    86  }