Skip to content

Commit

Permalink
Add "docker" subommand to "artifacts" task (palantir#223)
Browse files Browse the repository at this point in the history
Add "docker" subcommand that prints all of the tags that will be
created for a product.

Fixes palantir#219
  • Loading branch information
nmiyake authored Sep 6, 2017
1 parent 7ae2417 commit 6088141
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 0 deletions.
14 changes: 14 additions & 0 deletions apps/distgo/cmd/artifacts/artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package artifacts

import (
"fmt"
"path/filepath"
"strconv"

Expand All @@ -25,6 +26,19 @@ import (
"github.com/palantir/godel/apps/distgo/pkg/osarch"
)

// DockerArtifacts returns a map from product name to a slice that contains all of the Docker repository:tag labels
// defined for the products.
func DockerArtifacts(buildSpecsWithDeps []params.ProductBuildSpecWithDeps) map[string][]string {
output := make(map[string][]string)
for _, spec := range buildSpecsWithDeps {
for _, currImage := range spec.Spec.DockerImages {
imageName := fmt.Sprint(currImage.Repository, ":", currImage.Tag)
output[spec.Spec.ProductName] = append(output[spec.Spec.ProductName], imageName)
}
}
return output
}

// DistArtifacts returns a map from product name to OrderedStringSliceMap, where the values of the OrderedStringSliceMap
// contains the mapping from the String representation of the index of the dist type ("0", "1", etc.) to the paths for
// the artifact for that type.
Expand Down
101 changes: 101 additions & 0 deletions apps/distgo/cmd/artifacts/artifacts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,90 @@ func TestDistArtifacts(t *testing.T) {
}
}

func TestDockerArtifacts(t *testing.T) {
tmpDir, cleanup, err := dirs.TempDir("", "")
defer cleanup()
require.NoError(t, err)

for i, currCase := range []struct {
specs func(projectDir string) []params.ProductBuildSpecWithDeps
want map[string][]string
}{
{
specs: func(projectDir string) []params.ProductBuildSpecWithDeps {
return []params.ProductBuildSpecWithDeps{}
},
want: map[string][]string{},
},
{
specs: func(projectDir string) []params.ProductBuildSpecWithDeps {
return []params.ProductBuildSpecWithDeps{
createSpecWithDockerImages(projectDir, "foo", "0.1.0", nil,
params.DockerImage{
Repository: "foo/foo",
Tag: "snapshot",
},
),
}
},
want: map[string][]string{
"foo": {"foo/foo:snapshot"},
},
},
{
specs: func(projectDir string) []params.ProductBuildSpecWithDeps {
return []params.ProductBuildSpecWithDeps{
createSpecWithDockerImages(projectDir, "foo", "0.1.0", nil,
params.DockerImage{
Repository: "foo/foo",
Tag: "snapshot",
},
),
createSpecWithDockerImages(projectDir, "bar", "0.1.0", nil,
params.DockerImage{
Repository: "bar/bar",
Tag: "snapshot",
},
),
}
},
want: map[string][]string{
"foo": {"foo/foo:snapshot"},
"bar": {"bar/bar:snapshot"},
},
},
{
specs: func(projectDir string) []params.ProductBuildSpecWithDeps {
return []params.ProductBuildSpecWithDeps{
createSpecWithDockerImages(projectDir, "foo", "0.1.0", nil,
params.DockerImage{
Repository: "foo/foo",
Tag: "snapshot",
},
params.DockerImage{
Repository: "foo/foo",
Tag: "release",
},
),
}
},
want: map[string][]string{
"foo": {
"foo/foo:snapshot",
"foo/foo:release",
},
},
},
} {
currProjectDir, err := ioutil.TempDir(tmpDir, "")
require.NoError(t, err)

got := artifacts.DockerArtifacts(currCase.specs(currProjectDir))
require.NoError(t, err, "Case %d", i)
assert.Equal(t, currCase.want, got, "Case %d", i)
}
}

func toAbs(input map[string][]string, baseDir string) map[string][]string {
absWant := make(map[string][]string, len(input))
for k, v := range input {
Expand Down Expand Up @@ -398,6 +482,23 @@ func createSpecWithDists(projectDir, productName, productVersion string, osArchs
}
}

func createSpecWithDockerImages(projectDir, productName, productVersion string, osArchs []osarch.OSArch, images ...params.DockerImage) params.ProductBuildSpecWithDeps {
return params.ProductBuildSpecWithDeps{
Spec: params.ProductBuildSpec{
Product: params.Product{
Build: params.Build{
OutputDir: "build",
OSArchs: osArchs,
},
DockerImages: images,
},
ProjectDir: projectDir,
ProductName: productName,
ProductVersion: productVersion,
},
}
}

func toMap(input map[string]artifacts.OrderedStringSliceMap) map[string][]string {
output := make(map[string][]string, len(input))
for product, m := range input {
Expand Down
11 changes: 11 additions & 0 deletions apps/distgo/cmd/artifacts/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func Command() cli.Command {
Subcommands: []cli.Command{
buildArtifactsCommand("build", "Print the paths to the build artifacts for products"),
artifactsCommand("dist", "Print the paths to the distribution artifacts for products", distArtifactsAction),
artifactsCommand("docker", "Print the labels for the Docker tags for products", dockerArtifactsAction),
},
}
}
Expand Down Expand Up @@ -120,3 +121,13 @@ func buildArtifactsAction(ctx cli.Context, specs []params.ProductBuildSpecWithDe
func distArtifactsAction(ctx cli.Context, specs []params.ProductBuildSpecWithDeps, absPath bool) (map[string]OrderedStringSliceMap, error) {
return DistArtifacts(specs, absPath)
}

func dockerArtifactsAction(ctx cli.Context, specs []params.ProductBuildSpecWithDeps, absPath bool) (map[string]OrderedStringSliceMap, error) {
artifacts := make(map[string]OrderedStringSliceMap)
for k, v := range DockerArtifacts(specs) {
ordered := newOrderedStringSliceMap()
ordered.PutValues(k, v)
artifacts[k] = ordered
}
return artifacts, nil
}
14 changes: 14 additions & 0 deletions apps/distgo/cmd/docker/products.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2016 Palantir Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package docker

import (
Expand Down
14 changes: 14 additions & 0 deletions apps/distgo/cmd/docker/products_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2016 Palantir Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package docker

import (
Expand Down

0 comments on commit 6088141

Please sign in to comment.