-
Notifications
You must be signed in to change notification settings - Fork 31
/
content.go
114 lines (98 loc) · 3.19 KB
/
content.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
// Copyright © 2016 Asteris, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package content
import (
"fmt"
"io/ioutil"
"os"
"github.com/asteris-llc/converge/resource"
"golang.org/x/net/context"
)
// Content renders content to disk
type Content struct {
// configured content of the file
Content string `export:"content"`
// configured destination of the file
Destination string `export:"destination"`
}
// Check if the content needs to be rendered
func (t *Content) Check(context.Context, resource.Renderer) (resource.TaskStatus, error) {
diffs := make(map[string]resource.Diff)
contentDiff := resource.TextDiff{Values: [2]string{"", t.Content}}
stat, err := os.Stat(t.Destination)
if os.IsNotExist(err) {
contentDiff.Values[0] = "<file-missing>"
diffs[t.Destination] = contentDiff
return &resource.Status{
Level: resource.StatusWillChange,
Differences: diffs,
Output: []string{t.Destination + ": File is missing"},
}, nil
} else if err != nil {
return &resource.Status{
Level: resource.StatusFatal,
Output: []string{"Cannot read `" + t.Destination + "`"},
}, nil
} else if stat.IsDir() {
return &resource.Status{
Level: resource.StatusCantChange,
Output: []string{t.Destination + " is a directory"},
}, fmt.Errorf("cannot update contents of %q, it is a directory", t.Destination)
}
actual, err := ioutil.ReadFile(t.Destination)
if err != nil {
return &resource.Status{}, err
}
statusMessage := "OK"
if string(actual) != t.Content {
statusMessage = "contents differ"
diffs[t.Destination] = resource.TextDiff{Values: [2]string{string(actual), t.Content}}
}
return &resource.Status{
Output: []string{statusMessage},
Differences: diffs,
}, nil
}
// Apply writes the content to disk
func (t *Content) Apply(context.Context) (resource.TaskStatus, error) {
var perm os.FileMode
var preChange string
diffs := make(map[string]resource.Diff)
stat, err := os.Stat(t.Destination)
if os.IsNotExist(err) {
diffs["mode"] = resource.TextDiff{Values: [2]string{"not set", "0600"}}
perm = 0600
} else if err != nil {
return &resource.Status{
Level: resource.StatusFatal,
Output: []string{err.Error()},
}, err
} else {
perm = stat.Mode()
}
if rawData, readErr := ioutil.ReadFile(t.Destination); readErr != nil {
preChange = "<file-missing>"
} else {
preChange = string(rawData)
}
diffs[t.Destination] = resource.TextDiff{Values: [2]string{preChange, t.Content}}
if err = ioutil.WriteFile(t.Destination, []byte(t.Content), perm); err != nil {
return &resource.Status{
Output: []string{err.Error()},
Level: resource.StatusFatal,
Differences: diffs,
}, err
}
return &resource.Status{Differences: diffs}, nil
}