Skip to content

Commit

Permalink
Rename project
Browse files Browse the repository at this point in the history
  • Loading branch information
kachick committed Sep 26, 2023
1 parent 6309e11 commit c510ff5
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ jobs:
with:
# Keep after `setup-go`, to use same go version
install-go: false
version: '2023.1.6' # Replace as { "regex": "\\d{4}[^']+", "command": "staticcheck --version | cut -d ' ' -f 2" }
version: '2023.1.6' # selfup { "regex": "\\d{4}[^']+", "script": "staticcheck --version | cut -d ' ' -f 2" }
2 changes: 1 addition & 1 deletion .github/workflows/dependency-review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ jobs:
# https://spdx.org/licenses/
allow-licenses: MIT, BSD-3-Clause, BSD-2-Clause, 0BSD, Unlicense, ISC, Apache-2.0, CC-BY-4.0
# The PURL prefix is got from following command
# `gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" /repos/kachick/self-replacer/dependency-graph/compare/87d736b80a8a254bcac003d97e0b84712176b04f...5ba877ff1048d5fedcc6758484623eb67737f0fb`
# `gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" /repos/kachick/selfup/dependency-graph/compare/87d736b80a8a254bcac003d97e0b84712176b04f...5ba877ff1048d5fedcc6758484623eb67737f0fb`
# Not found in https://github.com/package-url/packageurl-js
allow-dependencies-licenses: pkg:githubactions/DeterminateSystems/nix-installer-action@4
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ jobs:
- uses: actions/checkout@v4
- uses: dprint/check@v2.2
with:
dprint-version: '0.40.2' # Replace as { "regex": "\\d[^']+", "command": "dprint --version | cut -d ' ' -f 2" }
dprint-version: '0.40.2' # selfup { "regex": "\\d[^']+", "script": "dprint --version | cut -d ' ' -f 2" }

typos:
timeout-minutes: 15
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: crate-ci/typos@v1.16.11 # Replace as { "regex": "\\d\\.\\d+\\.\\d+", "command": "typos --version | cut -d ' ' -f 2" }
- uses: crate-ci/typos@v1.16.11 # selfup { "regex": "\\d\\.\\d+\\.\\d+", "script": "typos --version | cut -d ' ' -f 2" }
with:
files: |
.
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
if: startsWith(github.ref, 'refs/tags/')
with:
distribution: goreleaser
version: 'v1.20.0' # Replace as { "regex": "\\d[^']+", "command": "goreleaser --version | grep 'GitVersion:' | tr -s ' ' | cut -d ' ' -f 2" }
version: 'v1.20.0' # selfup { "regex": "\\d[^']+", "script": "goreleaser --version | grep 'GitVersion:' | tr -s ' ' | cut -d ' ' -f 2" }
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
go.work

# Binary when not used goreleaser
/self-replacer
/selfup

# goreleaser generates to
dist/
Expand Down
2 changes: 1 addition & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ before:
builds:
- env:
- CGO_ENABLED=0
main: ./cmd/self-replacer
main: ./cmd/selfup
goos:
- linux
# - windows
Expand Down
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ task: [test] go test
task: [lint] dprint check
task: [lint] go vet
PASS
ok self-replacer 0.313s
ok selfup 0.313s

