fs

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2018 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendToFile

func AppendToFile(filePath, contents string) (bytesWritten int, created bool, err error)

AppendToFile appends the supplied string to the file at the given path. If the file already exists and is not newline-terminated, a newline will be added before contents are appended. If the file does not exist, it will be created.

func MakeTestDirectory

func MakeTestDirectory(t *testing.T, path string)

MakeTestDirectory wraps os.MkdirAll. If an error occurs, it is fatal to the test.

func ParentOptionFiles

func ParentOptionFiles(dirPath string, baseConfig *mybase.Config) ([]*mybase.File, error)

ParentOptionFiles returns a slice of *mybase.File, corresponding to the option files in the specified path's parent dir hierarchy. Evaluation of parent dirs stops once we hit either a directory containing .git, the user's home directory, or the root of the filesystem. The result is ordered such that the closest-to-root dir's File is returned first and this dir's direct parent File last. The return value excludes dirPath's file, as well as the home directory's, as these are presumed to be parsed elsewhere. The files will be read and parsed, using baseConfig to know which options are defined and valid.

func ReadTestFile

func ReadTestFile(t *testing.T, filename string) string

ReadTestFile wraps ioutil.ReadFile. If an error occurs, it is fatal to the test.

func RemoveTestDirectory

func RemoveTestDirectory(t *testing.T, path string)

RemoveTestDirectory wraps os.RemoveAll. If an error occurs, it is fatal to the test.

func RemoveTestFile

func RemoveTestFile(t *testing.T, filename string)

RemoveTestFile deletes a file (or directory). If an error occurs, it is fatal to the test.

func WriteTestFile

func WriteTestFile(t *testing.T, filename, contents string)

WriteTestFile wraps ioutil.WriteFile. If an error occurs, it is fatal to the test.

Types

type Dir

type Dir struct {
	Path              string
	Config            *mybase.Config
	OptionFile        *mybase.File
	SQLFiles          []SQLFile
	LogicalSchemas    []*LogicalSchema // for now, always 0 or 1 elements; 2+ in same dir to be supported in future
	IgnoredStatements []*Statement     // statements with unknown type / not supported by this package
}

Dir is a parsed representation of a directory that may have contained a .skeema config file and/or *.sql files.

func ParseDir

func ParseDir(dirPath string, globalConfig *mybase.Config) (*Dir, error)

