diff --git a/cmd/annotate/main.go b/cmd/annotate/main.go new file mode 100644 index 0000000..679bfd6 --- /dev/null +++ b/cmd/annotate/main.go @@ -0,0 +1,68 @@ +package main + +import ( + "flag" + "fmt" + "log" + "net/url" + "os" + "strings" + + "github.com/google/go-github/v24/github" + handler "github.com/itskoko/k8s-webhook-handler" + "k8s.io/apimachinery/pkg/api/meta" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" +) + +func main() { + var ( + evType = flag.String("-type", "push", "Event type") + action = flag.String("-action", "", "Event action") + revision = flag.String("-revision", "0000000000000000000000000000000000000000", "Revision") + ref = flag.String("-ref", "refs/heads/master", "Ref") + before = flag.String("-before", "0000000000000000000000000000000000000000", "Before") + repoURL = flag.String("-url", "git://github.com/itskoko/k8s-webhook-handler.git", "git URL") + sshUser = flag.String("-ssh-user", "git", "SSH user") + ) + flag.Parse() + files := flag.Args() + if len(files) == 0 { + log.Fatal("Usage: annotate [flags] file-to-annotate [more-files-to-annotate...]") + } + u, err := url.Parse(*repoURL) + if err != nil { + log.Fatal(err) + } + + var ( + sshURL = fmt.Sprintf("%s@%s:%s", *sshUser, u.Host, u.Path) + fullName = strings.TrimSuffix(u.Path, ".git") + ) + + repo := &github.Repository{ + FullName: &fullName, + GitURL: repoURL, + SSHURL: &sshURL, + } + annotations := (&handler.Event{ + Type: *evType, + Action: *action, + Revision: *revision, + Ref: *ref, + Before: *before, + Repository: repo, + }).Annotations() + + for _, file := range flag.Args() { + fh, err := os.Open(file) + if err != nil { + log.Fatalf("Couldn't read file %s: %s", err) + } + defer fh.Close() + obj, err := handler.Decode(fh) + meta.NewAccessor().SetAnnotations(obj, annotations) + if err := unstructured.UnstructuredJSONScheme.Encode(obj, os.Stdout); err != nil { + log.Fatal(err) + } + } +} diff --git a/loader.go b/loader.go index f8ae9c1..c715145 100644 --- a/loader.go +++ b/loader.go @@ -3,6 +3,7 @@ package handler import ( "context" "fmt" + "io" "io/ioutil" "net/url" "os" @@ -68,7 +69,12 @@ func (l *GithubLoader) Load(ctx context.Context, repo, path, ref string) (runtim return nil, fmt.Errorf("Couldn't get file %s from %s/%s at %s: %s", path, owner, name, ref, err) } defer file.Close() - content, err := ioutil.ReadAll(file) + return Decode(file) +} + +// Decode reads a reader and parses the stream as runtime.Object. +func Decode(r io.Reader) (runtime.Object, error) { + content, err := ioutil.ReadAll(r) if err != nil { return nil, fmt.Errorf("Couldn't read file: %s", err) }