-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52dc295
commit 07d138e
Showing
2 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |