-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for printing published docker image metadata to a file (#17)
- Loading branch information
1 parent
43db3f2
commit df81f82
Showing
7 changed files
with
215 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package artifact | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
const ( | ||
dockerArtifactV1 string = "docker/v1" | ||
) | ||
|
||
type RegistryTypeEnum string | ||
|
||
const ( | ||
Docker RegistryTypeEnum = "Docker" | ||
ECR RegistryTypeEnum = "ECR" | ||
GCR RegistryTypeEnum = "GCR" | ||
) | ||
|
||
type ( | ||
Image struct { | ||
Image string `json:"image"` | ||
Digest string `json:"digest"` | ||
} | ||
Data struct { | ||
RegistryType RegistryTypeEnum `json:"registryType"` | ||
RegistryUrl string `json:"registryUrl"` | ||
Images []Image `json:"images"` | ||
} | ||
DockerArtifact struct { | ||
Kind string `json:"kind"` | ||
Data Data `json:"data"` | ||
} | ||
) | ||
|
||
func WritePluginArtifactFile(registryType RegistryTypeEnum, artifactFilePath, registryUrl, imageName, digest string, tags []string) error { | ||
var images []Image | ||
for _, tag := range tags { | ||
images = append(images, Image{ | ||
Image: fmt.Sprintf("%s:%s", imageName, tag), | ||
Digest: digest, | ||
}) | ||
} | ||
data := Data{ | ||
RegistryType: registryType, | ||
RegistryUrl: registryUrl, | ||
Images: images, | ||
} | ||
|
||
dockerArtifact := DockerArtifact{ | ||
Kind: dockerArtifactV1, | ||
Data: data, | ||
} | ||
|
||
b, err := json.MarshalIndent(dockerArtifact, "", "\t") | ||
if err != nil { | ||
return errors.Wrap(err, fmt.Sprintf("failed to marshal output %+v", dockerArtifact)) | ||
} | ||
|
||
dir := filepath.Dir(artifactFilePath) | ||
err = os.MkdirAll(dir, 0644) | ||
if err != nil { | ||
return errors.Wrap(err, fmt.Sprintf("failed to create %s directory for artifact file", dir)) | ||
} | ||
|
||
err = ioutil.WriteFile(artifactFilePath, b, 0644) | ||
if err != nil { | ||
return errors.Wrap(err, fmt.Sprintf("failed to write artifact to artifact file %s", artifactFilePath)) | ||
} | ||
return nil | ||
} |
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,17 @@ | ||
{ | ||
"kind": "docker/v1", | ||
"data": { | ||
"registryType": "Docker", | ||
"registryUrl": "https://index.docker.io/", | ||
"images": [ | ||
{ | ||
"image": "image:a1", | ||
"digest": "sha256:22332233" | ||
}, | ||
{ | ||
"image": "image:latest", | ||
"digest": "sha256:22332233" | ||
} | ||
] | ||
} | ||
} |
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,38 @@ | ||
package artifact | ||
|
||
import ( | ||
"io/ioutil" | ||
"testing" | ||
) | ||
|
||
func TestWritePluginArtifactFile(t *testing.T) { | ||
|
||
testFile := t.TempDir() + "got.json" | ||
|
||
err := WritePluginArtifactFile(Docker, testFile, "https://index.docker.io/", "image", "sha256:22332233", []string{"a1", "latest"}) | ||
if err != nil { | ||
t.Error(err) | ||
t.FailNow() | ||
} | ||
|
||
gotBytes, err := ioutil.ReadFile(testFile) | ||
if err != nil { | ||
t.Error(err) | ||
t.FailNow() | ||
} | ||
|
||
wantBytes, err := ioutil.ReadFile("./artifact.json") | ||
if err != nil { | ||
t.Error(err) | ||
t.FailNow() | ||
} | ||
|
||
got := string(gotBytes) | ||
want := string(wantBytes) | ||
|
||
if got != want { | ||
t.Logf("got:%s", got) | ||
t.Logf("want:%s", want) | ||
t.FailNow() | ||
} | ||
} |
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
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