Skip to content

Commit

Permalink
Implement subcommand to migrate JSON (#173)
Browse files Browse the repository at this point in the history
* Add migration mode

* Add example for old schema

* Keep indent

* Revert "Keep indent"

This reverts commit 22d185a.

* Do not prior on migrate future
  • Loading branch information
kachick committed Apr 24, 2024
1 parent c31b6f1 commit 5fdc2d2
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
17 changes: 17 additions & 0 deletions cmd/selfup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sync"

"github.com/fatih/color"
"github.com/kachick/selfup/internal/migrate"
"github.com/kachick/selfup/internal/runner"
"golang.org/x/term"
"golang.org/x/xerrors"
Expand Down Expand Up @@ -41,6 +42,7 @@ func main() {
$ selfup run .github/workflows/*.yml
$ selfup run --prefix='# Update with this json: ' --skip-by='nix run' .github/workflows/*.yml
$ selfup list .github/workflows/*.yml
$ selfup migrate .github/workflows/have_beta_schema.yml
$ selfup --version
`

Expand Down Expand Up @@ -72,6 +74,21 @@ $ selfup --version
subCommand := os.Args[1]
isListMode := subCommand == "list"
isRunMode := subCommand == "run"
isMigrateMode := subCommand == "migrate"
if isMigrateMode {
paths := os.Args[2:]
for _, path := range paths {
isMigrated, err := migrate.Migrate(path)
if err != nil {
log.Fatalf("%+v", err)
}
if isMigrated {
log.Println(path + ": migrated schema beta -> v1")
}
}

return
}

if !(isListMode || isRunMode) {
flag.Usage()
Expand Down
9 changes: 9 additions & 0 deletions examples/beta_schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
jobs:
dprint:
timeout-minutes: 15
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
- uses: dprint/check@2f1cf31537886c3bfb05591c031f7744e48ba8a1 # v2.2
with:
dprint-version: '0.45.0' # selfup { "regex": "\\d[^']+", "script": "dprint --version | cut -d ' ' -f 2" }
77 changes: 77 additions & 0 deletions internal/migrate/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package migrate

import (
"bufio"
"encoding/json"
"io/fs"
"os"
"strings"
)

type V1Schema struct {
Extract string `json:"extract"`
Command []string `json:"replacer"`
Nth int `json:"nth,omitempty"`
Delimiter string `json:"delimiter,omitempty"`
}

type BetaSchema struct {
Regex string `json:"regex"`
Script string `json:"script"`
}

const defaultPrefix string = "# selfup "

func Migrate(path string) (bool, error) {
newLines := []string{}
isMigrated := false
bytes, err := os.ReadFile(path)
if err != nil {
return false, err
}
scanner := bufio.NewScanner(strings.NewReader(string(bytes)))
for scanner.Scan() {
line := scanner.Text()
head, tail, found := strings.Cut(line, defaultPrefix)
if !found {
newLines = append(newLines, line)
continue
}

beta := &BetaSchema{}
err := json.Unmarshal([]byte(tail), beta)
if err != nil {
return false, err
}
if beta.Regex == "" || beta.Script == "" {
newLines = append(newLines, line)
continue
}
v1 := V1Schema{
Extract: beta.Regex,
Command: []string{"bash", "-c", beta.Script},
Nth: 0,
Delimiter: "",
}
migrated, err := json.Marshal(v1)
if err != nil {
return false, err
}
newLines = append(newLines, head+defaultPrefix+string(migrated))
if !isMigrated {
isMigrated = true
}
}
if scanner.Err() != nil {
return false, err
}

if isMigrated {
err = os.WriteFile(path, []byte(strings.Join(newLines, "\n")), fs.ModePerm)
if err != nil {
return true, err
}
}

return isMigrated, nil
}

0 comments on commit 5fdc2d2

Please sign in to comment.