ParseDir parses the specified directory, including all *.sql files in it, its .skeema config file, and all .skeema config files of its parent directory hierarchy. Evaluation of parent dirs stops once we hit either a directory containing .git, the user's home directory, or the root of the filesystem. Config sources are ordered such that the closest-to-root-dir's .skeema file is added first (and the current working dir's last), meaning that options "cascade" down the fs hierarchy and can be overridden by child directories.

func (*Dir) BaseName

func (dir *Dir) BaseName() string

BaseName returns the name of the directory without the rest of its path.

func (*Dir) Delete

func (dir *Dir) Delete() error

Delete unlinks the directory and all files within.

func (*Dir) FirstInstance

func (dir *Dir) FirstInstance() (*tengo.Instance, error)

FirstInstance returns at most one tengo.Instance based on the directory's configuration. If the config maps to multiple instances, only the first will be returned. If the config maps to no instances, nil will be returned. The instance WILL be checked for connectivity. If multiple instances are returned and some have connectivity issues, the first reachable instance will be returned.

func (*Dir) HasFile

func (dir *Dir) HasFile(name string) (bool, error)

HasFile returns true if the specified filename exists in dir.

func (*Dir) HasSchema

func (dir *Dir) HasSchema() bool

HasSchema returns true if this dir maps to at least one schema, either by stating a "schema" option in the dir's config for the current environment, and/or by having *.sql files that explicitly mention a schema name.

func (*Dir) InstanceDefaultParams

func (dir *Dir) InstanceDefaultParams() (string, error)

InstanceDefaultParams returns a param string for use in constructing a DSN. Any overrides specified in the config for this dir will be taken into account. The returned string will already be in the correct format (HTTP query string). An error will be returned if the configuration tried manipulating params that should not be user-specified.

func (*Dir) Instances

func (dir *Dir) Instances() ([]*tengo.Instance, error)

Instances returns 0 or more tengo.Instance pointers, based on the directory's configuration. The Instances will NOT be checked for connectivity. However, if the configuration is invalid (for example, illegal hostname or invalid connect-options), an error will be returned instead of any instances.

func (*Dir) SchemaNames

func (dir *Dir) SchemaNames(instance *tengo.Instance) ([]string, error)

SchemaNames interprets the value of the dir's "schema" option, returning one or more schema names that the statements in dir's *.sql files will be applied to, in cases where no schema name is explicitly specified. An instance must be supplied since the value may be instance-specific.

func (*Dir) String

func (dir *Dir) String() string

func (*Dir) Subdirs

func (dir *Dir) Subdirs() ([]*Dir, int, error)

Subdirs reads the list of direct, non-hidden subdirectories of dir, parses them (*.sql and .skeema files), and returns them. An error will be returned if there are problems reading dir's the directory list. Otherwise, err is nil but the returned int is a count of subdirs that had problems being read or parsed.

type LogicalSchema added in v1.1.0

type LogicalSchema struct {
	Name         string
	CharSet      string
	Collation    string
	CreateTables map[string]*Statement // keyed by table name
	AlterTables  []*Statement          // alterations that are run separately from CreateTables, afterwards
}

LogicalSchema represents a set of statements from *.sql files in a directory that all operated on the same schema. Note that Name is often blank, which means "all SQL statements in this dir that don't have an explicit USE statement before them". This "nameless" LogicalSchema is mapped to schema names based on the "schema" option in the dir's OptionFile.

type SQLFile

type SQLFile struct {
	Dir      string
	FileName string
}

SQLFile represents a file containing zero or more SQL statements.

func (SQLFile) Create

func (sf SQLFile) Create(contents string) error

Create writes a new file, erroring if it already exists.

func (SQLFile) Delete

func (sf SQLFile) Delete() error

Delete unlinks the file.

func (SQLFile) Exists

func (sf SQLFile) Exists() (bool, error)

Exists returns true if sf already exists in the filesystem, false if not.

func (SQLFile) Path

func (sf SQLFile) Path() string

Path returns the full absolute path to a SQLFile.

func (SQLFile) String

func (sf SQLFile) String() string

func (SQLFile) Tokenize

func (sf SQLFile) Tokenize() (*TokenizedSQLFile, error)

Tokenize reads the file and splits it into statements, returning a TokenizedSQLFile that wraps sf with the statements added. Statements preserve their whitespace and semicolons; the return value exactly represents the entire file. Some of the returned "statements" may just be comments and/or whitespace, since any comments and/or whitespace between SQL statements gets split into separate Statement values.

func (SQLFile) WriteStatements

func (sf SQLFile) WriteStatements(statements []*Statement) (int, error)

WriteStatements writes (or re-writes) the file using the contents of the supplied statements. The number of bytes written is returned.

type Statement

type Statement struct {
	File            string
	LineNo          int
	CharNo          int
	Text            string
	DefaultDatabase string // only populated if a StatementTypeUse was encountered
	Type            StatementType
	TableName       string // only populated for Types relating to Tables
	FromFile        *TokenizedSQLFile
}

Statement represents a logical instruction in a file, consisting of either an SQL statement, a command (e.g. "USE some_database"), or whitespace and/or comments between two separate statements or commands.

func (*Statement) Location

func (stmt *Statement) Location() string

Location returns the file, line number, and character number where the statement was obtained from

func (*Statement) Remove

func (stmt *Statement) Remove()

Remove removes the statement from the list of statements in stmt.FromFile. It does not rewrite the file though.

func (*Statement) SplitTextBody

func (stmt *Statement) SplitTextBody() (body string, suffix string)

SplitTextBody returns Text with its trailing semicolon and whitespace (if any) separated out into a separate string.

type StatementType

type StatementType int

StatementType indicates the type of a SQL statement found in a SQLFile. Parsing of types is very rudimentary, which can be advantageous for linting purposes. Otherwise, SQL errors or typos would prevent type detection.

const (
	StatementTypeUnknown StatementType = iota
	StatementTypeNoop                  // entirely whitespace and/or comments
	StatementTypeUse
	StatementTypeCreateTable
	StatementTypeAlterTable
)

Constants enumerating different types of statements

type TokenizedSQLFile

type TokenizedSQLFile struct {
	SQLFile
	Statements []*Statement
}

TokenizedSQLFile represents a SQLFile that has been tokenized into statements successfully.

func NewTokenizedSQLFile

func NewTokenizedSQLFile(sf SQLFile, statements []*Statement) *TokenizedSQLFile

NewTokenizedSQLFile creates a TokenizedSQLFile whose statements have a FromFile pointer linking back to the TokenizedSQLFile. This permits easy mutation of the statements and rewriting of the file.

func (*TokenizedSQLFile) Rewrite

func (tsf *TokenizedSQLFile) Rewrite() (int, error)

Rewrite rewrites the SQLFile with the current statements, returning the number of bytes written. If the file's statements now only consist of noop (comments and whitespace) statements, the file will be deleted instead, and a length of 0 will be returned.

Jump to

Keyboard shortcuts

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