Skip to content
This repository was archived by the owner on Dec 2, 2020. It is now read-only.

Commit be73810

Browse files
committed
Validate CLI options
1 parent de77ea4 commit be73810

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

main.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"errors"
5+
"fmt"
56
"log"
67
"os"
78

@@ -22,14 +23,15 @@ func createApp() (*cli.App, *cmdOptions) {
2223
app := cli.NewApp()
2324
app.EnableBashCompletion = true
2425
app.Name = "img2lambda"
25-
app.Usage = "Repackages a container image into Lambda layers and publishes them to Lambda"
26+
app.Usage = "Repackages a container image into AWS Lambda layers and publishes them to Lambda"
2627
app.Action = func(c *cli.Context) error {
28+
validateCliOptions(&opts, c)
2729
return repackImageAction(&opts)
2830
}
2931
app.Flags = []cli.Flag{
3032
cli.StringFlag{
3133
Name: "image, i",
32-
Usage: "Name of the source container image. For example, 'my-docker-image:latest'",
34+
Usage: "Name of the source container image. For example, 'my-docker-image:latest'. The Docker daemon must be pulled locally already.",
3335
Destination: &opts.image,
3436
},
3537
cli.StringFlag{
@@ -56,9 +58,20 @@ func createApp() (*cli.App, *cmdOptions) {
5658
Destination: &opts.dryRun,
5759
},
5860
}
61+
62+
app.Setup()
63+
app.Commands = []cli.Command{}
64+
5965
return app, &opts
6066
}
6167

68+
func validateCliOptions(opts *cmdOptions, context *cli.Context) {
69+
if opts.image == "" {
70+
fmt.Println("ERROR: Image name is required\n")
71+
cli.ShowAppHelpAndExit(context, 1)
72+
}
73+
}
74+
6275
func repackImageAction(opts *cmdOptions) error {
6376
layers, err := RepackImage("docker-daemon:"+opts.image, opts.outputDir)
6477
if err != nil {
@@ -84,6 +97,5 @@ func main() {
8497
err := app.Run(os.Args)
8598
if err != nil {
8699
log.Fatal(err)
87-
os.Exit(1)
88100
}
89101
}

0 commit comments

Comments
 (0)