Skip to content

Commit

Permalink
Create file resource
Browse files Browse the repository at this point in the history
  • Loading branch information
au2001 committed Jan 21, 2022
1 parent c93c044 commit 5ad84e8
Showing 1 changed file with 122 additions and 6 deletions.
128 changes: 122 additions & 6 deletions provider/resource_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ package provider

import (
"context"
"errors"
"io"
"os"
"path/filepath"

gogit "github.com/go-git/go-git/v5"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand All @@ -11,36 +16,147 @@ func resourceFile() *schema.Resource {
return &schema.Resource{
CreateContext: resourceFileCreate,
ReadContext: resourceFileRead,
// UpdateContext: resourceFileUpdate,
UpdateContext: resourceFileUpdate,
DeleteContext: resourceFileDelete,

Schema: map[string]*schema.Schema{
"path": {
"repository": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"path": {
Type: schema.TypeString,
Required: true,
ForceNew: true, // TODO: Move file
},
"content": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

func resourceFileCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
dir := d.Get("repository").(string)
path := d.Get("path").(string)
content := d.Get("content").(string)

// Open already cloned repository
_, worktree, err := getRepository(dir)
if err != nil {
return diag.Errorf("failed to open repository: %s", err.Error())
}

d.SetId(filepath.Join(dir, path))

// Create, write then close file
file, err := worktree.Filesystem.Create(path)
if err != nil {
return diag.Errorf("failed to create file: %s", err.Error())
}

_, err = io.WriteString(file, content)
if err != nil {
return diag.Errorf("failed to write to file: %s", err.Error())
}

err = file.Close()
if err != nil {
return diag.Errorf("failed to close file: %s", err.Error())
}

return nil
}

func resourceFileRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
dir := d.Get("repository").(string)
path := d.Get("path").(string)

// Open already cloned repository
_, worktree, err := getRepository(dir)
if err != nil && errors.Is(err, gogit.ErrRepositoryNotExists) {
d.SetId("")
return nil
} else if err != nil {
return diag.Errorf("failed to open repository: %s", err.Error())
}

d.SetId(filepath.Join(dir, path))

// Open, read then close file
file, err := worktree.Filesystem.Open(path)
if os.IsNotExist(err) {
d.SetId("")
return nil
} else if err != nil {
return diag.Errorf("failed to open file: %s", err.Error())
}

content, err := io.ReadAll(file)
if err != nil {
return diag.Errorf("failed to read file: %s", err.Error())
}
d.Set("content", string(content))

err = file.Close()
if err != nil {
return diag.Errorf("failed to close file: %s", err.Error())
}

return nil
}

// func resourceFileUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
// return nil
// }
func resourceFileUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
dir := d.Get("repository").(string)
path := d.Get("path").(string)
content := d.Get("content").(string)

// Open already cloned repository
_, worktree, err := getRepository(dir)
if err != nil {
return diag.Errorf("failed to open repository: %s", err.Error())
}

d.SetId(filepath.Join(dir, path))

// Truncate, write then close file
file, err := worktree.Filesystem.Create(path)
if err != nil {
return diag.Errorf("failed to truncate file: %s", err.Error())
}

_, err = io.WriteString(file, content)
if err != nil {
return diag.Errorf("failed to write to file: %s", err.Error())
}

err = file.Close()
if err != nil {
return diag.Errorf("failed to close file: %s", err.Error())
}

return nil
}

func resourceFileDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
dir := d.Get("repository").(string)
path := d.Get("path").(string)

// Open already cloned repository
_, worktree, err := getRepository(dir)
if err != nil {
return diag.Errorf("failed to open repository: %s", err.Error())
}

d.SetId(filepath.Join(dir, path))

// Delete file
err = worktree.Filesystem.Remove(path)
if err != nil {
return diag.Errorf("failed to remove file: %s", err.Error())
}

return nil
}

0 comments on commit 5ad84e8

Please sign in to comment.