Skip to content

Commit

Permalink
making tarball flag global and adding error checking
Browse files Browse the repository at this point in the history
  • Loading branch information
SethHollandsworth committed Dec 21, 2022
1 parent f9067a7 commit 7a72f7a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmd/dmverity-vhd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ dmverity-vhd --docker roothash -i alpine:3.12
Compute root hashes with tarball:

```bash
dmverity-vhd roothash --tarball /path/to/tarball.tar -i alpine:3.12
dmverity-vhd --tarball /path/to/tarball.tar roothash -i alpine:3.12
```
26 changes: 21 additions & 5 deletions cmd/dmverity-vhd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,26 @@ func main() {
Name: dockerFlag + ",d",
Usage: "Optional: use local docker daemon",
},
cli.StringFlag{
Name: tarballFlag + ",t",
Usage: "Optional: path to tarball containing image info",
},
}

// error check to see if the `docker` and `tarball` flags are both being used
// without having access to the `cli.Context` struct
localDockerUsed := false
tarballUsed := false
for i := 0; i < len(os.Args); i++ {
if os.Args[i] == "-d" || os.Args[i] == "-"+dockerFlag {
localDockerUsed = true
} else if os.Args[i] == "-t" || os.Args[i] == "-"+tarballFlag {
tarballUsed = true
}
}
if localDockerUsed && tarballUsed {
_, _ = fmt.Fprintln(os.Stderr, "cannot use both docker and tarball for image source")
os.Exit(1)
}

if err := app.Run(os.Args); err != nil {
Expand All @@ -74,7 +94,7 @@ func main() {

func fetchImageLayers(ctx *cli.Context) (layers []v1.Layer, err error) {
image := ctx.String(imageFlag)
tarballPath := ctx.String(tarballFlag)
tarballPath := ctx.GlobalString(tarballFlag)
ref, err := name.ParseReference(image)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse image reference: %s", image)
Expand Down Expand Up @@ -229,10 +249,6 @@ var rootHashVHDCommand = cli.Command{
Usage: "Required: container image reference",
Required: true,
},
cli.StringFlag{
Name: tarballFlag + ",t",
Usage: "Optional: path to tarball containing image info",
},
cli.StringFlag{
Name: usernameFlag + ",u",
Usage: "Optional: custom registry username",
Expand Down

0 comments on commit 7a72f7a

Please sign in to comment.