> ./dist/self-replacer --version
self-replacer 0.1.1-next (906924b) # 2023-06-19T09:33:14Z
> ./dist/selfup --version
selfup 0.1.1-next (906924b) # 2023-06-19T09:33:14Z
```
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# self-replacer
# selfup

[![CI - Go Status](https://github.com/kachick/self-replacer/actions/workflows/ci-go.yml/badge.svg?branch=main)](https://github.com/kachick/self-replacer/actions/workflows/ci-go.yml?query=branch%3Amain+)
[![CI - Nix Status](https://github.com/kachick/self-replacer/actions/workflows/ci-nix.yml/badge.svg?branch=main)](https://github.com/kachick/self-replacer/actions/workflows/ci-nix.yml?query=branch%3Amain+)
[![CI - Go Status](https://github.com/kachick/selfup/actions/workflows/ci-go.yml/badge.svg?branch=main)](https://github.com/kachick/selfup/actions/workflows/ci-go.yml?query=branch%3Amain+)
[![CI - Nix Status](https://github.com/kachick/selfup/actions/workflows/ci-nix.yml/badge.svg?branch=main)](https://github.com/kachick/selfup/actions/workflows/ci-nix.yml?query=branch%3Amain+)

Replace strings in files if the line contains the scripts to output new string
Replace strings if the line contains the definition of how to update itself.

## Installation

In [Nix](https://nixos.org/), you can skip installation steps

```bash
nix run github:kachick/self-replacer -- --prefix='# Replace as ' .github/workflows/*.yml
nix run github:kachick/selfup -- --prefix='# selfup ' .github/workflows/*.yml
```

## Usage
Expand All @@ -20,28 +20,28 @@ Assume some GitHub actions workflow have lines like follows
```yaml
- uses: dprint/check@v2.2
with:
dprint-version: '0.40.2' # Replace as { "regex": "\\d[^']+", "command": "dprint --version | cut -d ' ' -f 2" }
dprint-version: '0.40.2' # selfup { "regex": "\\d[^']+", "script": "dprint --version | cut -d ' ' -f 2" }
```
Then we can call this tool as follows
```bash
self-replacer --prefix='# Replace as ' .github/workflows/*.yml
selfup --prefix='# selfup ' .github/workflows/*.yml
```

### JSON schema

| Field | Description |
| ------- | ----------------------------------------------------------------------------------------- |
| regex | [RE2](https://github.com/google/re2/wiki/Syntax), be careful to escape for JSON stringify |
| command | Bash scripts |
| Field | Description |
| ------ | ----------------------------------------------------------------------------------------- |
| regex | [RE2](https://github.com/google/re2/wiki/Syntax), be careful to escape for JSON stringify |
| script | Bash script |

### Mode

`--list-targets` option prints extracted targets without side-effect

```console
> self-replacer --list-targets --prefix='# Replace as ' .github/workflows/*.yml
> selfup --list-targets --prefix='# selfup ' .github/workflows/*.yml
.github/workflows/ci-go.yml:48: 2023.1.6
.github/workflows/lint.yml:17: 0.40.2
.github/workflows/lint.yml:24: 1.16.11
Expand Down
4 changes: 2 additions & 2 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ tasks:
- go install -ldflags "-X main.commit=$(git rev-parse HEAD)"
build:preview:
cmds:
- goreleaser build --snapshot --single-target --clean --output dist/self-replacer
- goreleaser build --snapshot --single-target --clean --output dist/selfup
build:
cmds:
- go build -o ./dist/self-replacer ./cmd/self-replacer
- go build -o ./dist/selfup ./cmd/selfup
test:
cmds:
- go test ./...
Expand Down
12 changes: 6 additions & 6 deletions cmd/self-replacer/main.go → cmd/selfup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"os"
"sync"

replacer "github.com/kachick/self-replacer"
updater "github.com/kachick/selfup"
"golang.org/x/xerrors"
)

Expand All @@ -24,10 +24,10 @@ func main() {
listFlag := flag.Bool("list-targets", false, "print target lines without actual replacing")
versionFlag := flag.Bool("version", false, "print the version of this program")

const usage = `Usage: self-replacer [OPTIONS] [PATH]...
const usage = `Usage: selfup [OPTIONS] [PATH]...
$ self-replacer --prefix='# Replace as ' .github/workflows/*.yml
$ self-replacer --prefix='# Replace as ' --list-targets .github/workflows/*.yml
$ selfup --prefix='# selfup ' .github/workflows/*.yml
$ selfup --prefix='# selfup ' --list-targets .github/workflows/*.yml
`

flag.Usage = func() {
Expand All @@ -40,7 +40,7 @@ $ self-replacer --prefix='# Replace as ' --list-targets .github/workflows/*.yml
if len(commit) >= 7 {
revision = commit[:7]
}
version := fmt.Sprintf("%s\n", "self-replacer"+" "+version+" "+"("+revision+")")
version := fmt.Sprintf("%s\n", "selfup"+" "+version+" "+"("+revision+")")

flag.Parse()
if *versionFlag {
Expand All @@ -61,7 +61,7 @@ $ self-replacer --prefix='# Replace as ' --list-targets .github/workflows/*.yml
wg.Add(1)
go func(path string) {
defer wg.Done()
newBody, isDirty, err := replacer.Replace(path, prefix, isListMode)
newBody, isDirty, err := updater.Update(path, prefix, isListMode)

if err != nil {
log.Fatalf("%+v", err)
Expand Down
12 changes: 6 additions & 6 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
replacerVersion =
updaterVersion =
if (self ? shortRev)
then self.shortRev
else "dev";
Expand All @@ -37,21 +37,21 @@
];
};

packages.self-replacer = pkgs.buildGo121Module {
pname = "self-replacer";
version = replacerVersion;
packages.selfup = pkgs.buildGo121Module {
pname = "selfup";
version = updaterVersion;
src = pkgs.lib.cleanSource self;

# When updating go.mod or go.sum, update this sha together
vendorSha256 = "sha256-ot3JjQ49kLVG+l1EyLaxonDfgxTyFTmli3B8YDIjhyY=";
};

packages.default = packages.self-replacer;
packages.default = packages.selfup;

# `nix run`
apps.default = {
type = "app";
program = "${packages.self-replacer}/bin/self-replacer";
program = "${packages.selfup}/bin/selfup";
};
}
);
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/kachick/self-replacer
module github.com/kachick/selfup

go 1.21

Expand Down
12 changes: 6 additions & 6 deletions replacer.go → selfup.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package replacer
package updater

import (
"bufio"
Expand All @@ -14,12 +14,12 @@ import (
)

type Definition struct {
Regex string
Command string
Regex string
Script string
}

// Returns new body and true if it is changed
func Replace(path string, prefix string, isListMode bool) (string, bool, error) {
func Update(path string, prefix string, isListMode bool) (string, bool, error) {
newLines := []string{}
isChanged := false

Expand Down Expand Up @@ -50,9 +50,9 @@ func Replace(path string, prefix string, isListMode bool) (string, bool, error)
fmt.Printf("%s:%d: %s\n", path, lineNumber, re.FindString(head))
continue
}
out, err := exec.Command("bash", "-c", definition.Command).Output()
out, err := exec.Command("bash", "-c", definition.Script).Output()
if err != nil {
return "", false, xerrors.Errorf("%s:%d: Executing %s with bash has been failed: %w", path, lineNumber, definition.Command, err)
return "", false, xerrors.Errorf("%s:%d: Executing %s with bash has been failed: %w", path, lineNumber, definition.Script, err)
}
replaced := re.ReplaceAllString(head, strings.TrimSuffix(string(out), "\n"))
if !isChanged {
Expand Down

0 comments on commit c510ff5

Please sign in to comment.