-
Notifications
You must be signed in to change notification settings - Fork 5
/
data_nix_build.go
54 lines (45 loc) · 1 KB
/
data_nix_build.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
package main
import (
"os"
"github.com/andrewchambers/terraform-provider-nix/nix"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceNixBuild() *schema.Resource {
return &schema.Resource{
Read: dataNixBuildRead,
Schema: map[string]*schema.Schema{
"nix_path": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"expression_path": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"store_path": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataNixBuildRead(d *schema.ResourceData, m interface{}) error {
nixPath := os.Getenv("NIX_PATH")
if p, ok := d.GetOk("nix_path"); ok {
nixPath = p.(string)
}
expressionPath := d.Get("expression_path").(string)
storePath, err := nix.BuildExpression(nixPath, expressionPath, nil)
if err != nil {
return err
}
id := d.Id()
if id == "" {
d.SetId(randomID())
}
err = d.Set("store_path", storePath)
if err != nil {
return err
}
return nil
}