Created
December 4, 2023 21:20
-
-
Save GZGavinZhao/8fd445a8bc1d651f9008d15e43c5820a to your computer and use it in GitHub Desktop.
Bump release number of a package.yml file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"bytes" | |
"fmt" | |
"os" | |
"strconv" | |
"gopkg.in/yaml.v3" | |
) | |
type PackageYML struct { | |
Name string `yaml:"name"` | |
Version yaml.Node `yaml:"version"` | |
Release yaml.Node `yaml:"release"` | |
// Component yaml.Node `yaml:"component"` | |
// Patterns yaml.Node `yaml:"patterns"` | |
// RunDeps yaml.Node `yaml:"rundeps"` | |
// BuildDeps []string `yaml:"builddeps"` | |
// CheckDeps []string `yaml:"checkdeps"` | |
// Environment string `yaml:"environment"` | |
// Setup string `yaml:"setup"` | |
// Build string `yaml:"build"` | |
// Install string `yaml:"install"` | |
// Networking bool `yaml:"networking"` | |
// Clang bool `yaml:"clang"` | |
} | |
func Load(path string) (pkg PackageYML, err error) { | |
raw, err := os.Open(path) | |
if err != nil { | |
return | |
} | |
defer raw.Close() | |
dec := yaml.NewDecoder(raw) | |
err = dec.Decode(&pkg) | |
return | |
} | |
func main() { | |
if len(os.Args) != 2 { | |
println("Usage: ./exe <package.yml>") | |
os.Exit(0) | |
} | |
filePath := os.Args[1] | |
pkg, err := Load(filePath) | |
if err != nil { | |
fmt.Printf("Failed to load file: %s\n", err) | |
os.Exit(1) | |
} | |
orig, err := os.Open(filePath) | |
if err != nil { | |
fmt.Printf("Failed to load file: %s\n", err) | |
os.Exit(1) | |
} | |
// defer orig.Close() | |
var buf bytes.Buffer | |
// output, err := os.Create("bumped.yml") | |
// if err != nil { | |
// fmt.Printf("Failed to create output file: %s\n", err) | |
// os.Exit(1) | |
// } | |
// defer output.Close() | |
scanner := bufio.NewScanner(orig) | |
line := 1 | |
for scanner.Scan() { | |
if line == pkg.Release.Line { | |
buf.WriteString("release : ") | |
relnum, err := strconv.Atoi(pkg.Release.Value) | |
if err != nil { | |
fmt.Printf("Failed to parse release num (\"%s\"): %s", pkg.Release.Value, err) | |
} | |
relnum++ | |
buf.WriteString(fmt.Sprintln(relnum)) | |
} else { | |
_, err := buf.Write(scanner.Bytes()) | |
buf.WriteString("\n") | |
if err != nil { | |
fmt.Printf("Failed to write line %d (content: \"%s\"): %s\n", line, scanner.Text(), err) | |
} | |
} | |
line++ | |
} | |
orig.Close() | |
// err = orig.Truncate(0) | |
orig, err = os.Create(filePath) | |
if err != nil { | |
fmt.Printf("Failed to truncate file: %s\n", err) | |
os.Exit(1) | |
} | |
defer orig.Close() | |
_, err = orig.Write(buf.Bytes()) | |
if err != nil { | |
fmt.Printf("Failed to write buffered output to file: %s\n", err) | |
os.Exit(1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment