/
get_xmi.go
126 lines (103 loc) · 2.7 KB
/
get_xmi.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
package github
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strings"
"github.com/FINTLabs/fint-graphql-cli/common/utils"
"github.com/google/go-github/github"
"github.com/mitchellh/go-homedir"
"golang.org/x/net/context"
"golang.org/x/text/encoding/charmap"
)
func GetXMIFile(owner string, repo string, tag string, filename string, force bool) string {
outFileName := getFilePath(tag)
if force {
downloadFile(owner, repo, tag, filename, outFileName)
cleanFile(outFileName)
} else if !utils.FileExists(outFileName) {
downloadFile(owner, repo, tag, filename, outFileName)
cleanFile(outFileName)
}
return outFileName
}
func downloadFile(owner string, repo string, tag string, filename string, outFileName string) {
client := github.NewClient(nil)
ctx := context.Background()
opt := &github.RepositoryContentGetOptions{
Ref: tag,
}
out, err := client.Repositories.DownloadContents(ctx, owner, repo, filename, opt)
if err != nil {
fmt.Printf("Unable to download XMI file from GitHub: %s", err)
}
outFile, err := os.Create(outFileName)
defer outFile.Close()
_, err = io.Copy(outFile, out)
if err != nil {
fmt.Printf("Unable to write XMI file: %s", err)
}
}
func getFilePath(tag string) string {
homeDir, err := homedir.Dir()
if err != nil {
fmt.Println("Unable to get homedir.")
os.Exit(2)
}
dir := fmt.Sprintf("%s/.fint-model/.cache", homeDir)
err = os.MkdirAll(dir, 0777)
if err != nil {
fmt.Println("Unable to create .fint-model")
os.Exit(2)
}
outFileName := fmt.Sprintf("%s/%s.xml", dir, tag)
return outFileName
}
func cleanFile(fileName string) {
toUtf8(fileName)
input, err := ioutil.ReadFile(fileName)
if err != nil {
log.Fatalln(err)
}
lines := strings.Split(string(input), "\n")
var newLines []string
newLines = append(newLines, "<?xml version=\"1.0\" encoding=\"utf-8\"?>")
keep := false
for i, line := range lines {
if strings.Contains(line, "<xmi:Extension extender=\"Enterprise Architect\" extenderID=\"6.5\">") {
keep = true
}
if strings.Contains(line, "</xmi:XMI>") {
keep = false
}
if keep {
l := strings.Replace(lines[i], "uml:", "", -1)
l = strings.Replace(l, "xmi:", "", -1)
newLines = append(newLines, l)
}
}
output := strings.Join(newLines, "\n")
err = ioutil.WriteFile(fileName, []byte(output), 0777)
if err != nil {
log.Fatalln(err)
}
}
func toUtf8(fileName string) {
f, err := os.Open(fileName)
if err != nil {
fmt.Printf("Error opening %s (%s)", fileName, err)
os.Exit(2)
}
defer f.Close()
r := charmap.Windows1252.NewDecoder().Reader(f)
content, err := ioutil.ReadAll(r)
if err != nil {
panic(err)
}
err = ioutil.WriteFile(fileName, content, 0777)
if err != nil {
fmt.Println("\nio.Copy failed:", err)
}
}