Skip to content

Commit

Permalink
Create commit resource
Browse files Browse the repository at this point in the history
  • Loading branch information
au2001 committed Jan 22, 2022
1 parent 5ad84e8 commit 176022a
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 117 deletions.
8 changes: 4 additions & 4 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Apache License

Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/

Expand Down Expand Up @@ -178,15 +179,15 @@ Apache License
APPENDIX: How to apply the Apache License to your work.

To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright {yyyy} {name of copyright owner}
Copyright [yyyy] [name of copyright owner]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -199,4 +200,3 @@ Apache License
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

69 changes: 0 additions & 69 deletions Makefile

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ resource "git_commit" "hello_world" {
message = "Create file.txt"
add {
path = git_file.hello_world.path
file = git_file.hello_world.path
}
}
```
Expand Down
8 changes: 4 additions & 4 deletions provider/data_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,26 @@ func dataFileRead(ctx context.Context, d *schema.ResourceData, meta interface{})
// Open already cloned repository
_, worktree, err := getRepository(dir)
if err != nil {
return diag.Errorf("failed to open repository: %s", err.Error())
return diag.Errorf("failed to open repository: %s", err)
}

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

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

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

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

return nil
Expand Down
129 changes: 120 additions & 9 deletions provider/resource_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package provider
import (
"context"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand All @@ -11,32 +13,141 @@ func resourceCommit() *schema.Resource {
return &schema.Resource{
CreateContext: resourceCommitCreate,
ReadContext: resourceCommitRead,
// UpdateContext: resourceCommitUpdate,
DeleteContext: resourceCommitDelete,
DeleteContext: schema.NoopContext,

Schema: map[string]*schema.Schema{
"repository": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"branch": {
Type: schema.TypeString,
Optional: true,
Default: "main",
ForceNew: true,
},
"message": {
Type: schema.TypeString,
Optional: true,
Default: "Created by Terraform",
Default: "Committed with Terraform",
ForceNew: true,
},
"add": {
Type: schema.TypeList,
Required: true,
MinItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"file": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"pattern": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
},
ForceNew: true,
},

"sha": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceCommitCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
dir := d.Get("repository").(string)
// branch := d.Get("branch").(string)
message := d.Get("message").(string)
items := d.Get("add").([]interface{})

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

// Stage files
for _, item := range items {
if pattern, ok := item.(map[string]interface{})["pattern"]; ok {
err := worktree.AddGlob(pattern.(string))
if err != nil {
return diag.Errorf("failed to stage pattern %s: %s", pattern, err)
}
}
if file, ok := item.(map[string]interface{})["file"]; ok {
_, err := worktree.Add(file.(string))
if err != nil {
return diag.Errorf("failed to stage file %s: %s", file, err)
}
}
}

// Commit
sha, err := worktree.Commit(message, &git.CommitOptions{})
if err != nil {
return diag.Errorf("failed to commit: %s", err)
}

d.SetId(sha.String())
d.Set("sha", sha.String())

// Push
// err = repo.Push(&git.PushOptions{
// RefSpecs: []config.RefSpec{
// config.RefSpec(fmt.Sprintf("+HEAD:refs/remotes/origin/%s", branch)),
// },
// Force: true,
// })
// if err != nil {
// return diag.Errorf("failed to push: %s", err)
// }

return nil
}

func resourceCommitRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
return nil
}
dir := d.Get("repository").(string)
items := d.Get("add").([]interface{})

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

// func resourceCommitUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
// return nil
// }
// Stage files
for _, item := range items {
if pattern, ok := item.(map[string]interface{})["pattern"]; ok {
err := worktree.AddGlob(pattern.(string))
if err != nil {
return diag.Errorf("failed to stage pattern %s: %s", pattern, err)
}
}
if file, ok := item.(map[string]interface{})["file"]; ok {
_, err := worktree.Add(file.(string))
if err != nil && err.Error() != object.ErrEntryNotFound.Error() {
return diag.Errorf("failed to stage file %s: %s", file, err)
}
}
}

// Check if worktree is clean
status, err := worktree.Status()
if err != nil {
return diag.Errorf("failed to compute worktree status: %s", err)
}
if status.IsClean() {
d.SetId("")
return nil
}

func resourceCommitDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
return nil
}
Loading

0 comments on commit 176022a

Please sign in to comment.