1 Commits

Author SHA1 Message Date
Brad Rydzewski
e311310166 minor refactor to prep for cards 2022-01-12 11:18:55 -05:00
2 changed files with 68 additions and 5 deletions

48
card.go Normal file
View File

@@ -0,0 +1,48 @@
package docker
import (
"encoding/base64"
"encoding/json"
"io"
"io/ioutil"
"os"
"os/exec"
)
func (p Plugin) writeCard() error {
cmd := exec.Command("docker", "inspect", p.Build.Name)
data, err := cmd.CombinedOutput()
if err != nil {
return err
}
out := map[string]interface{}{} // replace with docker inspect struct
if err := json.Unmarshal(data, &out); err != nil {
return err
}
card := map[string]interface{}{} // replace with card struct, populate with docker inspect output
writeCard( /*p.CardPath*/ "", &card)
return nil
}
func writeCard(path string, card interface{}) {
data, _ := json.Marshal(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")
}

View File

@@ -156,11 +156,6 @@ func (p Plugin) Exec() error {
}
}
if p.Cleanup {
cmds = append(cmds, commandRmi(p.Build.Name)) // docker rmi
cmds = append(cmds, commandPrune()) // docker system prune -f
}
// execute all commands in batch mode.
for _, cmd := range cmds {
cmd.Stdout = os.Stdout
@@ -179,6 +174,26 @@ func (p Plugin) Exec() error {
}
}
// output the adaptive card
if err := p.writeCard(); err != nil {
fmt.Printf("Could not create adaptive card. %s\n", err)
}
// execute cleanup routines in batch mode
if p.Cleanup {
// clear the slice
cmds = nil
cmds = append(cmds, commandRmi(p.Build.Name)) // docker rmi
cmds = append(cmds, commandPrune()) // docker system prune -f
for _, cmd := range cmds {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
trace(cmd)
}
}
return nil
}