-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
97 lines (83 loc) · 2.64 KB
/
main.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
package main
import (
"context"
"encoding/json"
"io/ioutil"
"log"
"runtime"
"time"
"github.com/chromedp/chromedp/kb"
"github.com/chromedp/chromedp"
)
func main() {
cfg, err := readConfig()
if err != nil {
log.Fatalf("Could not read config file: %v", err)
}
// create context
ctxt, cancel := context.WithCancel(context.Background())
defer cancel()
// create chrome instance
c, err := chromedp.New(ctxt, chromedp.WithLog(log.Printf))
checkErr(err)
// run task list
checkErr(c.Run(ctxt, changeDrupalSettings(cfg)))
// shutdown chrome
checkErr(c.Shutdown(ctxt))
// wait for chrome to finish
checkErr(c.Wait())
log.Println("Successfully changed Drupal settings")
}
func readConfig() (*config, error) {
_, filePath, _, _ := runtime.Caller(0)
pwd := filePath[:len(filePath)-7]
txt, err := ioutil.ReadFile(pwd + "/config.json")
if err != nil {
return nil, err
}
var cfg = new(config)
if err := json.Unmarshal(txt, cfg); err != nil {
return nil, err
}
return cfg, nil
}
func changeDrupalSettings(cfg *config) chromedp.Tasks {
return chromedp.Tasks{
chromedp.Navigate(cfg.URL + "user/login"),
chromedp.WaitVisible(`#edit-name`, chromedp.ByID),
chromedp.SendKeys(`#edit-name`, cfg.Username, chromedp.ByID),
chromedp.SendKeys(`#edit-pass`, cfg.Password, chromedp.ByID),
chromedp.Click("#edit-submit"),
chromedp.Sleep(1 * time.Second),
chromedp.Navigate(cfg.URL + "admin/appearance/settings/bootstrap#edit-advanced"),
chromedp.WaitVisible(`#edit-cdn`, chromedp.ByID),
chromedp.Click(`#edit-cdn`),
chromedp.Click(`#edit-cdn-provider`),
chromedp.SendKeys(`#edit-cdn-provider`, "c"+kb.Select, chromedp.ByID),
chromedp.WaitVisible(`#edit-cdn-custom-css`, chromedp.ByID),
chromedp.Clear(`#edit-cdn-custom-css`),
chromedp.Clear(`#edit-cdn-custom-css-min`),
chromedp.Clear(`#edit-cdn-custom-js`),
chromedp.Clear(`#edit-cdn-custom-js-min`),
chromedp.SendKeys(`#edit-cdn-custom-css`, cfg.BootstrapCSS, chromedp.ByID),
chromedp.SendKeys(`#edit-cdn-custom-css-min`, cfg.BootstrapCSSMin, chromedp.ByID),
chromedp.SendKeys(`#edit-cdn-custom-js`, cfg.BootstrapJS, chromedp.ByID),
chromedp.SendKeys(`#edit-cdn-custom-js-min`, cfg.BootstrapJSMin, chromedp.ByID),
chromedp.Click("#edit-submit"),
chromedp.Sleep(1 * time.Second),
}
}
func checkErr(err error) {
if err != nil {
log.Fatal(err)
}
}
type config struct {
URL string `json:"url"`
Username string `json:"username"`
Password string `json:"password"`
BootstrapCSS string `json:"bootstrap_css"`
BootstrapCSSMin string `json:"bootstrap_css_min"`
BootstrapJS string `json:"bootstrap_js"`
BootstrapJSMin string `json:"bootstrap_js_min"`
}