Skip to content

Commit

Permalink
return exit code 2 if no results returned.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonhadfield committed Feb 5, 2019
1 parent 4f9ec4d commit 27a2ee0
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ type IPRangeDoc struct {
}

// overwritten at build time
var version, versionOutput, tag, sha, buildDate string
var (
version, versionOutput, tag, sha, buildDate string
msg string
rCode int
)

func stringInSlice(a string, list []string) bool {
for _, b := range list {
Expand All @@ -57,18 +61,18 @@ func stringInSlice(a string, list []string) bool {
}

func main() {
msg, display, err := startCLI(os.Args)
display, err := startCLI(os.Args)
if err != nil {
fmt.Printf("error: %+v\n", err)
os.Exit(1)
}
if display && msg != "" {
fmt.Print(msg)
}
os.Exit(0)
os.Exit(rCode)
}

func startCLI(args []string) (msg string, display bool, err error) {
func startCLI(args []string) (display bool, err error) {
app := cli.NewApp()
app.EnableBashCompletion = true

Expand Down Expand Up @@ -176,7 +180,6 @@ func startCLI(args []string) (msg string, display bool, err error) {
_, _ = fmt.Fprintf(c.App.Writer, "error: name cannot be used with region or service.\n")
os.Exit(1)
}
// TODO: check name is valid (format and lookup)
}

for _, f := range strings.Split(fields, ",") {
Expand Down Expand Up @@ -258,6 +261,9 @@ func startCLI(args []string) (msg string, display bool, err error) {
}
}

if len(outputDoc.Prefixes) == 0 && len(outputDoc.IPv6Prefixes) == 0 {
rCode = 2
}
// only output text format if there are entries to output
if encoding == strText && (len(outputDoc.Prefixes) == 0 && len(outputDoc.IPv6Prefixes) == 0) {
return nil
Expand All @@ -275,5 +281,5 @@ func startCLI(args []string) (msg string, display bool, err error) {
}

sort.Sort(cli.FlagsByName(app.Flags))
return msg, display, app.Run(args)
return display, app.Run(args)
}

0 comments on commit 27a2ee0

Please sign in to comment.