gogenerate

package
v0.0.0-...-0e5bf40 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2018 License: BSD-3-Clause, MIT Imports: 12 Imported by: 0

Documentation

Overview

Package gogenerate exposes some of the unexported internals of the go generate command as a convenience for the authors of go generate generators. See https://github.com/myitcv/gogenerate/wiki/Go-Generate-Notes for further notes on such generators. It also exposes some convenience functions that might be useful to authors of generators

Index

Constants

View Source
const (
	GOARCH    = "GOARCH"
	GOFILE    = "GOFILE"
	GOLINE    = "GOLINE"
	GOOS      = "GOOS"
	GOPACKAGE = "GOPACKAGE"
	GOPATH    = "GOPATH"

	GoGeneratePrefix = "//go:generate"
)

These constants correspond in name and value to the details given in go generate --help

View Source
const (
	// FlagLog is the name of the common flag shared between go generate generators
	// to control logging verbosity
	FlagLog = "gglog"

	// FlagLicenseFile is the name of the common flag shared between go generate generators
	// to provide a license header file
	FlagLicenseFile = "licenseFile"
)

Variables

This section is empty.

Functions

func CommentLicenseHeader

func CommentLicenseHeader(file *string) (string, error)

CommentLicenseHeader is a convenience function to be used in conjunction with LicenseFileFlag; if a filename is provided it reads the contents of the file and returns a line-commented transformation of the contents with a final blank newline

func DefaultLogLevel

func DefaultLogLevel(f *string, ll LogLevel)

DefaultLogLevel is provided simply as a convenience along with LogFlag to ensure a default LogLevel in a flag variable. This is necessary because of the interplay between go generate argument parsing and the advice given for log levels via gg.

func DirFunc

func DirFunc(pkg string, name string, f func(line int, dirArgs []string) error) error

DirFunc runs f(cmds) on each go generate directive (as defined by go generate -help) found in the absolute-named file that is part of package pkg

func FileGeneratedBy

func FileGeneratedBy(path string, cmd string) bool

FileGeneratedBy returns true if the base name of the supplied path is a Go file that would have been generated by the supplied cmd

func FileIsGenerated

func FileIsGenerated(path string) (string, bool)

FileIsGenerated determines wheter the Go file located at path is generated or not and if it is generated returns the base name of the generator that generated it

func FilesContainingCmd

func FilesContainingCmd(dir string, commands ...string) ([]string, error)

FilesContainingCmd returns a sorted list of Go file names (defined by go list as GoFiles + CgoFiles + TestGoFiles + XTestGoFiles) in the directory dir that contain a command matching any of the provided commands after quote and variable expansion (as described by go generate -help). When comparing commands, the filepath.Base of each is compared. The file names will, by definition, be relative to dir

func LicenseFileFlag

func LicenseFileFlag() *string

LicenseFileFlag defines a command line string flag named according to the constant FlagLicenseFile and returns a pointer ot the string that flag set

func LogFlag

func LogFlag() *string

LogFlag defines a command line string flag named according to the constant FlagLog and returns a pointer to the string the flag sets

func NameFile

func NameFile(name string, cmd string) string

NameFile returns a file name that conforms with the pattern associated with files generated by the provided command

func NameFileFromFile

func NameFileFromFile(name string, cmd string) (string, bool)

NameFileFromFile uses the provided filename as a template and returns a generated filename consistent with the provided command

func NameTestFile

func NameTestFile(name string, cmd string) string

NameTestFile returns a file name that conforms with the pattern associated with files generated by the provided command

func WriteIfDiff

func WriteIfDiff(toWrite []byte, path string) (bool, error)

WriteIfDiff writes the supplied byte slice to the file identified by path in the case that either no file exists at that path or the contents of the file differ from the supplied bytes. The returned bool indicates whether a write was necessary or not; in case of any errors false is returned along with the error

Types

type LogLevel

type LogLevel string
const (
	LogInfo    LogLevel = "info"
	LogWarning LogLevel = "warning"
	LogError   LogLevel = "error"
	LogFatal   LogLevel = "fatal"
)

The various log levels supported by the flag returned by LogFlag()

Directories

Path Synopsis
_testFiles

Jump to

Keyboard shortcuts

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