/
directory.go
104 lines (84 loc) · 2.71 KB
/
directory.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
// 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 directory
import (
"fmt"
"os"
"path"
"github.com/asteris-llc/converge/resource"
"github.com/pkg/errors"
"golang.org/x/net/context"
)
// Directory makes sure a directory is present on disk
type Directory struct {
resource.TaskStatus
// directory path
Destination string `export:"destination"`
// if true, directories will be created recursively
CreateAll bool `export:"createall"`
}
// Check if the directory exists
func (d *Directory) Check(context.Context, resource.Renderer) (resource.TaskStatus, error) {
status := resource.NewStatus()
dest := d.Destination
for dest != "/" {
stat, err := os.Stat(dest)
switch {
case err != nil && !os.IsNotExist(err):
return status, errors.Wrapf(err, "could not stat %q")
case os.IsNotExist(err):
// if we aren't told to create everything, we should fail early
// since we can't create the parent directories
if !d.CreateAll && dest != d.Destination {
status.RaiseLevel(resource.StatusCantChange)
status.AddMessage(fmt.Sprintf("%q does not exist and will not be created (enable create_all to do this)", dest))
status.Differences = nil
return status, nil
}
status.RaiseLevel(resource.StatusWillChange)
status.AddDifference(dest, "<absent>", "<present>", "<absent>")
case !stat.IsDir():
status.RaiseLevel(resource.StatusCantChange)
status.AddMessage(fmt.Sprintf("%q already exists and is not a directory", dest))
return status, nil
default:
status.RaiseLevel(resource.StatusNoChange)
if !status.HasChanges() {
status.AddMessage(fmt.Sprintf("%q already exists", dest))
}
return status, nil
}
// pop a bit off the end
dest = path.Dir(dest)
}
d.TaskStatus = status
return d, nil
}
// Apply creates the directory
func (d *Directory) Apply(context.Context) (resource.TaskStatus, error) {
var err error
if d.CreateAll {
err = os.MkdirAll(d.Destination, 0700)
} else {
err = os.Mkdir(d.Destination, 0700)
}
if err != nil {
return nil, err
}
status := resource.NewStatus()
status.RaiseLevel(resource.StatusWillChange)
status.AddMessage(fmt.Sprintf("%q exists", d.Destination))
d.TaskStatus = status
return d, err
}