cmd

package
v0.0.0-...-a7a0a81 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 15, 2018 License: MIT Imports: 16 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Check = &cobra.Command{
	Use:   "check",
	Short: "Check the lingo of all files in a directory",
	Run: func(cmd *cobra.Command, args []string) {
		configData, err := ioutil.ReadFile(configFile)
		if err != nil {
			cli.ExitError("failed to read config file: %s", configFile)
		}

		var config Config
		if err := yaml.Unmarshal(configData, &config); err != nil {
			cli.ExitError("failed to parse config file: %s", configFile)
		}

		var matchers []file.Matcher
		for _, matcher := range config.Matchers {
			matchers = append(matchers, file.Get(matcher.Type, matcher.Config))
		}
		feeder := file.NewFeeder(matchers...)

		fc := checker.NewFileChecker()
		for slug, config := range config.Checkers {
			c := checker.Get(slug, config)
			if c == nil {
				cli.ExitError("unknown checker: %s", slug)
			}

			fc.Register(c)
		}

		files, err := feeder.Feed(args[0])
		if err != nil {
			cli.ExitError("failed to process files: %s", args[0])
		}

		reports := map[string]*checker.Report{}
		fileSets := map[string]*token.FileSet{}

		for path := range files {
			reports[path] = &checker.Report{}
			fileSets[path] = token.NewFileSet()

			content, err := ioutil.ReadFile(path)
			if err != nil {
				panic(err)
			}

			file, err := parser.ParseFile(
				fileSets[path],
				path,
				nil,
				parser.ParseComments)
			if err != nil {
				cli.ExitError("failed to parse file: %s", path)
			}

			fc.Check(file, string(content), reports[path])
		}

		totalErrors := 0
		for path, report := range reports {
			if len(report.Errors) == 0 {
				continue
			}

			fmt.Println(path)
			for _, err := range report.Errors {
				position := fileSets[path].Position(err.Pos)
				fmt.Printf("\t- line %d: %s\n", position.Line, err.Message)
			}
			fmt.Println()

			totalErrors += len(report.Errors)
		}

		if totalErrors > 0 {
			cli.ExitError("%d violations found in %d files",
				totalErrors, len(reports))
		} else {
			cli.ExitOK("%d violations found in %d files",
				totalErrors, len(reports))
		}
	},
}

Check is a command handler that checks the lingo in a directory for violations.

View Source
var Guide = &cobra.Command{
	Use:   "guide",
	Short: "Read a guide with the lingo of the project",
	Run: func(cmd *cobra.Command, args []string) {
		configData, err := ioutil.ReadFile(configFile)
		if err != nil {
			cli.ExitError("failed to read config file: %s", configFile)
		}

		var config Config
		if err := yaml.Unmarshal(configData, &config); err != nil {
			cli.ExitError("failed to parse config file: %s", configFile)
		}

		var checkers []checker.NodeChecker
		for slug, config := range config.Checkers {
			c := checker.Get(slug, config)
			if c == nil {
				cli.ExitError("unknown checker: %s", slug)
			}

			checkers = append(checkers, c)
		}

		configPath, err := filepath.Abs(configFile)
		if err != nil {
			cli.ExitError("failed to resolve config file: %s", configFile)
		}

		project := filepath.Base(filepath.Dir(configPath))

		var items []guideItem
		for _, checker := range checkers {
			item := guideItem{
				Title:       checker.Title(),
				Description: checker.Description(),
			}

			for _, example := range checker.Examples() {
				var good bytes.Buffer
				err = quick.Highlight(&good, example.Good, "go", "html", "github")
				if err != nil {
					cli.ExitError("failed to init example: %s", checker.Title())
				}

				var bad bytes.Buffer
				err = quick.Highlight(&bad, example.Bad, "go", "html", "github")
				if err != nil {
					cli.ExitError("failed to init example: %s", checker.Title())
				}

				item.Examples = append(item.Examples, guideItemExample{
					Good: good.String(),
					Bad:  bad.String(),
				})
			}

			items = append(items, item)
		}

		dir, err := ioutil.TempDir("", "lingo")
		if err != nil {
			cli.ExitError("failed to create guide dir")
		}

		guide, err := os.Create(filepath.Join(dir, "guide.html"))
		if err != nil {
			cli.ExitError("failed to create guide file")
		}
		defer guide.Close()

		data := map[string]interface{}{
			"Project": project,
			"Items":   items,
		}

		if err := guideTemplate.Execute(guide, data); err != nil {
			cli.ExitError("failed to initialize guide")
		}

		if err := openBrowser("file://" + guide.Name()); err != nil {
			cli.ExitError("failed to open guide")
		}
	},
}

Guide is a command handler that displays a guidebook of rules applicable for the current project.

View Source
var Root = &cobra.Command{
	Use:   "lingo",
	Short: "Lingo helps you define and enforce project-specific Go lingo",
}

Root is a dummy command handler.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Matchers is a list of file matchers used to define
	// the files that will be checked.
	Matchers []struct {
		Type   string                 `yaml:"type"`
		Config map[string]interface{} `yaml:"config"`
	} `yaml:"matchers"`

	// Checkers is a map[checker_slug]checker_config of checkers
	// that need to be executed.
	Checkers map[string]map[string]interface{} `yaml:"checkers"`
}

Config describes the lingo check config file structure.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL