Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 433639b

Browse files
committed
Introduce --quiet option
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 331f68a commit 433639b

File tree

8 files changed

+64
-18
lines changed

8 files changed

+64
-18
lines changed

Gopkg.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

e2e/build_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package e2e
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"io/ioutil"
67
"os"
78
"path"
@@ -49,7 +50,28 @@ func TestBuild(t *testing.T) {
4950
iid := string(bytes)
5051
actualID, err := store.FromBundle(&bndl)
5152
assert.NilError(t, err)
52-
assert.Equal(t, iid, actualID.String())
53+
assert.Equal(t, iid, fmt.Sprintf("sha256:%s", actualID.String()))
54+
})
55+
}
56+
57+
func TestQuietBuild(t *testing.T) {
58+
runWithDindSwarmAndRegistry(t, func(info dindSwarmAndRegistryInfo) {
59+
cmd := info.configuredCmd
60+
tmp := fs.NewDir(t, "TestBuild")
61+
62+
testDir := path.Join("testdata", "build")
63+
iidfile := tmp.Join("iidfile")
64+
cmd.Command = dockerCli.Command("app", "build", "--quiet", "--iidfile", iidfile, "-f", path.Join(testDir, "single.dockerapp"), testDir)
65+
out := icmd.RunCmd(cmd).Assert(t, icmd.Success).Combined()
66+
out = strings.Trim(out, "\r\n")
67+
68+
_, err := os.Stat(iidfile)
69+
assert.NilError(t, err)
70+
bytes, err := ioutil.ReadFile(iidfile)
71+
assert.NilError(t, err)
72+
iid := string(bytes)
73+
assert.NilError(t, err)
74+
assert.Equal(t, iid, out)
5375
})
5476
}
5577

internal/commands/build/build.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type buildOptions struct {
3939
folder string
4040
imageIDFile string
4141
args []string
42+
quiet bool
4243
}
4344

4445
func Cmd(dockerCli command.Cli) *cobra.Command {
@@ -60,6 +61,7 @@ func Cmd(dockerCli command.Cli) *cobra.Command {
6061
flags.StringVarP(&opts.folder, "folder", "f", "", "Docker app folder containing application definition")
6162
flags.BoolVar(&opts.pull, "pull", false, "Always attempt to pull a newer version of the image")
6263
flags.StringArrayVar(&opts.args, "build-arg", []string{}, "Set build-time variables")
64+
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Suppress the build output and print app image ID on success")
6365
flags.StringVar(&opts.imageIDFile, "iidfile", "", "Write the app image ID to the file")
6466

6567
return cmd
@@ -110,14 +112,18 @@ func runBuild(dockerCli command.Cli, contextPath string, opt buildOptions) error
110112
}
111113

112114
if opt.imageIDFile != "" {
113-
if err = ioutil.WriteFile(opt.imageIDFile, []byte(id.String()), 0644); err != nil {
115+
if err = ioutil.WriteFile(opt.imageIDFile, []byte(id.Digest().String()), 0644); err != nil {
114116
fmt.Fprintf(dockerCli.Err(), "Failed to write application image id in %s: %s", opt.imageIDFile, err)
115117
}
116118
}
117119

118-
fmt.Printf("Successfully built %s\n", id)
120+
if opt.quiet {
121+
fmt.Fprintln(dockerCli.Out(), id.Digest().String())
122+
return err
123+
}
124+
fmt.Fprintf(dockerCli.Out(), "Successfully built %s\n", id.String())
119125
if ref != nil {
120-
fmt.Printf("Successfully tagged %s\n", ref.String())
126+
fmt.Fprintf(dockerCli.Out(), "Successfully tagged %s\n", ref.String())
121127
}
122128
return err
123129
}
@@ -145,13 +151,23 @@ func buildImageUsingBuildx(app *types.App, contextPath string, opt buildOptions,
145151
Driver: d,
146152
},
147153
}
148-
pw := progress.NewPrinter(ctx, os.Stderr, opt.progress)
154+
155+
var out *os.File
156+
if opt.quiet {
157+
if out, err = os.Create(os.DevNull); err != nil {
158+
return nil, err
159+
}
160+
} else {
161+
out = os.NewFile(dockerCli.Out().FD(), "/dev/stdout")
162+
}
163+
164+
pw := progress.NewPrinter(ctx, out, opt.progress)
165+
149166
// We rely on buildx "docker" builder integrated in docker engine, so don't need a DockerAPI here
150167
resp, err := build.Build(ctx, driverInfo, buildopts, nil, dockerCli.ConfigFile(), pw)
151168
if err != nil {
152169
return nil, err
153170
}
154-
fmt.Fprintln(dockerCli.Out(), "Successfully built service images") //nolint:errcheck
155171

