-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b0eb107
commit 30253d3
Showing
7 changed files
with
159 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,24 +2,26 @@ package main | |
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"gopkg.in/src-d/go-git.v4" | ||
"gopkg.in/src-d/go-git.v4/plumbing" | ||
"gopkg.in/src-d/go-git.v4/plumbing/object" | ||
"io" | ||
"log" | ||
"os" | ||
"time" | ||
) | ||
|
||
func (c *Cru) Commit() error { | ||
func (c *Cru) Commit() (hash plumbing.Hash, err error) { | ||
if c.CommitMsg == "" { | ||
return nil | ||
return plumbing.ZeroHash, fmt.Errorf("a commit message is required") | ||
} | ||
|
||
for _, path := range c.updatedFiles { | ||
|
||
_, err := c.workTree.Add(c.RelPath(path)) | ||
_, err = c.workTree.Add(c.RelPath(path)) | ||
if err != nil { | ||
return err | ||
return plumbing.ZeroHash, err | ||
} | ||
|
||
if c.Verbose { | ||
|
@@ -28,7 +30,7 @@ func (c *Cru) Commit() error { | |
} | ||
|
||
if !c.DryRun { | ||
hash, err := c.workTree.Commit(c.CommitMsg, &git.CommitOptions{ | ||
hash, err = c.workTree.Commit(c.CommitMsg, &git.CommitOptions{ | ||
Author: &object.Signature{ | ||
Name: "cru", | ||
Email: "[email protected]", | ||
|
@@ -38,15 +40,15 @@ func (c *Cru) Commit() error { | |
if err != nil { | ||
log.Printf("ERROR: failed to commit changes, %s\n", err) | ||
c.workTree.Reset(nil) | ||
return err | ||
} | ||
|
||
if c.Verbose { | ||
log.Printf("INFO: changes committed with %s", hash.String()[0:7]) | ||
} | ||
} else { | ||
hash = plumbing.ZeroHash | ||
} | ||
|
||
return nil | ||
return | ||
} | ||
|
||
func (c *Cru) Push() error { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/binxio/cru/ref" | ||
"gopkg.in/src-d/go-git.v4/plumbing" | ||
"log" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
type ContainerReferenceUpdateRequest struct { | ||
CommitMessage string `json:"commit-message"` | ||
ImageReferences []string `json:"image-references"` | ||
} | ||
|
||
type ContainerReferenceUpdateResponse struct { | ||
Files []string `json:"files,omitempty"` | ||
Hash string `json:"commit-sha,omitempty"` | ||
} | ||
|
||
func (c Cru) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
|
||
var hash plumbing.Hash | ||
var request ContainerReferenceUpdateRequest | ||
|
||
err := json.NewDecoder(r.Body).Decode(&request) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
c.CommitMsg = request.CommitMessage | ||
if c.CommitMsg == "" { | ||
http.Error(w, "commit message is empty", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
c.imageRefs = make(ref.ContainerImageReferences, 0) | ||
for _, r := range request.ImageReferences { | ||
r, err := ref.NewContainerImageReference(r) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
c.imageRefs = append(c.imageRefs, *r) | ||
} | ||
if len(c.imageRefs) == 0 { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
|
||
if err = c.ConnectToRepository(); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if err = c.Walk(Update); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if len(c.updatedFiles) > 0 { | ||
log.Printf("INFO: updated a total of %d files", len(c.updatedFiles)) | ||
if hash, err = c.Commit(); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
if err = c.Push(); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
} else { | ||
log.Println("INFO: no files were updated by cru") | ||
c.updatedFiles = make([]string, 0) | ||
} | ||
if body, err := json.Marshal(ContainerReferenceUpdateResponse{ | ||
c.updatedFiles, hash.String()}); err == nil { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.Write(body) | ||
return | ||
} else { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
} | ||
|
||
func (c *Cru) ListenAndServe() { | ||
port := os.Getenv("PORT") | ||
if port == "" { | ||
port = "8080" | ||
} | ||
|
||
if c.Port == "" { | ||
c.Port = os.Getenv("PORT") | ||
} | ||
if c.Port == "" { | ||
c.Port = "8080" | ||
} | ||
|
||
log.Printf("Listening on port %s", c.Port) | ||
if err := http.ListenAndServe(":"+c.Port, c); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters