-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement subcommand to migrate JSON (#173)
* 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
Showing
3 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
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
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
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" } |
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
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 | ||
} |