156172
bundle, err := packager.MakeBundleFromApp(dockerCli, app, nil)
157173
if err != nil {

internal/commands/image/list_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ type bundleStoreStubForListCmd struct {
2626
refList []reference.Reference
2727
}
2828

29-
func (b *bundleStoreStubForListCmd) Store(ref reference.Reference, bndle *bundle.Bundle) (reference.Reference, error) {
29+
func (b *bundleStoreStubForListCmd) Store(ref reference.Reference, bndle *bundle.Bundle) (reference.Digested, error) {
3030
b.refMap[ref] = bndle
3131
b.refList = append(b.refList, ref)
32-
return ref, nil
32+
return store.FromBundle(bndle)
3333
}
3434

3535
func (b *bundleStoreStubForListCmd) Read(ref reference.Reference) (*bundle.Bundle, error) {

internal/commands/image/tag_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@ type bundleStoreStub struct {
1515
ReadError error
1616
StoredBundle string
1717
StoredError error
18+
StoredID reference.Digested
1819
}
1920

20-
func (b *bundleStoreStub) Store(ref reference.Reference, bndle *bundle.Bundle) (reference.Reference, error) {
21+
func (b *bundleStoreStub) Store(ref reference.Reference, bndle *bundle.Bundle) (reference.Digested, error) {
2122
defer func() {
2223
b.StoredError = nil
2324
}()
2425

2526
b.StoredBundle = ref.String()
2627

27-
return ref, b.StoredError
28+
return b.StoredID, b.StoredError
2829
}
2930

3031
func (b *bundleStoreStub) Read(ref reference.Reference) (*bundle.Bundle, error) {

internal/packager/bundle.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func MakeCNABImageName(appName, appVersion, suffix string) (string, error) {
7171
}
7272

7373
// PersistInBundleStore do store a bundle with optional reference and return it's ID
74-
func PersistInBundleStore(ref reference.Reference, bndle *bundle.Bundle) (reference.Reference, error) {
74+
func PersistInBundleStore(ref reference.Reference, bndle *bundle.Bundle) (reference.Digested, error) {
7575
appstore, err := store.NewApplicationStore(config.Dir())
7676
if err != nil {
7777
return nil, err

internal/store/bundle.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ import (
1111

1212
"github.com/deislabs/cnab-go/bundle"
1313
"github.com/docker/distribution/reference"
14+
"github.com/opencontainers/go-digest"
1415
"github.com/pkg/errors"
1516
)
1617

1718
//
1819
type BundleStore interface {
1920
// Store do store the bundle with optional reference, and return it's unique ID
20-
Store(ref reference.Reference, bndle *bundle.Bundle) (reference.Reference, error)
21+
Store(ref reference.Reference, bndle *bundle.Bundle) (reference.Digested, error)
2122
Read(ref reference.Reference) (*bundle.Bundle, error)
2223
List() ([]reference.Reference, error)
2324
Remove(ref reference.Reference) error
@@ -46,12 +47,12 @@ type bundleStore struct {
4647
// \_ bundle.json
4748
//
4849

49-
func (b *bundleStore) Store(ref reference.Reference, bndle *bundle.Bundle) (reference.Reference, error) {
50+
func (b *bundleStore) Store(ref reference.Reference, bndle *bundle.Bundle) (reference.Digested, error) {
5051
digest, err := ComputeDigest(bndle)
5152
if err != nil {
5253
return nil, errors.Wrapf(err, "failed to store bundle %q", ref)
5354
}
54-
id := ID{digest.Encoded()}
55+
id := ID{digest}
5556

5657
if ref == nil {
5758
ref = id
@@ -107,7 +108,7 @@ func (b *bundleStore) List() ([]reference.Reference, error) {
107108
if strings.HasPrefix(path, digests) {
108109
rel := path[len(digests)+1:]
109110
dg := strings.Split(filepath.ToSlash(rel), "/")[0]
110-
references = append(references, ID{dg})
111+
references = append(references, ID{digest.NewDigestFromEncoded(digest.SHA256, dg)})
111112
return nil
112113
}
113114

internal/store/digest.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,26 @@ func FromString(s string) (ID, error) {
3232
if ok, _ := regexp.MatchString("[a-z0-9]{64}", s); !ok {
3333
return ID{}, fmt.Errorf("could not parse '%s' as a valid reference", s)
3434
}
35-
return ID{s}, nil
35+
digest := digest.NewDigestFromEncoded(digest.SHA256, s)
36+
return ID{digest}, nil
3637
}
3738

3839
func FromBundle(bndle *bundle.Bundle) (ID, error) {
3940
digest, err := ComputeDigest(bndle)
40-
return ID{digest.Encoded()}, err
41+
return ID{digest}, err
4142
}
4243

4344
// ID is an unique identifier for docker app image bundle, implementing reference.Reference
4445
type ID struct {
45-
digest string
46+
digest digest.Digest
4647
}
4748

4849
var _ reference.Reference = ID{}
4950

5051
func (id ID) String() string {
52+
return id.digest.Encoded()
53+
}
54+
55+
func (id ID) Digest() digest.Digest {
5156
return id.digest
5257
}

0 commit comments

Comments
 (0)