codeberg-pages-deploy/plugin/util.go

37 lines
826 B
Go
Raw Normal View History

2022-07-17 18:28:14 +02:00
// 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"
"io"
"io/ioutil"
"os"
)
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 != "":
ioutil.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")
}