Skip to content

Commit e4e887b

Browse files
committed
Add additional inspection/debugging commands
1 parent 7c32f4c commit e4e887b

File tree

3 files changed

+212
-4
lines changed

3 files changed

+212
-4
lines changed

cmd/core/project-hash.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright © 2020 Christian Weichel
2+
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE.
20+
21+
package core
22+
23+
import (
24+
"context"
25+
"fmt"
26+
"os"
27+
28+
"github.com/csweichel/dazzle/pkg/dazzle"
29+
"github.com/spf13/cobra"
30+
)
31+
32+
var projectHashCmd = &cobra.Command{
33+
Use: "hash <target-ref> [chunk]",
34+
Short: "prints the hash of a chunk (or all of them)",
35+
Args: cobra.MinimumNArgs(1),
36+
RunE: func(cmd *cobra.Command, args []string) error {
37+
prj, err := dazzle.LoadFromDir(rootCfg.ContextDir, dazzle.LoadFromDirOpts{})
38+
if err != nil {
39+
return err
40+
}
41+
42+
sess, err := dazzle.NewSession(nil, args[0], dazzle.WithResolver(getResolver()))
43+
if err != nil {
44+
return err
45+
}
46+
err = sess.DownloadBaseInfo(context.Background(), prj)
47+
if err != nil {
48+
return err
49+
}
50+
51+
var chunks []dazzle.ProjectChunk
52+
if len(args[1:]) == 0 {
53+
chunks = append(prj.Chunks, prj.Base)
54+
} else {
55+
for _, c := range args[1:] {
56+
if c == "base" {
57+
chunks = append(chunks, prj.Base)
58+
continue
59+
}
60+
61+
var found bool
62+
for _, cs := range prj.Chunks {
63+
if cs.Name != c {
64+
continue
65+
}
66+
67+
found = true
68+
chunks = append(chunks, cs)
69+
}
70+
71+
if !found {
72+
return fmt.Errorf("chunk %s not found", c)
73+
}
74+
}
75+
}
76+
77+
for _, c := range chunks {
78+
hash, err := c.Hash(os.Stdout, sess)
79+
if err != nil {
80+
return err
81+
}
82+
83+
fmt.Printf("%s: %s\n", c.Name, hash)
84+
}
85+
86+
return nil
87+
},
88+
}
89+
90+
func init() {
91+
projectCmd.AddCommand(projectHashCmd)
92+
}

cmd/core/project-image-name.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright © 2020 Christian Weichel
2+
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE.
20+
21+
package core
22+
23+
import (
24+
"context"
25+
"fmt"
26+
27+
"github.com/csweichel/dazzle/pkg/dazzle"
28+
"github.com/spf13/cobra"
29+
)
30+
31+
var projectImageNameOpts struct {
32+
ImageType string
33+
ExcludeTests bool
34+
}
35+
36+
var projectImageNameCmd = &cobra.Command{
37+
Use: "image-name <target-ref> [chunk]",
38+
Short: "prints the image-name of a chunk (or all of them)",
39+
Args: cobra.MinimumNArgs(1),
40+
RunE: func(cmd *cobra.Command, args []string) error {
41+
prj, err := dazzle.LoadFromDir(rootCfg.ContextDir, dazzle.LoadFromDirOpts{})
42+
if err != nil {
43+
return err
44+
}
45+
46+
sess, err := dazzle.NewSession(nil, args[0], dazzle.WithResolver(getResolver()), dazzle.WithNoTests(projectImageNameOpts.ExcludeTests))
47+
if err != nil {
48+
return err
49+
}
50+
err = sess.DownloadBaseInfo(context.Background(), prj)
51+
if err != nil {
52+
return err
53+
}
54+
55+
var chunks []dazzle.ProjectChunk
56+
if len(args[1:]) == 0 {
57+
chunks = append(prj.Chunks, prj.Base)
58+
} else {
59+
for _, c := range args[1:] {
60+
if c == "base" {
61+
chunks = append(chunks, prj.Base)
62+
continue
63+
}
64+
65+
var found bool
66+
for _, cs := range prj.Chunks {
67+
if cs.Name != c {
68+
continue
69+
}
70+
71+
found = true
72+
chunks = append(chunks, cs)
73+
}
74+
75+
if !found {
76+
return fmt.Errorf("chunk %s not found", c)
77+
}
78+
}
79+
}
80+
81+
for _, c := range chunks {
82+
img, err := c.ImageName(dazzle.ChunkImageType(projectImageNameOpts.ImageType), sess)
83+
if err != nil {
84+
return err
85+
}
86+
87+
fmt.Printf("%s: %s\n", c.Name, img)
88+
}
89+
90+
return nil
91+
},
92+
}
93+
94+
func init() {
95+
projectCmd.AddCommand(projectImageNameCmd)
96+
projectImageNameCmd.Flags().StringVarP(&projectImageNameOpts.ImageType, "type", "t", string(dazzle.ImageTypeChunked), "chunk image type")
97+
projectImageNameCmd.Flags().BoolVar(&projectImageNameOpts.ExcludeTests, "no-tests", false, "exclude tests")
98+
}

pkg/dazzle/project.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,10 @@ type ProjectChunk struct {
125125
Tests []*test.Spec
126126
Args map[string]string
127127

128-
cachedHash string
128+
cachedHash struct {
129+
ExcludeTests string
130+
WithTests string
131+
}
129132
}
130133

131134
// LoadProjectConfig loads a dazzle project config file from disk
@@ -351,8 +354,14 @@ func loadChunks(dir fs.FS, contextBase, base, name string) (res []ProjectChunk,
351354
}
352355

353356
func (p *ProjectChunk) hash(baseref string, excludeTests bool) (res string, err error) {
354-
if p.cachedHash != "" {
355-
return p.cachedHash, nil
357+
var cachedHash *string
358+
if excludeTests {
359+
cachedHash = &p.cachedHash.ExcludeTests
360+
} else {
361+
cachedHash = &p.cachedHash.WithTests
362+
}
363+
if *cachedHash != "" {
364+
return *cachedHash, nil
356365
}
357366

358367
defer func() {
@@ -372,7 +381,7 @@ func (p *ProjectChunk) hash(baseref string, excludeTests bool) (res string, err
372381
}
373382

374383
res = hex.EncodeToString(hash.Sum(nil))
375-
p.cachedHash = res
384+
*cachedHash = res
376385
return
377386
}
378387

@@ -484,3 +493,12 @@ func (p *ProjectChunk) PrintManifest(out io.Writer, sess *BuildSession) error {
484493

485494
return p.manifest(sess.baseRef.String(), out, false)
486495
}
496+
497+
// PrintManifest prints the manifest to writer ... this is intended for debugging only
498+
func (p *ProjectChunk) Hash(out io.Writer, sess *BuildSession) (string, error) {
499+
if sess.baseRef == nil {
500+
return "", fmt.Errorf("base ref not set")
501+
}
502+
503+
return p.hash(sess.baseRef.String(), sess.opts.NoTests)
504+
}

0 commit comments

Comments
 (0)