-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-c5-products.go
158 lines (139 loc) · 3.52 KB
/
update-c5-products.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"os"
"net/http"
"io"
"os/exec"
"fmt"
"archive/zip"
"path/filepath"
"github.com/mholt/archiver"
"path"
"time"
"strconv"
)
func main() {
// Check for existence of maven distribution and if it doesn't download and extract maven
if _,err:=os.Stat("./apache-maven-3.5.2");os.IsNotExist(err){
// Download maven distribution
err = downloadContent("http://10.100.1.85:8484/apache-maven-3.5.2-bin.zip", "apache-maven-3.5.2-bin.zip")
if err != nil {
fmt.Print(err)
}
// Extract maven distribution
err = extractDistribution("./apache-maven-3.5.2-bin.zip", "./")
if err != nil {
fmt.Print(err)
}
}
// Download the pom from server
err := downloadContent("http://10.100.1.85:8484/pom.xml", "pom.xml")
if err != nil {
fmt.Print(err)
}
// Extract the given distribution
err = extractDistribution("./c5-custom-product-5.4.0.zip", "./")
if err != nil {
fmt.Print(err)
}
// Update the distribution
err = updateDistribution()
if err != nil {
fmt.Print(err)
}
timestamp := strconv.FormatInt(time.Now().Unix(), 10)
// Create the updated zip
createArchive("./c5-custom-product-5.4.0", timestamp)
// Delete temp directories/files
deleteTempFiles()
}
// Extracts the given distribution
func extractDistribution(distributionLocation, destination string) error {
zipReader, err := zip.OpenReader(distributionLocation)
if err != nil {
return err
}
defer zipReader.Close()
// Extract each file to the given destination
for _, file := range zipReader.File {
extractFile(file, destination)
}
return nil
}
// Extracts each file to the given distribution to the given location
func extractFile(file *zip.File, destination string) error {
fileContent, error := file.Open()
if error != nil {
return error
}
defer fileContent.Close()
// Create the file path
path := filepath.Join(destination, file.Name)
if file.FileInfo().IsDir() {
err := os.MkdirAll(path, 0777)
if err != nil {
return err
}
} else {
// Create parent directories if any
err := os.MkdirAll(filepath.Dir(path), 0777)
if err != nil {
return err
}
// Open the file for writing
openedFile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0777)
if err != nil {
return err
}
// Write the content to the opened file
_, err = io.Copy(openedFile, fileContent)
if err != nil {
return err
}
}
return nil
}
// Downloads content from server
func downloadContent(URL, location string) error {
// Create the file in the file system
out, err := os.Create(location)
if err != nil {
return err
}
defer out.Close()
// Get content from server
response, err := http.Get(URL)
if err != nil {
return err
}
defer response.Body.Close()
// Writes the contents of response to the created file
_, err = io.Copy(out, response.Body)
if err != nil {
return err
}
return nil
}
// Use os package to run the maven build using location where maven(headless) resides
func updateDistribution() error {
command := exec.Command("apache-maven-3.5.2/bin/mvn", "clean", "install")
command.Dir = "."
output, err := command.Output()
if err != nil {
return err
}
fmt.Printf("%s", output)
return nil
}
// Creates the updated archive
func createArchive(destination, timestamp string) {
archiver.Zip.Make(path.Join(destination+"-"+timestamp+".zip"), []string{"c5-custom-product-5.4.0"})
}
// Deletes temp directories/files
func deleteTempFiles() {
os.RemoveAll("./c5-custom-product-5.4.0")
os.RemoveAll("./target")
//os.RemoveAll("./apache-maven-3.5.2")
os.Remove("./pom.xml")
os.Remove("apache-maven-3.5.2-bin.zip")
}