github.com/AlekSi/nut@v0.3.1-0.20130607203728-cce108d4135e/nut/check.go (about)

     1  package main
     2  
     3  import (
     4  	"go/build"
     5  	"log"
     6  	"os"
     7  	"strings"
     8  
     9  	. "github.com/AlekSi/nut"
    10  )
    11  
    12  var (
    13  	cmdCheck = &Command{
    14  		Run:       runCheck,
    15  		UsageLine: "check [-v] [filenames]",
    16  		Short:     "check specs and nuts for errors",
    17  	}
    18  
    19  	checkV bool
    20  )
    21  
    22  func init() {
    23  	cmdCheck.Long = `
    24  Checks given spec (.json) or nut (.nut) files.
    25  If no filenames are given, checks spec nut.json in current directory.
    26  
    27  Examples:
    28      nut check
    29      nut check test_nut1/nut.json
    30      nut check test_nut1/test_nut1-0.0.1.nut
    31  `
    32  
    33  	cmdCheck.Flag.BoolVar(&checkV, "v", false, vHelp)
    34  }
    35  
    36  func runCheck(cmd *Command) {
    37  	if !checkV {
    38  		checkV = Config.V
    39  	}
    40  
    41  	args := cmd.Flag.Args()
    42  	if len(args) == 0 {
    43  		args = []string{SpecFileName}
    44  	}
    45  
    46  	for _, arg := range args {
    47  		var errors []string
    48  
    49  		parts := strings.Split(arg, ".")
    50  		switch parts[len(parts)-1] {
    51  		case "json":
    52  			spec := new(Spec)
    53  			err := spec.ReadFile(arg)
    54  			FatalIfErr(err)
    55  			pack, err := build.ImportDir(".", 0)
    56  			FatalIfErr(err)
    57  			errors = spec.Check()
    58  			errors = append(errors, CheckPackage(pack)...)
    59  
    60  		case "nut":
    61  			nf := new(NutFile)
    62  			err := nf.ReadFile(arg)
    63  			FatalIfErr(err)
    64  			errors = nf.Check()
    65  
    66  		default:
    67  			log.Fatalf("%q doesn't end with .json or .nut", arg)
    68  		}
    69  
    70  		if len(errors) != 0 {
    71  			log.Printf("Found errors in %s:", arg)
    72  			for _, e := range errors {
    73  				log.Printf("    %s", e)
    74  			}
    75  			os.Exit(1)
    76  		}
    77  
    78  		if checkV {
    79  			log.Printf("%s looks good.", arg)
    80  		}
    81  	}
    82  }