Skip to content

Commit

Permalink
added serve option
Browse files Browse the repository at this point in the history
  • Loading branch information
mvanholsteijn committed Nov 7, 2021
1 parent b0eb107 commit 30253d3
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 42 deletions.
18 changes: 10 additions & 8 deletions committer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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]",
Expand All @@ -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 {
Expand Down
56 changes: 34 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type Cru struct {
Path []string
List bool
Update bool
Serve bool
Port string
Bump bool
NoFilename bool
DryRun bool
Expand Down Expand Up @@ -119,56 +121,57 @@ func Update(c *Cru, filename string) error {
return nil
}

func (cru *Cru) ConnectToRepository() {
if cru.Url != "" {
func (c *Cru) ConnectToRepository() error {
if c.Url != "" {
var progressReporter io.Writer = os.Stderr
if !cru.Verbose {
if !c.Verbose {
progressReporter = &bytes.Buffer{}
}
repository, err := Clone(cru.Url, progressReporter)
repository, err := Clone(c.Url, progressReporter)
if err != nil {
log.Fatal(err)
return err
}
cru.repository = repository
c.repository = repository

wt, err := repository.Worktree()
if err != nil {
log.Fatal(err)
return err
}

cru.workTree = wt
if cru.Branch != "" {
c.workTree = wt
if c.Branch != "" {
var branch *plumbing.Reference
if branches, err := repository.Branches(); err == nil {
branches.ForEach(func(ref *plumbing.Reference) error {
if ref.Name().Short() == cru.Branch {
if ref.Name().Short() == c.Branch {
branch = ref
}
return nil
})
}
if err != nil {
log.Fatal(err)
return err
}
if branch == nil {
log.Fatalf("ERROR: branch %s not found", cru.Branch)
return fmt.Errorf("ERROR: branch %s not found", c.Branch)
}
err = wt.Checkout(&git.CheckoutOptions{Branch: branch.Name()})
if err != nil {
log.Fatal(err)
return err
}
}
cru.filesystem = &wt.Filesystem
cru.cwd = "/"
c.filesystem = &wt.Filesystem
c.cwd = "/"
} else {
cwd, err := filepath.Abs(".")
if err != nil {
log.Fatal(err)
return err
}
cru.cwd = cwd
c.cwd = cwd
fs := osfs.New("/")
cru.filesystem = &fs
c.filesystem = &fs
}
return nil
}

func main() {
Expand All @@ -177,6 +180,7 @@ func main() {
Usage:
cru list [--verbose] [--no-filename] [--repository=URL [--branch=BRANCH]] [PATH] ...
cru update [--verbose] [--dry-run] [(--resolve-digest|--resolve-tag)] [--repository=URL [--branch=BRANCH] [--commit=MESSAGE]] (--all | --image-reference=REFERENCE ...) [PATH] ...
cru serve [--verbose] [--dry-run] [--port=PORT] --repository=URL --branch=BRANCH [PATH] ...
Options:
--no-filename do not print the filename.
Expand All @@ -189,7 +193,7 @@ Options:
--commit=MESSAGE commit the changes with the specified message.
--repository=URL to read and/or update.
--branch=BRANCH to update.
--port=PORT to listen on, defaults to 8080 or PORT environment variable.
`
cru := Cru{}

Expand All @@ -202,10 +206,18 @@ Options:
log.Fatal(err)
}

cru.ConnectToRepository()
if err = cru.ConnectToRepository(); err != nil {
log.Fatal(err)
}
cru.AssertPathsExists()
cru.imageRefs = make(ref.ContainerImageReferences, 0)

cru.AssertPathsExists()
if cru.Serve {
if cru.Url == "" {
log.Fatalf("cru as a service requires an git url.")
}
cru.ListenAndServe()
}

if cru.All {
if cru.Verbose {
Expand Down Expand Up @@ -260,7 +272,7 @@ Options:
if len(cru.updatedFiles) > 0 {
log.Printf("INFO: updated a total of %d files", len(cru.updatedFiles))
if cru.CommitMsg != "" {
if err = cru.Commit(); err != nil {
if _, err = cru.Commit(); err != nil {
log.Fatal(err)
}
if !IsLocalEndpoint(cru.Url) {
Expand Down
2 changes: 1 addition & 1 deletion ref/ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func (a ContainerImageReferences) ResolveTag() (ContainerImageReferences, error)
for _, tag := range tags {
if tag != r.Tag {
log.Printf("resolving repository %s tag '%s' to '%s'\n", r.Name, r.Tag, tag)
result = append(result, ContainerImageReference{Tag: tag, Name:r.Name})
result = append(result, ContainerImageReference{Tag: tag, Name: r.Name})
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion ref/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestImageResolves(t *testing.T) {

func TestFindAlternateTags(t *testing.T) {
latest := MustNewContainerImageReference("gcr.io/binx-io-public/paas-monitor:latest")
tags, err :=latest.FindAlternateTags()
tags, err := latest.FindAlternateTags()
if err != nil {
t.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions ref/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ does that work?
references := []ContainerImageReference{*MustNewContainerImageReference(`gcr.io/binx-io-public/paas-monitor:v1.0.0`),
*MustNewContainerImageReference(`mvanholsteijn/paas-monitor:3.1.0`)}

result, updated := UpdateReferences(input, references,"myfile.txt", true)
result, updated := UpdateReferences(input, references, "myfile.txt", true)
if !updated {
t.Errorf("expected the references to be updated\n")
}
Expand Down Expand Up @@ -100,11 +100,11 @@ resource "google_cloud_run_service" "app" {
}
`)
ref, _ := NewContainerImageReference(`gcr.io/binx-io-public/paas-monitor:v0.3.2`)
result, updated := UpdateReference(input, *ref,"myfile.txt", true)
result, updated := UpdateReference(input, *ref, "myfile.txt", true)
if !updated {
t.Errorf("expected the reference to be updated\n")
}
if bytes.Compare(expect, result) != 0 {
t.Errorf("expected %s, got %s\n", string(expect), string(result))
}
}
}
104 changes: 104 additions & 0 deletions serve.go
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)
}
}
13 changes: 6 additions & 7 deletions tag/tag.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package tag

import (
"fmt"
"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/name"
"regexp"
"sort"
"strconv"
"fmt"
"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/name"
"regexp"
"sort"
"strconv"
)

type Tag struct {
Expand Down Expand Up @@ -142,4 +142,3 @@ func MakeTagCategories(tags TagList) TagCategories {
}
return result
}

0 comments on commit 30253d3

Please sign in to comment.