-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
109 lines (86 loc) · 2.5 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"github.com/danmademe/debian-builder/models"
"github.com/ghodss/yaml"
)
var (
configFile string
postInst string
)
func check(err error, what string) {
if err != nil {
fmt.Println("debian-builder | " + what + ": " + err.Error())
return
}
}
func getDirectory() string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
check(err, "Error getting current directory")
return dir
}
func loadConfigToModel(file string) (models.Config, string, error) {
config := models.Config{}
dat, err := ioutil.ReadFile(file)
yaml.Unmarshal([]byte(dat), &config)
location, _ := filepath.Abs(file)
return config, location, err
}
func buildPostInst() error {
//Setup default postinst
postInst = "#!/bin/sh \nsudo docker-service provision"
//Get current directory
filepath := getDirectory()
postinstByte := []byte(postInst)
postinstLocation := filepath + "/.postinst"
err := ioutil.WriteFile(postinstLocation, postinstByte, 0777)
return err
}
func buildDebianPackage(control models.Control, fileName string) *exec.Cmd {
args := []string{"-s", "dir", "-t", "deb"}
//Setup Name
name := control.Package + "-" + control.Distribution
args = append(args, "-n", name)
//Setup version
version := control.Version
args = append(args, "-v", version)
//Setup After Install
afterInstall := getDirectory() + "/.postinst"
args = append(args, "--after-install", afterInstall)
//Disable Warning for no config
noConfigWarningDisable := "--deb-no-default-config-files"
args = append(args, noConfigWarningDisable)
//Setup Config
config := fileName + "=/etc/docker-service/services.d/" + control.Package + "_" + control.Version + ".yaml"
args = append(args, config)
//Exec fpm
fmt.Printf("debian-builder | %s", args)
cmd := exec.Command("fpm", args...)
return cmd
}
func setupEnvironment() (models.Control, string) {
//Read flags
flag.StringVar(&configFile, "config", "test.yaml", "a string var")
flag.Parse()
//Load config
config, filepath, err := loadConfigToModel(configFile)
check(err, "Error loading config")
//Write file
postinstErr := buildPostInst()
check(postinstErr, "Error installing postinst File")
return config.Control, filepath
}
func main() {
//Setup Environment
control, filepath := setupEnvironment()
//Build Debian Package
output, err := buildDebianPackage(control, filepath).Output()
check(err, "Error Building Package")
fmt.Println("debian-builder | Building Package")
fmt.Printf("debian-builder | %s\n", output)
}