forked from govend/govend
/
write.go
62 lines (52 loc) · 1.16 KB
/
write.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
// Copyright 2016 govend. All rights reserved.
// Use of this source code is governed by an Apache 2.0
// license that can be found in the LICENSE file.
package manifest
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"sort"
"github.com/BurntSushi/toml"
"gopkg.in/yaml.v2"
)
// Write writes the vendors to the manifest file on disk.
func (m *Manifest) Write() error {
// sort the manfiest vendors to fixate ordering
sort.Sort(m)
// set a slice of bytes aside for the manifest formatted contents
b := []byte{}
var err error
switch m.fmt {
case "json":
b, err = json.MarshalIndent(m, "", " ")
if err != nil {
return err
}
case "yml":
b, err = yaml.Marshal(m)
if err != nil {
return err
}
case "toml":
buf := new(bytes.Buffer)
if err := toml.NewEncoder(buf).Encode(m); err != nil {
log.Fatal(err)
}
b = buf.Bytes()
default:
return fmt.Errorf("format type %q not supported", m.fmt)
}
// cleanup any previous file formats
for _, ext := range extensions {
os.Remove(file + ext)
}
// write manifest file bytes to disk
if err := ioutil.WriteFile(file+"."+m.fmt, b, 0644); err != nil {
return err
}
return nil
}