github.com/coreos/mantle@v0.13.0/cmd/kola/console.go (about)

     1  // Copyright 2017 CoreOS, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"fmt"
    19  	"io/ioutil"
    20  	"os"
    21  
    22  	"github.com/spf13/cobra"
    23  
    24  	"github.com/coreos/mantle/kola"
    25  )
    26  
    27  var (
    28  	cmdCheckConsole = &cobra.Command{
    29  		Use:    "check-console [input-file...]",
    30  		Run:    runCheckConsole,
    31  		PreRun: preRun,
    32  		Short:  "Check console output for badness.",
    33  		Long: `
    34  Check console output for expressions matching failure messages logged
    35  by a Container Linux instance.
    36  
    37  If no files are specified as arguments, stdin is checked.
    38  `}
    39  
    40  	checkConsoleVerbose bool
    41  )
    42  
    43  func init() {
    44  	cmdCheckConsole.Flags().BoolVarP(&checkConsoleVerbose, "verbose", "v", false, "output user input prompts")
    45  	root.AddCommand(cmdCheckConsole)
    46  }
    47  
    48  func runCheckConsole(cmd *cobra.Command, args []string) {
    49  	if len(args) == 0 {
    50  		// default to stdin
    51  		args = append(args, "-")
    52  	}
    53  
    54  	errors := 0
    55  	for _, arg := range args {
    56  		var console []byte
    57  		var err error
    58  		sourceName := arg
    59  		if arg == "-" {
    60  			sourceName = "stdin"
    61  			if checkConsoleVerbose {
    62  				fmt.Printf("Reading input from %s...\n", sourceName)
    63  			}
    64  			console, err = ioutil.ReadAll(os.Stdin)
    65  		} else {
    66  			console, err = ioutil.ReadFile(arg)
    67  		}
    68  		if err != nil {
    69  			fmt.Fprintf(os.Stderr, "%s\n", err)
    70  			errors += 1
    71  			continue
    72  		}
    73  		for _, badness := range kola.CheckConsole(console, nil) {
    74  			fmt.Printf("%v: %v\n", sourceName, badness)
    75  			errors += 1
    76  		}
    77  	}
    78  	if errors > 0 {
    79  		os.Exit(1)
    80  	}
    81  }