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

Commit

Permalink
Extract "detect" as subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
kachick committed Jun 22, 2023
1 parent cfa3d8d commit e73968b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Install nix parser
run: curl -L https://github.com/kachick/nix-headbump/releases/latest/download/nix-headbump_Linux_x86_64.tar.gz | tar xvz -C ./ nix-headbump
run: curl -L https://github.com/kachick/nix-headbump/releases/download/v0.1.2/nix-headbump_Linux_x86_64.tar.gz | tar xvz -C ./ nix-headbump
- id: current
run: |
current="$(./nix-headbump -current)"
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ When I want to bump it, I always visit the nixpkgs repository and copy and paste
```console
> curl -L https://github.com/kachick/nix-headbump/releases/latest/download/nix-headbump_Linux_x86_64.tar.gz | tar xvz -C ./ nix-headbump
> ./nix-headbump --version
nix-headbump 0.1.0 (d8e9da7) # 2023-06-19T08:55:33Z
nix-headbump 0.2.0 (70f68fa) # 2023-06-22T09:58:05Z
```

## Usage

Providing two subcommands. I'm using `detect` in CI and `bump` in local.

```console
> nix-headbump && git commit -m 'Bump nixpkgs to latest' *.nix
> nix-headbump detect --current
e57b65abbbf7a2d5786acc86fdf56cde060ed026

> nix-headbump bump && git commit -m 'Bump nixpkgs to latest' *.nix
[main 213d1bf] Bump nixpkgs to latest
1 file changed, 1 insertion(+), 1 deletion(-)
```
Expand Down
79 changes: 54 additions & 25 deletions nix-headbump.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
Expand All @@ -21,49 +22,77 @@ var (
)

func main() {
usageError := errors.New("expected 'bump' or 'detect' subcommands")

detectCmd := flag.NewFlagSet("detect", flag.ExitOnError)
bumpCmd := flag.NewFlagSet("bump", flag.ExitOnError)
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")
printTargetFlag := flag.Bool("print-target", false, "detect and print which file will be bumped")
currentFlag := detectCmd.Bool("current", false, "print current nixpath without bumping")
lastFlag := detectCmd.Bool("last", false, "print git head ref without bumping")
target := detectCmd.Bool("target", false, "print which file will be bumped")
flag.Parse()

if *versionFlag {
revision := commit[:7]
fmt.Printf("%s\n", "nix-headbump"+" "+version+" "+"("+revision+") # "+date)
return
}

if len(os.Args) < 2 {
fmt.Println(usageError.Error())
os.Exit(1)
}

path, err := getTargetPath()
if err != nil {
log.Fatalf("Failed to get target files: %s", err.Error())
}

if path == "" {
log.Fatalln("Both default.nix and shell.nix are not found")
}

if *printTargetFlag {
fmt.Println(path)
return
}
if *currentFlag {
current, err := getCurrentVersion(path)
switch os.Args[1] {
case "detect":
err := detectCmd.Parse(os.Args[2:])
if err != nil {
log.Fatalf("Getting the current version has been failed: %s", err.Error())
fmt.Println(usageError.Error())
}
fmt.Println(current)
return
}
last, err := getLastVersion()
if err != nil {
log.Fatalf("Getting the last version has been failed: %s", err.Error())
}
if *lastFlag {
fmt.Println(last)
return
}
if err = bump(path, last); err != nil {
log.Fatalf("Bumping the version has been failed: %s", err.Error())
if *target {
fmt.Println(path)
return
}
if *currentFlag {
current, err := getCurrentVersion(path)
if err != nil {
log.Fatalf("Getting the current version has been failed: %s", err.Error())
}
fmt.Println(current)
return
}
last, err := getLastVersion()
if err != nil {
log.Fatalf("Getting the last version has been failed: %s", err.Error())
}
if *lastFlag {
fmt.Println(last)
return
}

detectCmd.PrintDefaults()
case "bump":
err := bumpCmd.Parse(os.Args[2:])
if err != nil {
fmt.Println(usageError.Error())
}
last, err := getLastVersion()
if err != nil {
log.Fatalf("Getting the last version has been failed: %s", err.Error())
}
if err = bump(path, last); err != nil {
log.Fatalf("Bumping the version has been failed: %s", err.Error())
}
default:
fmt.Println(usageError.Error())
os.Exit(1)
}
}

Expand Down

0 comments on commit e73968b

Please sign in to comment.