92 lines
1.7 KiB
Go
92 lines
1.7 KiB
Go
// Copyright 2020 the Drone Authors. All rights reserved.
|
|
// Use of this source code is governed by the Blue Oak Model License
|
|
// that can be found in the LICENSE file.
|
|
|
|
package plugin
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/go-git/go-git/v5"
|
|
)
|
|
|
|
type GetLatestGitTagOption func(repo *git.Repository) error
|
|
|
|
var FetchTags = func(repo *git.Repository) error {
|
|
err := repo.Fetch(&git.FetchOptions{Tags: git.AllTags})
|
|
|
|
if err == git.NoErrAlreadyUpToDate {
|
|
return nil
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
func getLatestGitTag(options ...GetLatestGitTagOption) (tag string, err error) {
|
|
r, err := git.PlainOpen(".")
|
|
|
|
if err != nil {
|
|
return "", fmt.Errorf("error opening git repo at %s: %w", ".", err)
|
|
}
|
|
|
|
for _, opt := range options {
|
|
err := opt(r)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
}
|
|
|
|
tagRefs, err := r.Tags()
|
|
|
|
if err != nil {
|
|
return "", fmt.Errorf("error getting git tag refs %w", err)
|
|
}
|
|
|
|
for {
|
|
r, err := tagRefs.Next()
|
|
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
|
|
if err != nil {
|
|
return "", fmt.Errorf("error iterating tags %w", err)
|
|
}
|
|
|
|
parts := strings.Split(string(r.Name()), "/")
|
|
tag = strings.Join(parts[2:], "/")
|
|
}
|
|
|
|
fmt.Println(tag)
|
|
|
|
return
|
|
}
|
|
|
|
func writeCard(path, schema string, card interface{}) {
|
|
data, _ := json.Marshal(map[string]interface{}{
|
|
"schema": schema,
|
|
"data": card,
|
|
})
|
|
switch {
|
|
case path == "/dev/stdout":
|
|
writeCardTo(os.Stdout, data)
|
|
case path == "/dev/stderr":
|
|
writeCardTo(os.Stderr, data)
|
|
case path != "":
|
|
os.WriteFile(path, data, 0644)
|
|
}
|
|
}
|
|
|
|
func writeCardTo(out io.Writer, data []byte) {
|
|
encoded := base64.StdEncoding.EncodeToString(data)
|
|
io.WriteString(out, "\u001B]1338;")
|
|
io.WriteString(out, encoded)
|
|
io.WriteString(out, "\u001B]0m")
|
|
io.WriteString(out, "\n")
|
|
}
|