Commit initial
This commit is contained in:
commit
132017b63d
|
@ -0,0 +1 @@
|
||||||
|
go-rename
|
|
@ -0,0 +1,11 @@
|
||||||
|
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
||||||
|
Version 2, December 2004
|
||||||
|
|
||||||
|
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
|
||||||
|
|
||||||
|
Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed.
|
||||||
|
|
||||||
|
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
||||||
|
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||||
|
|
||||||
|
0. You just DO WHAT THE FUCK YOU WANT TO.
|
|
@ -0,0 +1,3 @@
|
||||||
|
# go-rename
|
||||||
|
|
||||||
|
go-rename est un utilitaire permettant de renommer des fichiers en masse en utilisant une expression régulière.
|
|
@ -0,0 +1,8 @@
|
||||||
|
module gitea.zaclys.com/bvaudour/go-rename
|
||||||
|
|
||||||
|
go 1.22.0
|
||||||
|
|
||||||
|
require (
|
||||||
|
gitea.zaclys.com/bvaudour/gob v0.0.0-20240225133050-ec6a3ba492cc // indirect
|
||||||
|
github.com/rwtodd/Go.Sed v0.0.0-20230610052213-ba3e9c186f0a // indirect
|
||||||
|
)
|
|
@ -0,0 +1,4 @@
|
||||||
|
gitea.zaclys.com/bvaudour/gob v0.0.0-20240225133050-ec6a3ba492cc h1:taHLOnrgyIlGYSs9cu+FXChAYtIeHoJg2D8to+UDgoA=
|
||||||
|
gitea.zaclys.com/bvaudour/gob v0.0.0-20240225133050-ec6a3ba492cc/go.mod h1:Gw6x0KNKoXv6AMtRkaI+iWM2enVzwHikUSskuEzWQz4=
|
||||||
|
github.com/rwtodd/Go.Sed v0.0.0-20230610052213-ba3e9c186f0a h1:URwYffGNuBQkfwkcn+1CZhb8IE/mKSXxPXp/zzQsn80=
|
||||||
|
github.com/rwtodd/Go.Sed v0.0.0-20230610052213-ba3e9c186f0a/go.mod h1:c6qgHcSUeSISur4+Kcf3WYTvpL07S8eAsoP40hDiQ1I=
|
|
@ -0,0 +1,80 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"gitea.zaclys.com/bvaudour/gob/format"
|
||||||
|
"gitea.zaclys.com/bvaudour/gob/shell/console/read"
|
||||||
|
"github.com/rwtodd/Go.Sed/sed"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Vérifie si un fichier/dossier existe.
|
||||||
|
func exists(path string) bool {
|
||||||
|
_, err := os.Stat(path)
|
||||||
|
return err == nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Imprime l’aide.
|
||||||
|
func help() {
|
||||||
|
fmt.Println(format.Apply("Usage :", "l_yellow"), os.Args[0], "<regexp> <file> [<other_files>...]")
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// On vérifie d’abord que la commande contient bien une regexp et au moins un fichier.
|
||||||
|
if len(os.Args) <= 2 {
|
||||||
|
fmt.Fprintln(os.Stderr, format.Apply("Veuillez saisir au moins 2 arguments", "l_red"))
|
||||||
|
help()
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Le premier argument spécifie la regexp. Celle-ci doit être valide (ie. sous la même forme que celle utilisée pour sed).
|
||||||
|
engine, err := sed.New(strings.NewReader(os.Args[1]))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, format.Apply(err.Error(), "l_red"))
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// On regarde les fichiers à renommer, on vérifie qu’ils existent et qu’on peut les renommer selon la regexp.
|
||||||
|
var replaces [][]string
|
||||||
|
n := 0
|
||||||
|
for _, e1 := range os.Args[2:] {
|
||||||
|
e1 = strings.TrimSpace(e1)
|
||||||
|
if !exists(e1) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if e2, err := engine.RunString(e1); err == nil {
|
||||||
|
e2 = strings.TrimSpace(e2)
|
||||||
|
if e1 == e2 || len(e2) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
replaces = append(replaces, []string{e1, e2})
|
||||||
|
if l := len(e1); l > n {
|
||||||
|
n = l
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Si aucun fichier à remplacer, on affiche un message et on sort.
|
||||||
|
if len(replaces) == 0 {
|
||||||
|
fmt.Fprintln(os.Stderr, format.Apply("Aucun fichier à remplacer", "yellow"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sinon, on affiche les renommages qui seront effectués.
|
||||||
|
for _, r := range replaces {
|
||||||
|
fmt.Printf("%s → %s\n", format.Left(r[0], n), r[1])
|
||||||
|
}
|
||||||
|
|
||||||
|
// On demande une confirmation explicite avant renommage effectif.
|
||||||
|
if !read.QuestionBool("Remplacer ?", true) {
|
||||||
|
fmt.Fprintln(os.Stderr, format.Apply("Opération annulée", "yellow"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Renommage.
|
||||||
|
for _, r := range replaces {
|
||||||
|
os.Rename(r[0], r[1])
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue