-
Notifications
You must be signed in to change notification settings - Fork 2
/
update.go
143 lines (123 loc) · 4.01 KB
/
update.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package update
import (
"fmt"
"github.com/gocms-io/gcm/commands/install"
"github.com/gocms-io/gcm/config"
"github.com/gocms-io/gcm/utility"
"github.com/urfave/cli"
"os"
"path/filepath"
)
var CMD_UPDATE = cli.Command{
Name: "update",
Usage: "Update gocms - The .env file will be preserved. Updates apply to the current working directory.",
ArgsUsage: "<directory>",
Action: cmd_update,
}
type updatePluginContext struct {
backupDir string
stagingDir string
installDir string
versionToUse string
verbose bool
}
func cmd_update(c *cli.Context) error {
installDir, _ := filepath.Abs(".")
// verify this is a gocms dir
if _, err := os.Stat(filepath.Join(installDir, config.BINARY_FILE)); os.IsNotExist(err) {
fmt.Println("The provided directory doesn't appear to be an active GoCMS installation.")
return nil
}
versionToUse := config.BINARY_DEFAULT_VERSION
if c.GlobalString(config.FLAG_SET_VERSION) != "" {
versionToUse = c.GlobalString(config.FLAG_SET_VERSION)
}
uctx := updatePluginContext{
backupDir: filepath.Join(installDir, config.BACKUP_DIR),
stagingDir: filepath.Join(installDir, config.STAGING_DIR),
installDir: installDir,
verbose: c.GlobalBool(config.FLAG_VERBOSE),
versionToUse: versionToUse,
}
// copy current install to backup
err := utility.Copy(uctx.installDir, uctx.backupDir, true, uctx.verbose, "\\.bk", ".bk.*")
if err != nil {
return nil
}
// create staging dir
err = os.Mkdir(uctx.stagingDir, os.ModePerm)
if err != nil {
fmt.Printf("Error creating staging directory %v: %v\n", uctx.stagingDir, err.Error())
return nil
}
// do basic install and rollback on error
err = install.BasicInstall(uctx.stagingDir, uctx.versionToUse)
if err != nil {
// roll back update
fmt.Print("Rolling back changes...\n")
os.RemoveAll(uctx.backupDir)
os.RemoveAll(uctx.stagingDir)
return nil
}
// merge backup and staging into installation dir
fmt.Print("Applying update to staging...\n")
// .env
err = os.Rename(filepath.Join(uctx.backupDir, config.ENV_FILE), filepath.Join(uctx.stagingDir, config.ENV_FILE))
if err != nil {
fmt.Printf("Error applying .env file: %v\n", err.Error())
uctx.rollback()
return nil
}
// plugins
err = utility.ForceRename(filepath.Join(uctx.backupDir, config.CONTENT_DIR, config.PLUGINS_DIR), filepath.Join(uctx.stagingDir, config.CONTENT_DIR, config.PLUGINS_DIR))
if err != nil {
fmt.Printf("Error applying plugins file: %v\n", err.Error())
uctx.rollback()
return nil
}
// docs
// remove these default dir first
_ = os.RemoveAll(filepath.Join(uctx.backupDir, config.CONTENT_DIR, config.THEMES_DIR, config.THEMES_DEFAULT_DIR))
err = utility.ForceRename(filepath.Join(uctx.backupDir, config.CONTENT_DIR, config.THEMES_DIR), filepath.Join(uctx.stagingDir, config.CONTENT_DIR, config.THEMES_DIR))
if err != nil {
fmt.Printf("Error applying themes file: %v\n", err.Error())
uctx.rollback()
return nil
}
// move everything into production
fmt.Printf("Moving staging into production\n")
err = utility.Copy(uctx.stagingDir, uctx.installDir, false, uctx.verbose)
if err != nil {
fmt.Printf("Erorr moving staging into production: %v\n", err.Error())
uctx.rollback()
return nil
}
// clean up
fmt.Println("Cleaning up temp files")
err = os.RemoveAll(uctx.backupDir)
if err != nil {
fmt.Printf("Error removing backup: %v\n", err.Error())
}
err = os.RemoveAll(uctx.stagingDir)
if err != nil {
fmt.Printf("Error removing staging: %v\n", err.Error())
}
fmt.Println("GoCMS Installed Updated!\n")
return nil
}
func (uctx *updatePluginContext) rollback() {
fmt.Print("Rolling back changes...\n")
err := utility.Copy(uctx.backupDir, uctx.installDir, false, uctx.verbose)
if err != nil {
fmt.Printf("Error moving backup into production: %v\n", err.Error())
}
err = os.RemoveAll(uctx.backupDir)
if err != nil {
fmt.Printf("Error removing backup: %v\n", err.Error())
}
err = os.RemoveAll(uctx.stagingDir)
if err != nil {
fmt.Printf("Error removing staging: %v\n", err.Error())
}
fmt.Print("Complete!\n")
}