-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
initialize.go
51 lines (40 loc) · 1.34 KB
/
initialize.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
package main
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/rafecolton/docker-builder/analyzer"
"github.com/BurntSushi/toml"
"github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
)
func initialize(c *cli.Context) {
dir := c.Args().First()
if dir == "" {
dir = "."
}
file, err := analyzer.ParseAnalysisFromDir(dir)
if err != nil {
exitErr(1, "unable to create Bobfile", err)
}
bobfilePath := filepath.Join(dir, "Bobfile")
//no error when stating, file already exists, rename with timestamp
if _, err := os.Stat(bobfilePath); err == nil {
bobfilePath = fmt.Sprintf("%s.%d", bobfilePath, int32(time.Now().Unix()))
}
outfile, err := os.Create(bobfilePath)
if err != nil {
exitErr(86, "unable to create output file", map[string]interface{}{"output_file": bobfilePath, "error": err})
}
defer outfile.Close()
encoder := toml.NewEncoder(outfile)
if err = encoder.Encode(file); err != nil {
exitErr(123, "unable to write to output file", map[string]interface{}{"output_file": bobfilePath, "error": err})
}
vimFtComment := []byte("\n\n# vim:ft=toml")
if _, err := outfile.Write(vimFtComment); err != nil {
exitErr(127, "unable to write to output file", map[string]interface{}{"output_file": bobfilePath, "error": err})
}
Logger.WithFields(logrus.Fields{"output_file": bobfilePath}).Info("successfully initialized")
}