-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_file.go
50 lines (45 loc) · 932 Bytes
/
data_file.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
package main
import (
"os"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceFile() *schema.Resource {
return &schema.Resource{
Read: dataSourceFileRead,
Schema: map[string]*schema.Schema{
fsFILENAME: {
Type: schema.TypeString,
Description: "Filename to check for existence",
Required: true,
ForceNew: false,
},
fsEXISTS: {
Type: schema.TypeBool,
Computed: true,
},
fsISDIR: {
Type: schema.TypeBool,
Computed: true,
},
},
}
}
func dataSourceFileRead(d *schema.ResourceData, _ interface{}) error {
path := d.Get(fsFILENAME).(string)
d.SetId(path)
if fi, err := os.Stat(path); !os.IsNotExist(err) {
// path/to/whatever exists
d.Set(fsEXISTS, true)
switch mode := fi.Mode(); {
case mode.IsDir():
{
d.Set(fsISDIR, true)
}
default:
d.Set(fsISDIR, false)
}
return nil
}
d.Set(fsISDIR, false)
return nil
}