Skip to content

Commit

Permalink
Merge pull request #13 from coveooss/fix-list-files
Browse files Browse the repository at this point in the history
Fix panics when folders in list_files don't exist
  • Loading branch information
Julien Duchesne authored Oct 7, 2020
2 parents 79b5c4c + 0edab77 commit d34a781
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
24 changes: 17 additions & 7 deletions data_source_quantum_list_files.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"os"
"path/filepath"
"strings"
Expand All @@ -10,33 +11,34 @@ import (

func dataSourceQuantumListFiles() *schema.Resource {
return &schema.Resource{
Read: dataSourceQuantumListFilesRead,
DeprecationMessage: "Please use `fileset` instead: https://www.terraform.io/docs/configuration/functions/fileset.html",
Read: dataSourceQuantumListFilesRead,

Schema: map[string]*schema.Schema{
"folders": &schema.Schema{
"folders": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"patterns": &schema.Schema{
"patterns": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"include_folder": &schema.Schema{
"include_folder": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"recursive": &schema.Schema{
"recursive": {
Type: schema.TypeBool,
Optional: true,
},
"files": &schema.Schema{
"files": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Expand All @@ -63,7 +65,15 @@ func dataSourceQuantumListFilesRead(d *schema.ResourceData, m interface{}) error

var result []string
for _, folder := range folders {
err := filepath.Walk(folder, func(path string, info os.FileInfo, err error) error {
folderInfo, err := os.Stat(folder)
if os.IsNotExist(err) {
return fmt.Errorf("%s does not exist", folder)
}
if !folderInfo.IsDir() {
return fmt.Errorf("%s is not a dir", folder)
}

err = filepath.Walk(folder, func(path string, info os.FileInfo, err error) error {
for _, pattern := range patterns {
matched, err := filepath.Match(pattern, filepath.Base(path))
if err != nil {
Expand Down
13 changes: 7 additions & 6 deletions data_source_quantum_query_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,28 @@ import (

func dataSourceQuantumQueryJSON() *schema.Resource {
return &schema.Resource{
Read: dataSourceQuantumQueryJSONRead,
DeprecationMessage: "Terraform 0.12 supports nested maps. Ex: jsondecode(var.my_variable).attribute1.attribute2",
Read: dataSourceQuantumQueryJSONRead,

Schema: map[string]*schema.Schema{
"json": &schema.Schema{
"json": {
Type: schema.TypeString,
Required: true,
},
"query": &schema.Schema{
"query": {
Type: schema.TypeString,
Required: true,
},
"result_list": &schema.Schema{
"result_list": {
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Type: schema.TypeList,
},
"result_map": &schema.Schema{
"result_map": {
Computed: true,
Type: schema.TypeMap,
},
"result": &schema.Schema{
"result": {
Type: schema.TypeString,
Computed: true,
},
Expand Down

0 comments on commit d34a781

Please sign in to comment.