-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_dotfile_copy_file.go
119 lines (98 loc) · 3.14 KB
/
resource_dotfile_copy_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
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
115
116
117
118
119
package provider
import (
"fmt"
"github.com/FalcoSuessgott/terraform-provider-dotfiles/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceDotfilesCopyFile() *schema.Resource {
return &schema.Resource{
Create: resourceDotfilesCopyFileCreate,
Read: resourceDotfilesCopyFileRead,
Update: resourceDotfilesCopyFileUpdate,
Delete: resourceDotfilesCopyFileDelete,
Schema: map[string]*schema.Schema{
"src": {
Type: schema.TypeString,
Description: "absolute source path of the file",
Optional: true,
Computed: true,
ExactlyOneOf: []string{"src", "content"},
},
"content": {
Type: schema.TypeString,
Description: "content of the file.",
Optional: true,
Computed: true,
ExactlyOneOf: []string{"src", "content"},
},
"dest": {
Type: schema.TypeString,
Description: "absolute destination path of the file",
Required: true,
},
"permissions": {
Type: schema.TypeInt,
Description: "permissions of the destination file or file (defaults to 755)",
Optional: true,
Default: 755,
},
},
}
}
func resourceDotfilesCopyFileCreate(d *schema.ResourceData, meta interface{}) error {
src := d.Get("src").(string)
dest := d.Get("dest").(string)
content := d.Get("content").(string)
permissions := d.Get("permissions").(int)
switch {
case src != "":
if !utils.Exists(src) {
return fmt.Errorf("error file %s doesnt exist", src)
}
if err := utils.CopyFile(src, dest, permissions); err != nil {
return fmt.Errorf("error copying file %s to %s: %w", src, dest, err)
}
case content != "":
if err := utils.WriteFile([]byte(content), dest, permissions); err != nil {
return fmt.Errorf("error copying file %s to %s: %w", src, dest, err)
}
default:
return fmt.Errorf("this shouldnt happen")
}
d.SetId(utils.MD5Sum([]byte(content)))
return resourceDotfilesCopyFileRead(d, meta)
}
func resourceDotfilesCopyFileRead(d *schema.ResourceData, meta interface{}) error {
if err := d.Set("content", d.Get("content").(string)); err != nil {
return fmt.Errorf("error setting content: %w", err)
}
return nil
}
func resourceDotfilesCopyFileUpdate(d *schema.ResourceData, meta interface{}) error {
src := d.Get("src").(string)
dest := d.Get("dest").(string)
content := d.Get("content").(string)
permissions := d.Get("permissions").(int)
if d.HasChange("src") {
if !utils.Exists(src) {
return fmt.Errorf("error file %s doesnt exist", src)
}
if err := utils.CopyFile(src, dest, permissions); err != nil {
return fmt.Errorf("error copying file %s to %s: %w", src, dest, err)
}
content = string(utils.ReadFile(src))
}
if d.HasChange("content") {
if err := utils.WriteFile([]byte(content), dest, permissions); err != nil {
return fmt.Errorf("error copying file %s to %s: %w", src, dest, err)
}
}
return resourceDotfilesCopyFileRead(d, meta)
}
func resourceDotfilesCopyFileDelete(d *schema.ResourceData, meta interface{}) error {
dest := d.Get("dest").(string)
if err := utils.RemoveFile(dest); err != nil {
return fmt.Errorf("error deleting %s: %w", dest, err)
}
return nil
}