Skip to content
This repository has been archived by the owner on Aug 6, 2023. It is now read-only.

Commit

Permalink
Implement get last version only option
Browse files Browse the repository at this point in the history
  • Loading branch information
kachick committed Jun 19, 2023
1 parent 449a126 commit 17c9668
Showing 1 changed file with 41 additions and 28 deletions.
69 changes: 41 additions & 28 deletions nix-headbump.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ import (

const version string = "0.1.0"

var re = regexp.MustCompile(`(?s)(import\s+\(fetchTarball\s+"https://github.com/NixOS/nixpkgs/archive/)([^"]+?)(\.tar\.gz"\))`)

var revision string

func main() {
versionFlag := flag.Bool("version", false, "print the version of this program")
currentFlag := flag.Bool("current", false, "print current nixpath without bumping")
lastFlag := flag.Bool("last", false, "print git head ref without bumping")
flag.Parse()

if *versionFlag {
Expand All @@ -44,7 +47,15 @@ func main() {
fmt.Println(current)
return
}
err := bump(path)
last, err := getLastVersion()
if err != nil {
log.Fatalf("Getting the last version has been failed: %s", err.Error())
}
if *lastFlag {
fmt.Println(last)
return
}
err = bump(path, last)
if err != nil {
log.Fatalf("Bumping the version has been failed: %s", err.Error())
}
Expand All @@ -53,51 +64,53 @@ func main() {
}
}

type Commit struct {
Sha string `json:"sha"`
}
func bump(path string, last string) error {
origin, err := os.ReadFile(path)
if err != nil {
return err
}
replaced := re.ReplaceAll(origin, []byte("${1}"+last+"${3}"))
if bytes.Equal(origin, replaced) {
return nil
}

type Response struct {
Commit Commit `json:"commit"`
return os.WriteFile(path, replaced, os.ModePerm)
}

var re = regexp.MustCompile(`(?s)(import\s+\(fetchTarball\s+"https://github.com/NixOS/nixpkgs/archive/)([^"]+?)(\.tar\.gz"\))`)

func bump(path string) error {
func getCurrentVersion(path string) (string, error) {
origin, err := os.ReadFile(path)
if err != nil {
return err
return "", err
}
matches := re.FindStringSubmatch(string(origin))
return matches[2], nil
}

func getLastVersion() (string, error) {
type commit struct {
Sha string `json:"sha"`
}

type response struct {
Commit commit `json:"commit"`
}

req, _ := http.NewRequest("GET", "https://api.github.com/repos/NixOS/nixpkgs/branches/master", nil)
req.Header.Set("Accept", "application/vnd.github+json")
req.Header.Set("X-GitHub-Api-Version", "2022-11-28")
client := new(http.Client)
res, err := client.Do(req)
if err != nil {
return err
return "", err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return err
return "", err
}
jsonRes := &Response{}
jsonRes := &response{}
if json.Unmarshal(body, jsonRes) != nil {
return err
}
replaced := re.ReplaceAll(origin, []byte("${1}"+jsonRes.Commit.Sha+"${3}"))
if bytes.Equal(origin, replaced) {
return nil
}

return os.WriteFile(path, replaced, os.ModePerm)
}

func getCurrentVersion(path string) (string, error) {
origin, err := os.ReadFile(path)
if err != nil {
return "", err
}
matches := re.FindStringSubmatch(string(origin))
return matches[2], nil
return jsonRes.Commit.Sha, nil
}

0 comments on commit 17c9668

Please sign in to comment.