drone-gitea-release/plugin/util.go

63 lines
1.3 KiB
Go
Raw Permalink Normal View History

// 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"
"os/exec"
)
type GetLatestGitTagOption func() error
var FetchTags = func() error {
err := exec.Command("git", "fetch", "--tags")
return err.Err
}
func getLatestGitTag(options ...GetLatestGitTagOption) (string, error) {
for _, opt := range options {
err := opt()
if err != nil {
return "", err
}
}
tag, err := exec.Command("git", "describe", "--tags", "--abbrev=0").Output()
if err != nil {
return "", fmt.Errorf("error getting git tag %w", err)
}
return string(tag), err
}
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")
}