/
theme.go
99 lines (84 loc) · 2.77 KB
/
theme.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
91
92
93
94
95
96
97
98
99
package theme
import (
"fmt"
"github.com/gocms-io/gcm/config"
"github.com/gocms-io/gcm/utility"
"github.com/gocms-io/gocms/utility/errors"
"github.com/urfave/cli"
"path/filepath"
)
const theme_name = "name"
const theme_name_short = "n"
const flag_hard = "delete"
const flag_hard_short = "d"
const flag_watch = "watch"
const flag_watch_short = "w"
const flag_ignore_files = "ignore"
const flag_ignore_files_short = "i"
var CMD_THEME = cli.Command{
Name: "theme",
Usage: "copy theme files from development directory into the gocms themes directory",
ArgsUsage: "<source> <gocms installation>",
Action: cmd_copy_theme,
Flags: []cli.Flag{
cli.BoolFlag{
Name: flag_hard + ", " + flag_hard_short,
Usage: "Delete the existing destination and replace with the contents of the source.",
},
cli.BoolFlag{
Name: flag_watch + ", " + flag_watch_short,
Usage: "Watch for file changes in source and copy to destination on change.",
},
cli.StringFlag{
Name: theme_name + ", " + theme_name_short,
Usage: "Name of the theme. *Required",
},
cli.StringSliceFlag{
Name: flag_ignore_files + ", " + flag_ignore_files_short,
Usage: "Files to ignore while watching. Multiple ignore flags can be given to ignore multiple files. Ignore files are regex capable. ex: .git*",
},
},
}
func cmd_copy_theme(c *cli.Context) error {
// verify there is a source and destination
if !c.Args().Present() {
err := "A source and destination directory must be specified."
fmt.Println(err)
return errors.New(err)
}
srcDir := c.Args().Get(0)
destDir := c.Args().Get(1)
if srcDir == "" || destDir == "" {
err := "A source and destination directory must be specified."
fmt.Println(err)
return errors.New(err)
}
if srcDir == "." || srcDir == "./" {
srcDir, _ = filepath.Abs(srcDir)
}
// verify that a plugin name is given
if c.String(theme_name) == "" {
err := "A plugin name must be specified with the --name or -n flag."
fmt.Println(err)
return errors.New(err)
}
// ignore files
// add default ignore files
ignorePath := []string{}
ignorePath = append(ignorePath, []string{".git", "docs", ".idea", ".*___", "node_modules"}...)
// add files to ignore
if c.StringSlice(flag_ignore_files) != nil {
ignorePath = append(ignorePath, c.StringSlice(flag_ignore_files)...)
}
themeName := c.String(theme_name)
themeDirPath := filepath.Join(destDir, config.CONTENT_DIR, config.THEMES_DIR, themeName)
err := utility.Copy(filepath.Clean(srcDir), themeDirPath, c.Bool(flag_hard), c.GlobalBool(config.FLAG_VERBOSE), ignorePath...)
if err != nil {
fmt.Printf("Error copying theme dir: %v\n", err.Error())
return err
}
if c.Bool(flag_watch) {
utility.WatchFilesForCarbonCopy(srcDir, themeDirPath, c.GlobalBool(config.FLAG_VERBOSE), ignorePath...)
}
return nil
}