Skip to content

Commit

Permalink
Add dockerimage.Push
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasBergmeier6176 committed Aug 22, 2022
1 parent 52dc295 commit 07d138e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## v0.0.5 - Migrate `dockerimage` to _sherlock-mavrodi_

- Add function `dockerimage.BuildFromContainerfile` for building Container Image.
- Add function `dockerimage.Push` for pushing a Container Image.

**Bugfixes**
- Correct docs of `StreamContainerLog`
Expand Down
49 changes: 49 additions & 0 deletions pkg/dockerimage/push.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package dockerimage

import (
"bufio"
"errors"
"io"
"os"
"strings"
)

const (
// remoteDigestPrefix is the line start we search for to get the remote
// digest
remoteDigestPrefix = "latest: digest: sha256:"
)

// Push calls docker and pushes a tag.
// Returns the remote digest for the pushed image.
func Push(tag string) (string, error) {

r, w := io.Pipe()
defer r.Close()
defer w.Close()

digest := make(chan string)
defer close(digest)
go func() {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
if !strings.HasPrefix(scanner.Text(), remoteDigestPrefix) {
continue
}

digest <- scanner.Text()[len(remoteDigestPrefix) : len(remoteDigestPrefix)+64]
return
}
}()
err := executeDockerCommandWithStdout(io.MultiWriter(os.Stdout, w), "push", tag)
if err != nil {
return "", err
}

select {
case d := <-digest:
return d, nil
default:
return "", errors.New("No digest in output found")
}
}

0 comments on commit 07d138e

Please sign in to comment.