/
charset.go
executable file
·90 lines (76 loc) · 2.46 KB
/
charset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package cmd
import (
"fmt"
"strings"
"gopkg.in/urfave/cli.v2"
"github.com/rodkranz/char7/modules/charset"
"github.com/rodkranz/char7/modules/files"
"github.com/rodkranz/char7/modules/settings"
)
// CharSet is the main command for application
// define information about the program as parameters
var CharSet = &cli.Command{
Name: "charset",
Usage: "Change charset to html cod",
Description: `change key in map to value`,
Action: runCharSet,
Flags: []cli.Flag{
stringFlag("ext, e", strings.Join(settings.ExtFile, ","), "Define the extension type to filter files"),
stringFlag("file, f", settings.FileName, "Define the file name that needs to change."),
stringFlag("dir, d", settings.Dir, "Define the folder that will looking for files."),
boolFlag("backup, b", "Disable backup file when change charset"),
stringFlag("backupName, bn", settings.BackupName, "Define the file name that needs to change."),
},
}
// runCharSet is the funcation that make the magic
// find files with filter defined by user and resturn the files.
func runCharSet(ctx *cli.Context) error {
// Parse common information
parseFlags(ctx)
// split extensions
if ctx.IsSet("ext") {
settings.ExtFile = strings.Split(ctx.String("ext"), ",")
}
// filter to find files
optFilter := &files.Filter{
FileName: settings.FileName,
Exts: settings.ExtFile,
Dir: settings.Dir,
}
// search files using the filter defined by user
list, err := files.SearchFiles(optFilter)
if err != nil {
return err
}
var total int
for _, path := range list {
bkpPath := path + settings.BackupName
if !ctx.IsSet("backup") {
fmt.Fprintf(ctx.App.Writer, "Copyng %s to %s... ", path, bkpPath)
if e := files.Copy(path, bkpPath); e != nil {
fmt.Fprintln(ctx.App.Writer, "[FAIL]")
continue
}
fmt.Fprintln(ctx.App.Writer, "[SUCCESS]")
}
fmt.Fprintf(ctx.App.Writer, "Finding chars to convert from %s.. ", path)
if e := charset.CharSet(path); e != nil {
fmt.Fprintln(ctx.App.Writer, "[FAIL]")
continue
}
fmt.Fprintln(ctx.App.Writer, "[SUCCESS]")
if !charset.HasChange {
total++
if !ctx.IsSet("backup") {
fmt.Fprintf(ctx.App.Writer, "Deleting useless file %s.. ", path)
if err := files.Delete(bkpPath); err != nil {
fmt.Fprintln(ctx.App.Writer, "[FAIL]")
continue
}
fmt.Fprintln(ctx.App.Writer, "[SUCCESS]")
}
}
}
fmt.Fprintf(ctx.App.Writer, "Found %d files, changed %d files.\n", len(list), total)
return nil
}