Skip to content

Commit

Permalink
feat(cmd/scripts/rename/spaces_and_commas): Add command which renames…
Browse files Browse the repository at this point in the history
… Google Docs exports to better filenames suitable for URLs
  • Loading branch information
ondrejsika committed Apr 25, 2023
1 parent fac482f commit 9912a96
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ import (
_ "github.com/sikalabs/slu/cmd/scripts/kubernetes/install_maildev"
_ "github.com/sikalabs/slu/cmd/scripts/kubernetes/install_metrics_server"
_ "github.com/sikalabs/slu/cmd/scripts/kubernetes/install_prometheus_operator_crd"
_ "github.com/sikalabs/slu/cmd/scripts/rename"
_ "github.com/sikalabs/slu/cmd/scripts/rename/spaces_and_commas"
_ "github.com/sikalabs/slu/cmd/scripts/run_tcp_proxy_in_docker"
_ "github.com/sikalabs/slu/cmd/serve_files"
_ "github.com/sikalabs/slu/cmd/shell_scripts"
Expand Down
15 changes: 15 additions & 0 deletions cmd/scripts/rename/rename.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package rename

import (
parent_cmd "github.com/sikalabs/slu/cmd/scripts"
"github.com/spf13/cobra"
)

var Cmd = &cobra.Command{
Use: "rename",
Short: "Rename files",
}

func init() {
parent_cmd.Cmd.AddCommand(Cmd)
}
60 changes: 60 additions & 0 deletions cmd/scripts/rename/spaces_and_commas/spaces_and_commas.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package spaces_and_commas

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

parent_cmd "github.com/sikalabs/slu/cmd/scripts/rename"
"github.com/spf13/cobra"
)

var FlagDry bool
var FlagVersion string

var Cmd = &cobra.Command{
Use: "spaces-and-commas",
Short: "Replace spaces and commans in filenames with underscores",
Aliases: []string{"sac"},
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
replaceSpacesAndCommas()
},
}

func init() {
parent_cmd.Cmd.AddCommand(Cmd)
}

func replaceSpacesAndCommas() {
dir := "."

files, err := ioutil.ReadDir(dir)
if err != nil {
fmt.Println("Error reading directory:", err)
return
}

for _, file := range files {
if !file.IsDir() {
oldName := file.Name()
newName := strings.ReplaceAll(oldName, " - ", "__")
newName = strings.ReplaceAll(newName, ",", "_")
newName = strings.ReplaceAll(newName, " ", "_")

if oldName != newName {
oldPath := filepath.Join(dir, oldName)
newPath := filepath.Join(dir, newName)

err = os.Rename(oldPath, newPath)
if err != nil {
fmt.Printf("Error renaming file '%s' to '%s': %s\n", oldName, newName, err)
} else {
fmt.Printf("Renamed '%s' to '%s'\n", oldName, newName)
}
}
}
}
}

0 comments on commit 9912a96

Please sign in to comment.