2022-12-06 15:26:19 +00:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-03 12:39:36 +00:00
|
|
|
"errors"
|
|
|
|
"io/fs"
|
2022-12-06 15:26:19 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
tfjson "github.com/hashicorp/terraform-json"
|
|
|
|
)
|
|
|
|
|
|
|
|
const DatabricksProvider = "registry.terraform.io/databricks/databricks"
|
|
|
|
|
|
|
|
type Schema struct {
|
|
|
|
WorkingDir string
|
|
|
|
|
|
|
|
ProviderSchemaFile string
|
|
|
|
}
|
|
|
|
|
|
|
|
func New() (*Schema, error) {
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpdir := filepath.Join(wd, "./tmp")
|
|
|
|
err = os.MkdirAll(tmpdir, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Schema{
|
|
|
|
WorkingDir: tmpdir,
|
|
|
|
ProviderSchemaFile: filepath.Join(tmpdir, "provider.json"),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Load(ctx context.Context) (*tfjson.ProviderSchema, error) {
|
|
|
|
s, err := New()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate schema file if it doesn't exist.
|
2024-06-03 12:39:36 +00:00
|
|
|
if _, err := os.Stat(s.ProviderSchemaFile); errors.Is(err, fs.ErrNotExist) {
|
2022-12-06 15:26:19 +00:00
|
|
|
err = s.Generate(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.Load(ctx)
|
|
|
|
}
|