-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
111 lines (92 loc) · 2.41 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"encoding/json"
"flag"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/flocknroll/azga/go_installer/addtxtcontent"
"github.com/flocknroll/azga/go_installer/msfstools"
"github.com/flocknroll/azga/go_installer/utils"
)
type SourceType string
const (
Local SourceType = "local"
Git SourceType = "git"
Http SourceType = "http"
)
type InstallerConfig struct {
Config []ConfigEntry
}
type ConfigEntry struct {
SourceType SourceType
SourcePath string
DestPath string
CreateFile bool
StartTag string
EndTag string
}
// Handle installation process from source to destination
func handleInstall(srcPath string, destPath string, ce *ConfigEntry) {
if ce.CreateFile {
log.Printf("Copying %s -> %s\n", srcPath, destPath)
utils.CopyFile(srcPath, destPath)
} else {
log.Printf("Checking %s -> %s\n", srcPath, destPath)
found, _ := addtxtcontent.CheckContent(srcPath, destPath, 4)
if !found {
start, end, _, found := addtxtcontent.CheckDelimitedSection(destPath, ce.StartTag, ce.EndTag)
if found {
log.Println(" --> Old version found - deleting")
addtxtcontent.DeleteLines(destPath, start, end)
}
log.Println(" --> Content not found - adding")
addtxtcontent.AddContent(srcPath, destPath)
} else {
log.Println(" --> Content found")
}
}
}
func main() {
// Configuration
var config InstallerConfig
var configPath, msfsPath string
flag.StringVar(&configPath, "config", "config.json", "Path to the JSON config file")
flag.StringVar(&msfsPath, "package-path", "", "Path to the MSFS package folder")
flag.Parse()
if msfsPath == "" {
path, ok := msfstools.GetPackageFolderPath()
if !ok {
log.Fatal("MSFS package folder not found")
} else {
msfsPath = path
}
}
data, err := ioutil.ReadFile(configPath)
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(data, &config)
if err != nil {
log.Fatal(err)
}
// Installation
for _, ce := range config.Config {
switch ce.SourceType {
case Local:
dest := filepath.Join(msfsPath, ce.DestPath)
handleInstall(ce.SourcePath, dest, &ce)
case Http:
tmpFilePath := utils.DownloadContent(ce.SourcePath)
defer os.Remove(tmpFilePath)
log.Printf("Downloaded %s -> %s\n", ce.SourcePath, tmpFilePath)
dest := filepath.Join(msfsPath, ce.DestPath)
handleInstall(tmpFilePath, dest, &ce)
case Git:
panic("Not implemented")
default:
log.Fatal("Invalid source type")
}
}
}