agones.dev/agones@v1.53.0/build/scripts/bump-image/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 increment the new tag for the given examples image. Run this script using `make bump-image IMAGENAME=<imageName> VERSION=<current-version>`
    16  package main
    17  
    18  import (
    19  	"flag"
    20  	"log"
    21  	"os"
    22  	"path/filepath"
    23  	"regexp"
    24  	"strconv"
    25  	"strings"
    26  )
    27  
    28  var (
    29  	imageName       string
    30  	version         string
    31  	versionPattern  *regexp.Regexp
    32  	targetedFolders = map[string]bool{
    33  		"build":    true,
    34  		"examples": true,
    35  		"install":  true,
    36  		"pkg":      true,
    37  		"site":     true,
    38  		"test":     true,
    39  	}
    40  	versionPatternInMakefile *regexp.Regexp
    41  )
    42  
    43  func init() {
    44  	flag.StringVar(&imageName, "imageName", "", "Image name to update")
    45  	flag.StringVar(&version, "version", "", "Version to update")
    46  	versionPatternInMakefile = regexp.MustCompile(`version\s*:=\s*\d+\.\d+`)
    47  }
    48  
    49  func imageNamePrefix(imageName string) string {
    50  	// Exceptions list
    51  	exceptions := map[string]bool{
    52  		"simple-genai-game-server": true,
    53  		"simple-game-server":       true,
    54  	}
    55  
    56  	// Use the first two words for exceptions, otherwise use the first word
    57  	separator := "-"
    58  	if exceptions[imageName] {
    59  		return firstTwoWords(imageName, separator)
    60  	}
    61  
    62  	parts := strings.Split(imageName, separator)
    63  	return parts[0] // Only use the first word for non-exceptions
    64  }
    65  
    66  func firstTwoWords(s, separator string) string {
    67  	parts := strings.Split(s, separator)
    68  	if len(parts) >= 2 {
    69  		return parts[0] + separator + parts[1]
    70  	}
    71  	return parts[0]
    72  }
    73  
    74  func main() {
    75  	flag.Parse()
    76  
    77  	if imageName == "" || version == "" {
    78  		log.Fatal("Provide both an image name and a version using the flags.")
    79  	}
    80  
    81  	versionPatternString := imageName + `:(\d+)\.(\d+)`
    82  	versionPattern = regexp.MustCompile(versionPatternString)
    83  	newVersion := incrementVersion(version)
    84  
    85  	baseDirectory := "."
    86  	for folder := range targetedFolders {
    87  		directory := filepath.Join(baseDirectory, folder)
    88  
    89  		err := filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
    90  			if err != nil {
    91  				return err
    92  			}
    93  			if !info.IsDir() && filepath.Ext(path) != ".md" {
    94  				// Update the version in Makefiles located within the directories that are relevant to the given imageName.
    95  				if folder == "examples" && strings.HasPrefix(filepath.Base(filepath.Dir(path)), imageNamePrefix(imageName)) && filepath.Base(path) == "Makefile" {
    96  					err = updateMakefileVersion(path, newVersion)
    97  				} else {
    98  					err = updateFileVersion(path, newVersion)
    99  				}
   100  				if err != nil {
   101  					log.Printf("Error updating file %s: %v", path, err)
   102  				}
   103  			}
   104  			return nil
   105  		})
   106  
   107  		if err != nil {
   108  			log.Fatalf("Error processing directory %s: %v", directory, err)
   109  		}
   110  	}
   111  }
   112  
   113  func incrementVersion(version string) string {
   114  	parts := strings.Split(version, ".")
   115  	if len(parts) != 2 {
   116  		log.Fatalf("Invalid version format: %s", version)
   117  	}
   118  
   119  	minor, err := strconv.Atoi(parts[1])
   120  	if err != nil {
   121  		log.Fatalf("Invalid version number: %v", err)
   122  	}
   123  
   124  	minor++
   125  	return parts[0] + "." + strconv.Itoa(minor)
   126  }
   127  
   128  func updateFileVersion(filePath, newVersion string) error {
   129  	input, err := os.ReadFile(filePath)
   130  	if err != nil {
   131  		return err
   132  	}
   133  
   134  	content := versionPattern.ReplaceAllString(string(input), imageName+":"+newVersion)
   135  
   136  	return os.WriteFile(filePath, []byte(content), 0o644)
   137  }
   138  
   139  func updateMakefileVersion(filePath, newVersion string) error {
   140  	input, err := os.ReadFile(filePath)
   141  	if err != nil {
   142  		return err
   143  	}
   144  
   145  	content := versionPatternInMakefile.ReplaceAllString(string(input), "version := "+newVersion)
   146  
   147  	return os.WriteFile(filePath, []byte(content), 0o644)
   148  }