Compare commits

...

93 Commits

Author SHA1 Message Date
Andrey Nering
726130be09 v1.4.1 2017-07-15 15:52:33 -03:00
Andrey Nering
968a29d869 deduplicate conversion from dep and cmd to call 2017-07-15 15:46:53 -03:00
Andrey Nering
ce27e973be hotpath for a blank variable template 2017-07-15 15:38:57 -03:00
Andrey Nering
2607866c49 readme: document new syntax for synamic variables 2017-07-15 15:37:24 -03:00
Andrey Nering
e8e914b11c use YAML for dynamix variable instead of $ prefix
$ prefix still works but is now deprecated

before:

    VAR: $echo var

after:

    VAR:
      sh: echo bar

closes #46
2017-07-15 15:28:59 -03:00
Andrey Nering
d22b3b0d88 readme: document the task_checksum.txt file on releases
closes #44
2017-07-15 14:52:31 -03:00
Andrey Nering
5ece1d74f6 fix Taskvars.yml vars not available while interpolating vars prop
closes #40
2017-07-15 14:40:58 -03:00
Andrey Nering
ac7ab42d94 Create CODE_OF_CONDUCT.md 2017-07-15 14:31:53 -03:00
Andrey Nering
38a3f538f5 add CONTRIBUTING.md 2017-07-15 14:27:45 -03:00
Andrey Nering
998935ea55 add --list (or -l) flag to print existing tasks
If an inexixtent task is given, the help also prints as before

Also fixing README documentation

Closes #51
2017-07-15 14:10:46 -03:00
Andrey Nering
e781ac2512 own Taskfile: add task "todo" to print TODOs in code 2017-07-15 13:58:03 -03:00
Andrey Nering
ef2695974d os specific Taskvars file
Now it's possible to have Taskfile_windows.yml or Taskvars_linux.yml
(and others).

Asked in a comment in #46
2017-07-15 13:52:02 -03:00
Andrey Nering
b552cc2b12 yaml: use UnmarshalStrict instead on Unmarshal
https://github.com/go-yaml/yaml/pull/262
2017-07-15 13:36:22 -03:00
Andrey Nering
4ae9c2445d update deps 2017-07-15 13:34:42 -03:00
Sindre Røkenes Myren
769e25f080 Consider task up-to-date on equal timestamps
Fixes issue #48 by considering a task up-to-date if the newest
file from sources and the oldest file from generates has
exactly the same time-stamp.
2017-07-12 18:18:26 +02:00
Sindre Røkenes Myren
86a23849e0 Allow absolute path in generates section
Fixes issue #47 by allowing absolute paths in a task's generates and
sources sections. Tests are added for the generates section only at this
time.
2017-07-12 18:14:34 +02:00
Andrey Nering
a586fef414 Merge pull request #43 from smyrman/issue-42
Bugfix: allow templating when calling deps
2017-07-09 20:02:39 -03:00
Sindre Myren
a548c63a92 Bugfix: allow templating when calling deps
Fixes issue #42 by allowing for template evaluatation on task override
variables when the task is launched as dependency.
2017-07-09 23:48:36 +02:00
Andrey Nering
5268df6bfd only get variable map twice and implement vars template
ref #40
2017-07-08 16:00:17 -03:00
Andrey Nering
82e1c0f810 refactor: functions reorder (higher level first) 2017-07-08 15:13:27 -03:00
Andrey Nering
81b0ffb7f4 rename: readTaskvarsFile() -> readTaskvars() 2017-07-08 15:10:01 -03:00
Andrey Nering
0da130ee2c refactor: simplify some controls 2017-07-08 15:08:44 -03:00
Andrey Nering
6e880c9027 refactor: don't need to run template on Executor.Dir 2017-07-08 15:01:45 -03:00
Andrey Nering
082fa321cb rename 2 files
- read_taskfile.go -> taskfile.go
- variable_handling.go -> variables.go
2017-07-08 14:58:43 -03:00
Andrey Nering
ff1c49f111 refactor: better usage of bytes.Buffer type 2017-07-08 14:57:12 -03:00
Andrey Nering
50f592c540 refactor getVariables() 2017-07-08 14:48:37 -03:00
Andrey Nering
7a7f66dfdc refactor: join task and vars parameters in a single Call struct 2017-07-08 14:34:17 -03:00
Andrey Nering
a1140aa62f Merge branch 'cyclic-dep' 2017-07-08 13:36:45 -03:00
Andrey Nering
2dd3564da1 changed cyclic dep detection
since interpolation can be used, detection should be a execution time,
and not before

now, to prevent infinite execution, there's a miximum of 100 calls per
task

closes #37
2017-07-08 13:33:55 -03:00
Andrey Nering
fb4b0a187e Merge branch 'issue-37-cyclic-dep' 2017-07-08 10:26:18 -03:00
Andrey Nering
ac48ee066e fix panic for invalid task in cyclic dep detection
resolves partly #37
2017-07-08 10:25:15 -03:00
Andrey Nering
06031efc09 cyclic: refactor to return error instead 2017-07-08 10:13:56 -03:00
Andrey Nering
9bea80b862 Merge pull request #41 from zbindenren/master
better error output for dynamic variables in Taskvars.yml
2017-07-07 07:01:21 -03:00
Rene Zbinden
92ecb1c7ec better error output for dynamic variables in Taskvars.yml 2017-07-07 08:33:56 +02:00
Andrey Nering
645f77b849 Merge pull request #39 from smyrman/issue-38
Allow template evaluation when calling a task with vars
2017-07-06 21:30:47 -03:00
Sindre Røkenes Myren
2f9381065d Allow template evaluation in parameters
When passing variables to a sub-task, allow template evaluation
within the passed-on variables.
2017-07-07 00:47:56 +02:00
Sindre Røkenes Myren
774ef61c2f Update usage text slightly 2017-07-06 18:40:01 +02:00
Andrey Nering
dd2c66d2e1 v1.4.0 2017-07-05 21:32:30 -03:00
Andrey Nering
0deb2d78fb improve README documentation 2017-07-05 21:31:41 -03:00
Andrey Nering
fdd7e7f2a8 add own Taskfile.yml file 2017-07-05 21:06:12 -03:00
Andrey Nering
ad1a440576 cache dymanic variables 2017-07-05 21:03:59 -03:00
Andrey Nering
222b5cb587 add verbose mode (-v flag) 2017-07-05 20:56:31 -03:00
Andrey Nering
a1d1f73fe7 update dependencies 2017-07-05 20:46:05 -03:00
Andrey Nering
e7f9ace559 rename goreleaser.yml to .goreleaser.yml 2017-07-05 20:39:19 -03:00
Andrey Nering
cb72c404f5 Merge pull request #32 from go-task/parameters
Add task parameters
2017-07-05 20:34:42 -03:00
Andrey Nering
01b9bf5289 update README documentation about calling another task 2017-07-05 20:30:58 -03:00
Andrey Nering
a52a66ec1c test usage of param with $ prefix 2017-07-05 20:12:40 -03:00
Andrey Nering
313d7089da fix lint 2017-07-05 20:10:45 -03:00
Andrey Nering
06d80e92eb rename Cmd.Params to Cmd.Vars 2017-07-05 20:07:27 -03:00
Andrey Nering
e1fc3aa4fb remove support for TOML and JSON
ref #34
2017-07-05 19:56:52 -03:00
Andrey Nering
b8fe8d465e refactor: onyl read Taskvars file once 2017-07-03 21:16:10 -03:00
Andrey Nering
196d3cb13d add custom Cmd and Dep types 2017-07-03 21:04:38 -03:00
Andrey Nering
a3bfa13670 Merge pull request #35 from smyrman/print-on-set
Print on set
2017-07-03 19:53:03 -03:00
Sindre Røkenes Myren
2ace0defd0 Print command, also when "set:" is specified
Always prints the command, even when the
set-keyword is used within the task.
2017-07-03 15:05:19 +02:00
Sindre Røkenes Myren
023a902f61 Improve task command help text 2017-07-03 15:05:19 +02:00
Andrey Nering
789a4c03df Merge branch 'stdout-redir' 2017-07-01 15:33:44 -03:00
Andrey Nering
ecfd8e8a62 change all tests to call functions instead of binary directly
I had to temporarely hack github.com/mvdan/sh to fix dir handling
2017-07-01 15:32:13 -03:00
Andrey Nering
9ba44f3e6e allow custom Stdin, Stdout and Stderr while running as a lib 2017-07-01 15:05:51 -03:00
Andrey Nering
03fd5c84ec improvements on README 2017-06-28 21:20:52 -03:00
Andrey Nering
81e0f170ef accept setting dir of execution and improve tests
One test is not yet migrated. First we should have specific
Stdin, Stdout and Stderr for executor.
2017-06-24 20:09:05 -03:00
Andrey Nering
7e06ba1728 update deps 2017-06-24 16:00:10 -03:00
Andrey Nering
f8a5825083 readme: improve releases paragraph 2017-06-19 20:55:21 -03:00
Andrey Nering
a14f7f215c setup fpm to release .deb and .rpm packages
closes #15
2017-06-19 20:48:39 -03:00
Andrey Nering
8393e8c52f have a consistent release name format
closes #30
2017-06-19 20:31:03 -03:00
Andrey Nering
81d221667b update deps 2017-06-16 14:59:12 -03:00
Andrey Nering
4f928e7570 v1.3.1 2017-06-14 15:33:30 -03:00
Andrey Nering
08622ba8cb readme: update badge link 2017-06-14 15:32:29 -03:00
Andrey Nering
685b9ae293 improvements on release process 2017-06-14 15:28:35 -03:00
Andrey Nering
e97fd65cd3 update deps and move to golang/dep
Closes #28
2017-06-14 14:03:33 -03:00
Andrey Nering
ba494702ed Taskfile: add update-deps task 2017-06-14 14:00:07 -03:00
Andrey Nering
ad3f439cb5 readme: add one more alternative 2017-06-11 19:52:41 -03:00
Andrey Nering
067d6e6a02 Update README 2017-06-04 17:05:36 -03:00
Andrey Nering
540e458b16 refactor isUpToDate() 2017-06-04 16:45:34 -03:00
Andrey Nering
b530cba0d5 Abstract Tasks type 2017-06-04 16:41:38 -03:00
Andrey Nering
09e6d5269d Update dependencies 2017-06-04 16:06:04 -03:00
Andrey Nering
f98bf6c4b1 refactor: Create executor struct to get rid of global variables
Maybe eventually help on #17
2017-06-04 16:02:04 -03:00
Andrey Nering
c40148a52e Update github.com/mvdan/sh 2017-05-27 11:17:49 -03:00
Andrey Nering
460297e43a README: Add more alternatives 2017-05-27 10:52:44 -03:00
Andrey Nering
561349c820 Add ExeExt template function 2017-05-27 10:52:22 -03:00
Andrey Nering
398a2c519c Fix instantiation of parser
Seems that the parser cannot be reused.
Some tests were ramdomly failing.
2017-05-17 16:16:07 -03:00
Andrey Nering
2615000609 Add --init flag to create a new Taskfile 2017-05-17 15:38:46 -03:00
Andrey Nering
83f1b213fa Use context on status commands 2017-05-17 14:53:39 -03:00
Andrey Nering
c9a64fedd7 Fix build after update of dependency 2017-05-17 14:49:58 -03:00
Andrey Nering
504723bc19 Update github.com/mvdan/sh 2017-05-17 14:49:27 -03:00
Andrey Nering
b590e74ce6 Merge branch 'status' 2017-05-17 14:43:36 -03:00
Andrey Nering
353e4c4f48 README: Add documentation for status 2017-05-17 14:42:23 -03:00
Andrey Nering
2a2dfce137 Add status option to prevent task from running
Closes #27
2017-05-17 14:37:16 -03:00
Andrey Nering
86e0496555 Update vendor of github.com/mvdan/sh 2017-04-30 19:50:22 -03:00
Andrey Nering
bb84b067c5 Merge branch 'variables' 2017-04-30 19:42:27 -03:00
Andrey Nering
b269c6e162 Allow interpolation on "generates" and "sources" attributes
Closes #26
2017-04-30 19:32:33 -03:00
Andrey Nering
8b76911675 Small refactor of variables replacing 2017-04-30 19:13:21 -03:00
Andrey Nering
7e5cfefede Simplify condition 2017-04-30 18:50:44 -03:00
Andrey Nering
9beef8b99b Downloading goreleaser as a cli dep 2017-04-24 10:48:57 -03:00
190 changed files with 31152 additions and 24511 deletions

8
.gitignore vendored
View File

@@ -15,3 +15,11 @@
./task
dist/
vendor/**
!vendor/**/*.go
!vendor/**/LICENSE
!vendor/**/COPYING
!vendor/**/README
!vendor/**/README.md
vendor/**/*_test.go

33
.goreleaser.yml Normal file
View File

@@ -0,0 +1,33 @@
build:
binary: task
main: cmd/task/task.go
goos:
- windows
- darwin
- linux
goarch:
- 386
- amd64
ignore:
- goos: darwin
goarch: 386
archive:
name_template: "{{.Binary}}_{{.Os}}_{{.Arch}}"
format_overrides:
- goos: windows
format: zip
release:
draft: true
fpm:
vendor: Task
homepage: https://github.com/go-task/task
maintainer: Andrey Nering <andrey.nering@gmail.com>
description: Simple task runner written in Go
license: MIT
formats:
- deb
- rpm

46
CODE_OF_CONDUCT.md Normal file
View File

@@ -0,0 +1,46 @@
# Contributor Covenant Code of Conduct
## Our Pledge
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
## Our Standards
Examples of behavior that contributes to creating a positive environment include:
* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members
Examples of unacceptable behavior by participants include:
* The use of sexualized language or imagery and unwelcome sexual attention or advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a professional setting
## Our Responsibilities
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
## Scope
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
## Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at andrey.nering@gmail.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
## Attribution
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
[homepage]: http://contributor-covenant.org
[version]: http://contributor-covenant.org/version/1/4/

11
CONTRIBUTING.md Normal file
View File

@@ -0,0 +1,11 @@
* Bug reports and feature requests are welcome in [the issues][issues]
* For questions and discussion there's the [Slack room][slack]
* Pull Requests are welcome. For more complex changes and features it's
recommended to open an issue first
* About 3 or 4 pull requests accepted one gets write access to the repo.
Even then, possible backward incompatible changes should be discussed first
in an issue or pull request
* Documentation contributions are as important as code contributions
[issues]: https://github.com/go-task/task/issues
[slack]: https://gophers.slack.com/messages/task

117
Gopkg.lock generated Normal file
View File

@@ -0,0 +1,117 @@
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
name = "github.com/Masterminds/semver"
packages = ["."]
revision = "517734cc7d6470c0d07130e40fd40bdeb9bcd3fd"
version = "v1.3.1"
[[projects]]
branch = "master"
name = "github.com/Masterminds/sprig"
packages = ["."]
revision = "e039e20e500c2c025d9145be375e27cf42a94174"
[[projects]]
name = "github.com/aokoli/goutils"
packages = ["."]
revision = "3391d3790d23d03408670993e957e8f408993c34"
version = "v1.0.1"
[[projects]]
name = "github.com/davecgh/go-spew"
packages = ["spew"]
revision = "346938d642f2ec3594ed81d874461961cd0faa76"
version = "v1.1.0"
[[projects]]
branch = "master"
name = "github.com/fsnotify/fsnotify"
packages = ["."]
revision = "4da3e2cfbabc9f751898f250b49f2439785783a1"
[[projects]]
branch = "master"
name = "github.com/huandu/xstrings"
packages = ["."]
revision = "3959339b333561bf62a38b424fd41517c2c90f40"
[[projects]]
branch = "master"
name = "github.com/imdario/mergo"
packages = ["."]
revision = "e3000cb3d28c72b837601cac94debd91032d19fe"
[[projects]]
branch = "master"
name = "github.com/mattn/go-zglob"
packages = [".","fastwalk"]
revision = "95345c4e1c0ebc9d16a3284177f09360f4d20fab"
[[projects]]
branch = "master"
name = "github.com/mvdan/sh"
packages = ["interp","syntax"]
revision = "cbdcf488deaebe5f51b5bb993f21d194f9f2211e"
[[projects]]
name = "github.com/pmezard/go-difflib"
packages = ["difflib"]
revision = "792786c7400a136282c1664665ae0a8db921c6c2"
version = "v1.0.0"
[[projects]]
name = "github.com/satori/go.uuid"
packages = ["."]
revision = "879c5887cd475cd7864858769793b2ceb0d44feb"
version = "v1.1.0"
[[projects]]
branch = "master"
name = "github.com/spf13/pflag"
packages = ["."]
revision = "e57e3eeb33f795204c1ca35f56c44f83227c6e66"
[[projects]]
name = "github.com/stretchr/testify"
packages = ["assert"]
revision = "69483b4bd14f5845b5a1e55bca19e954e827f1d0"
version = "v1.1.4"
[[projects]]
branch = "master"
name = "golang.org/x/crypto"
packages = ["pbkdf2","scrypt"]
revision = "dd85ac7e6a88fc6ca420478e934de5f1a42dd3c6"
[[projects]]
branch = "master"
name = "golang.org/x/net"
packages = ["context"]
revision = "f01ecb60fe3835d80d9a0b7b2bf24b228c89260e"
[[projects]]
branch = "master"
name = "golang.org/x/sync"
packages = ["errgroup"]
revision = "f52d1811a62927559de87708c8913c1650ce4f26"
[[projects]]
branch = "master"
name = "golang.org/x/sys"
packages = ["unix"]
revision = "4cd6d1a821c7175768725b55ca82f14683a29ea4"
[[projects]]
branch = "v2"
name = "gopkg.in/yaml.v2"
packages = ["."]
revision = "3b4ad1db5b2a649883ff3782f5f9f6fb52be71af"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "93839de063626661a216a313ab71e2ad920afb2528f69ca6110c2155276e6dab"
solver-name = "gps-cdcl"
solver-version = 1

99
Gopkg.toml Normal file
View File

@@ -0,0 +1,99 @@
## Gopkg.toml example (these lines may be deleted)
## "metadata" defines metadata about the project that could be used by other independent
## systems. The metadata defined here will be ignored by dep.
# [metadata]
# key1 = "value that convey data to other systems"
# system1-data = "value that is used by a system"
# system2-data = "value that is used by another system"
## "required" lists a set of packages (not projects) that must be included in
## Gopkg.lock. This list is merged with the set of packages imported by the current
## project. Use it when your project needs a package it doesn't explicitly import -
## including "main" packages.
# required = ["github.com/user/thing/cmd/thing"]
## "ignored" lists a set of packages (not projects) that are ignored when
## dep statically analyzes source code. Ignored packages can be in this project,
## or in a dependency.
# ignored = ["github.com/user/project/badpkg"]
## Constraints are rules for how directly imported projects
## may be incorporated into the depgraph. They are respected by
## dep whether coming from the Gopkg.toml of the current project or a dependency.
# [[constraint]]
## Required: the root import path of the project being constrained.
# name = "github.com/user/project"
#
## Recommended: the version constraint to enforce for the project.
## Only one of "branch", "version" or "revision" can be specified.
# version = "1.0.0"
# branch = "master"
# revision = "abc123"
#
## Optional: an alternate location (URL or import path) for the project's source.
# source = "https://github.com/myfork/package.git"
#
## "metadata" defines metadata about the dependency or override that could be used
## by other independent systems. The metadata defined here will be ignored by dep.
# [metadata]
# key1 = "value that convey data to other systems"
# system1-data = "value that is used by a system"
# system2-data = "value that is used by another system"
## Overrides have the same structure as [[constraint]], but supersede all
## [[constraint]] declarations from all projects. Only [[override]] from
## the current project's are applied.
##
## Overrides are a sledgehammer. Use them only as a last resort.
# [[override]]
## Required: the root import path of the project being constrained.
# name = "github.com/user/project"
#
## Optional: specifying a version constraint override will cause all other
## constraints on this project to be ignored; only the overridden constraint
## need be satisfied.
## Again, only one of "branch", "version" or "revision" can be specified.
# version = "1.0.0"
# branch = "master"
# revision = "abc123"
#
## Optional: specifying an alternate source location as an override will
## enforce that the alternate location is used for that project, regardless of
## what source location any dependent projects specify.
# source = "https://github.com/myfork/package.git"
[[constraint]]
branch = "master"
name = "github.com/Masterminds/sprig"
[[constraint]]
branch = "master"
name = "github.com/fsnotify/fsnotify"
[[constraint]]
branch = "master"
name = "github.com/imdario/mergo"
[[constraint]]
branch = "master"
name = "github.com/mattn/go-zglob"
[[constraint]]
branch = "master"
name = "github.com/mvdan/sh"
[[constraint]]
branch = "master"
name = "github.com/spf13/pflag"
[[constraint]]
branch = "master"
name = "golang.org/x/sync"
[[constraint]]
branch = "v2"
name = "gopkg.in/yaml.v2"

249
README.md
View File

@@ -1,12 +1,27 @@
[![Join the chat at https://gitter.im/go-task/task](https://badges.gitter.im/go-task/task.svg)](https://gitter.im/go-task/task?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![Join Slack room](https://img.shields.io/badge/%23task-chat%20room-blue.svg)](https://gophers.slack.com/messages/task)
[![Build Status](https://travis-ci.org/go-task/task.svg?branch=master)](https://travis-ci.org/go-task/task)
# Task - Simple task runner / "Make" alternative
Task is a simple tool that allows you to easily run development and build
tasks. Task is written in Go, but can be used to develop any language.
tasks. Task is written in Golang, but can be used to develop any language.
It aims to be simpler and easier to use then [GNU Make][make].
- [Installation](#installation)
- [Usage](#usage)
- [Environment](#environment)
- [OS specific task](#os-specific-task)
- [Task directory](#task-directory)
- [Task dependencies](#task-dependencies)
- [Calling another task](#calling-another-task)
- [Prevent unnecessary work](#prevent-unnecessary-work)
- [Variables](#variables)
- [Dynamic variables](#dynamic-variables)
- [Go's template engine](#gos-template-engine)
- [Help](#help)
- [Watch tasks](#watch-tasks-experimental)
- [Alternative task runners](#alternative-task-runners)
## Installation
If you have a [Golang][golang] environment setup, you can simply run:
@@ -15,14 +30,14 @@ If you have a [Golang][golang] environment setup, you can simply run:
go get -u -v github.com/go-task/task/cmd/task
```
Or you can download from the [releases][releases] page and add to your `PATH`.
Or you can download the binary from the [releases][releases] page and add to
your `PATH`. DEB and RPM packages are also available.
The `task_checksums.txt` file contains the SHA-256 checksum for each file.
## Usage
Create a file called `Taskfile.yml` in the root of the project.
(`Taskfile.toml` and `Taskfile.json` are also supported, but YAML is used in
the documentation). The `cmds` attribute should contains the commands of a
task:
The `cmds` attribute should contains the commands of a task:
```yml
build:
@@ -54,15 +69,15 @@ You can specify environment variables that are added when running a command:
```yml
build:
cmds:
- echo $hallo
- echo $hallo
env:
hallo: welt
```
### OS specific task support
### OS specific task
If you add a `Taskfile_{{GOOS}}` you can override or amend your taskfile based
on the operating system.
If you add a `Taskfile_{{GOOS}}.yml` you can override or amend your taskfile
based on the operating system.
Example:
@@ -71,7 +86,7 @@ Taskfile.yml:
```yml
build:
cmds:
- echo "default"
- echo "default"
```
Taskfile_linux.yml:
@@ -79,12 +94,16 @@ Taskfile_linux.yml:
```yml
build:
cmds:
- echo "linux"
- echo "linux"
```
Will print out `linux` and not default
Will print out `linux` and not default.
### Running task in another dir
It's also possible to have OS specific `Taskvars.yml` file, like
`Taskvars_windows.yml` or `Taskvars_darwin.yml`. See the
[variables section](#variables) below.
### Task directory
By default, tasks will be executed in the directory where the Taskfile is
located. But you can easily make the task run in another folder informing
@@ -131,6 +150,9 @@ css:
- npm run buildcss
```
If there are more than one dependency, they always run in parallel for better
performance.
Each task can only be run once. If it is included from another dependend task causing
a cyclomatic dependency, execution will be stopped.
@@ -142,31 +164,62 @@ task2:
deps: [task1]
```
Will stop at the moment the dependencies of `task2` are executed.
The above will fail with the message: "cyclic dependency detected".
### Calling a task from another task
### Calling another task
When a task has many dependencies, they are executed concurrently. This will
often result in a faster build pipeline. But in some situations you may need
to call other tasks serially. For this just prefix a command with `^`:
to call other tasks serially. In this case, just use the following syntax:
```yml
main-task:
cmds:
- task: task-to-be-called
- task: another-task
- echo "Both done"
task-to-be-called:
cmds:
- echo "Task to be called"
another-task:
cmds:
- echo "Another task"
```
Overriding variables in the called task is as simple as informing `vars`
attribute:
```yml
main-task:
cmds:
- task: write-file
vars: {FILE: "hello.txt", CONTENT: "Hello!"}
- task: write-file
vars: {FILE: "world.txt", CONTENT: "World!"}
write-file:
cmds:
- echo "{{.CONTENT}}" > {{.FILE}}
```
The above syntax is also supported in `deps`.
> NOTE: It's also possible to call a task without any param prefixing it
with `^`, but this syntax is deprecaded:
```yml
a-task:
cmds:
- ^another-task
- ^even-another-task
- echo "Both done"
another-task:
cmds:
- ...
even-another-task:
cmds:
- ...
- echo "Another task"
```
### Prevent task from running when not necessary
### Prevent unnecessary work
If a task generates something, you can inform Task the source and generated
files, so Task will prevent to run them if not necessary.
@@ -199,74 +252,103 @@ will compare the modification date/time of the files to determine if it's
necessary to run the task. If not, it will just print
`Task "js" is up to date`.
Alternatively, you can inform a sequence of tests as `status`. If no error
is returned (exit status 0), the task is considered up-to-date:
```yml
generate-files:
cmds:
- mkdir directory
- touch directory/file1.txt
- touch directory/file2.txt
# test existence of files
status:
- test -d directory
- test -f directory/file1.txt
- test -f directory/file2.txt
```
You can use `--force` or `-f` if you want to force a task to run even when
up-to-date.
### Variables
```yml
build:
deps: [setvar]
cmds:
- echo "{{.PREFIX}} {{.THEVAR}}"
vars:
PREFIX: "Path:"
When doing interpolation of variables, Task will look for the below.
They are listed below in order of importance (e.g. most important first):
setvar:
cmds:
- echo "{{.PATH}}"
set: THEVAR
- Variables given while calling a task from another.
(See [Calling another task](#calling-another-task) above)
- Environment variables
- Variables declared locally in the task
- Variables available in the `Taskvars.yml` file
Example of overriding with environment variables:
```bash
$ TASK_VARIABLE=a-value task do-something
```
The above sample saves the path into a new variable which is then again echoed.
Example of locally declared vars:
You can use environment variables, task level variables and a file called
`Taskvars.yml` (or `Taskvars.toml` or `Taskvars.json`) as source of variables.
```yml
print-var:
cmds:
echo "{{.VAR}}"
vars:
VAR: Hello!
```
They are evaluated in the following order:
Example of `Taskvars.yml` file:
Task local variables are overwritten by variables found in `Taskvars` file.
Variables found in `Taskvars` file are overwritten with variables from the
environment. The output of the last command is stored in the environment. So
you can do something like this:
```yml
PROJECT_NAME: My Project
DEV_MODE: production
GIT_COMMIT: {sh: git log -n 1 --format=%h}
```
> NOTE: It's also possible setting a variable globally using `set` attribute
in task, but this is deprecated:
```yml
build:
deps: [setvar]
deps: [set-message]
cmds:
- echo "{{.PREFIX}} '{{.THEVAR}}'"
vars:
PREFIX: "Result: "
- echo "Message: {{.MESSAGE}}"
setvar:
set-message:
cmds:
- echo -n "a"
- echo -n "{{.THEVAR}}b"
- echo -n "{{.THEVAR}}c"
set: THEVAR
```
The result of a run of build would be:
```
a
ab
abc
Result: 'abc'
- echo "This is an important message"
set: MESSAGE
```
#### Dynamic variables
If you prefix a variable with `$`, then the variable is considered a dynamic
variable. The value after the $-symbol will be treated as a command and the
output assigned.
The below syntax (`sh:` prop in a variable) is considered a dynamic
variable. The value will be treated as a command and the output assigned.
```yml
build:
cmds:
- go build -ldflags="-X main.Version={{.LAST_GIT_COMMIT}}" main.go
- go build -ldflags="-X main.Version={{.GIT_COMMIT}}" main.go
vars:
LAST_GIT_COMMIT: $git log -n 1 --format=%h
GIT_COMMIT:
sh: git log -n 1 --format=%h
```
This works for all types of variables.
> It's also possible to prefix the variable with `$` to have a dynamic
variable, but this is now considered deprecated:
```yml
# Taskvars.yml
# recommended
GIT_COMMIT:
sh: git log -n 1 --format=%h
# deprecated
GIT_COMMIT: $git log -n 1 --format=%h
```
### Go's template engine
@@ -277,7 +359,7 @@ them. Variables are acessible through dot syntax (`.VARNAME`).
All functions by the Go's [sprig lib](http://masterminds.github.io/sprig/)
are available. The following example gets the current date in a given format:
```go
```yml
print-date:
cmds:
- echo {{now | date "2006-01-02"}}
@@ -293,21 +375,24 @@ Task also adds the following functions:
path format to `/`.
- `FromSlash`: Oposite of `ToSlash`. Does nothing on Unix, but on Windows
converts a string from `\` path format to `/`.
- `ExeExt`: Returns the right executable extension for the current OS
(`".exe"` for Windows, `""` for others).
Example:
```yml
printos:
print-os:
cmds:
- echo '{{OS}} {{ARCH}}'
- "echo '{{if eq OS \"windows\"}}windows-command{{else}}unix-command{{end}}'"
- echo '{{if eq OS "windows"}}windows-command{{else}}unix-command{{end}}'
# This will be path/to/file on Unix but path\to\file on Windows
- "{{FromSlash \"path/to/file\"}}"
- echo '{{FromSlash "path/to/file"}}'
```
### Help
Running `task help` lists all tasks with a description. The following taskfile:
Running `task --list` (or `task -l`) lists all tasks with a description.
The following taskfile:
```yml
build:
@@ -332,8 +417,8 @@ css:
would print the following output:
```bash
build Build the go binary.
test Run all the go tests.
* build: Build the go binary.
* test: Run all the go tests.
```
## Watch tasks (experimental)
@@ -342,12 +427,15 @@ If you give a `--watch` or `-w` argument, task will watch for files changes
and run the task again. This requires the `sources` attribute to be given,
so task know which files to watch.
## Alternatives
## Alternative task runners
Similar software:
- [tj/robo][robo]
- [dogtools/dog][dog]
- YAML based:
- [tj/robo][robo]
- [dogtools/dog][dog]
- [goeuro/myke][myke]
- Go based:
- [go-godo][godo]
- [markbates/grift][grift]
[make]: https://www.gnu.org/software/make/
[releases]: https://github.com/go-task/task/releases
@@ -355,4 +443,7 @@ Similar software:
[gotemplate]: https://golang.org/pkg/text/template/
[robo]: https://github.com/tj/robo
[dog]: https://github.com/dogtools/dog
[myke]: https://github.com/goeuro/myke
[godo]: https://github.com/go-godo/godo
[grift]: https://github.com/markbates/grift
[sh]: https://github.com/mvdan/sh

View File

@@ -3,12 +3,26 @@
install:
desc: Installs Task
cmds:
- go install -v ./...
- go install -v -ldflags="-w -s -X main.version={{.GIT_COMMIT}}" ./cmd/task
dl-deps:
desc: Downloads cli dependencies
cmds:
- go get -u github.com/golang/lint/golint
- go get -u github.com/goreleaser/goreleaser
- go get -u github.com/asticode/go-astitodo/astitodo
- go get -u github.com/golang/dep/cmd/dep
update-deps:
desc: Updates dependencies
cmds:
- dep ensure -update
- dep prune
clean:
desc: Cleans temp files and folders
cmds:
- rm -rf dist/
lint:
desc: Runs golint
@@ -28,3 +42,13 @@ release:
desc: Release Task
cmds:
- goreleaser
test-release:
desc: Tests release process without publishing
cmds:
- goreleaser --skip-validate --skip-publish
todo:
desc: Prints TODO comments present in the code
cmds:
- astitodo {{.GO_PACKAGES}}

6
Taskvars.yml Normal file
View File

@@ -0,0 +1,6 @@
GIT_COMMIT: $git log -n 1 --format=%h
GO_PACKAGES:
.
./cmd/task
./execext

View File

@@ -2,18 +2,26 @@ package main
import (
"fmt"
"log"
"os"
"github.com/go-task/task"
"github.com/spf13/pflag"
)
func main() {
pflag.Usage = func() {
fmt.Println(`task [target1 target2 ...]: Runs commands under targets like make.
var (
version = "master"
)
const usage = `Usage: task [-ilfwv] [--init] [--list] [--force] [--watch] [--verbose] [task...]
Runs the specified task(s). Falls back to the "default" task if no task name
was specified, or lists all tasks if an unknown task name was specified.
Example: 'task hello' with the following 'Taskfile.yml' file will generate an
'output.txt' file with the content "hello".
Example: 'task hello' with the following 'Taskfile.yml' file will generate
an 'output.txt' file.
'''
hello:
cmds:
@@ -22,11 +30,76 @@ hello:
generates:
- output.txt
'''
`)
Options:
`
func main() {
log.SetFlags(0)
pflag.Usage = func() {
fmt.Print(usage)
pflag.PrintDefaults()
}
pflag.BoolVarP(&task.Force, "force", "f", false, "forces execution even when the task is up-to-date")
pflag.BoolVarP(&task.Watch, "watch", "w", false, "enables watch of the given task")
var (
versionFlag bool
init bool
list bool
force bool
watch bool
verbose bool
)
pflag.BoolVar(&versionFlag, "version", false, "show Task version")
pflag.BoolVarP(&init, "init", "i", false, "creates a new Taskfile.yml in the current folder")
pflag.BoolVarP(&list, "list", "l", false, "lists tasks with description of current Taskfile")
pflag.BoolVarP(&force, "force", "f", false, "forces execution even when the task is up-to-date")
pflag.BoolVarP(&watch, "watch", "w", false, "enables watch of the given task")
pflag.BoolVarP(&verbose, "verbose", "v", false, "enables verbose mode")
pflag.Parse()
task.Run()
if versionFlag {
log.Printf("Task version: %s\n", version)
return
}
if init {
wd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
if err := task.InitTaskfile(wd); err != nil {
log.Fatal(err)
}
return
}
e := task.Executor{
Force: force,
Watch: watch,
Verbose: verbose,
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
}
if err := e.ReadTaskfile(); err != nil {
log.Fatal(err)
}
if list {
e.PrintTasksHelp()
return
}
args := pflag.Args()
if len(args) == 0 {
log.Println("task: No argument given, trying default task")
args = []string{"default"}
}
if err := e.Run(args...); err != nil {
log.Fatal(err)
}
}

74
command.go Normal file
View File

@@ -0,0 +1,74 @@
package task
import (
"errors"
"strings"
)
// Cmd is a task command
type Cmd struct {
Cmd string
Task string
Vars Vars
}
// Dep is a task dependency
type Dep struct {
Task string
Vars Vars
}
var (
// ErrCantUnmarshalCmd is returned for invalid command YAML
ErrCantUnmarshalCmd = errors.New("task: can't unmarshal cmd value")
// ErrCantUnmarshalDep is returned for invalid dependency YAML
ErrCantUnmarshalDep = errors.New("task: can't unmarshal dep value")
)
// UnmarshalYAML implements yaml.Unmarshaler interface
func (c *Cmd) UnmarshalYAML(unmarshal func(interface{}) error) error {
var cmd string
if err := unmarshal(&cmd); err == nil {
if strings.HasPrefix(cmd, "^") {
c.Task = strings.TrimPrefix(cmd, "^")
} else {
c.Cmd = cmd
}
return nil
}
var taskCall struct {
Task string
Vars Vars
}
if err := unmarshal(&taskCall); err == nil {
c.Task = taskCall.Task
c.Vars = taskCall.Vars
return nil
}
return ErrCantUnmarshalCmd
}
// UnmarshalYAML implements yaml.Unmarshaler interface
func (d *Dep) UnmarshalYAML(unmarshal func(interface{}) error) error {
var task string
if err := unmarshal(&task); err == nil {
d.Task = task
return nil
}
var taskCall struct {
Task string
Vars Vars
}
if err := unmarshal(&taskCall); err == nil {
d.Task = taskCall.Task
d.Vars = taskCall.Vars
return nil
}
return ErrCantUnmarshalDep
}
// Call is the parameters to a task call
type Call struct {
Task string
Vars Vars
}

60
command_test.go Normal file
View File

@@ -0,0 +1,60 @@
package task_test
import (
"testing"
"github.com/go-task/task"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
)
func TestCmdParse(t *testing.T) {
const (
yamlCmd = `echo "a string command"`
yamlDep = `"task-name"`
yamlTaskCall = `
task: another-task
vars:
PARAM1: VALUE1
PARAM2: VALUE2
`
)
tests := []struct {
content string
v interface{}
expected interface{}
}{
{
yamlCmd,
&task.Cmd{},
&task.Cmd{Cmd: `echo "a string command"`},
},
{
yamlTaskCall,
&task.Cmd{},
&task.Cmd{Task: "another-task", Vars: task.Vars{
"PARAM1": task.Var{Static: "VALUE1"},
"PARAM2": task.Var{Static: "VALUE2"},
}},
},
{
yamlDep,
&task.Dep{},
&task.Dep{Task: "task-name"},
},
{
yamlTaskCall,
&task.Dep{},
&task.Dep{Task: "another-task", Vars: task.Vars{
"PARAM1": task.Var{Static: "VALUE1"},
"PARAM2": task.Var{Static: "VALUE2"},
}},
},
}
for _, test := range tests {
err := yaml.Unmarshal([]byte(test.content), test.v)
assert.NoError(t, err)
assert.Equal(t, test.expected, test.v)
}
}

View File

@@ -1,29 +0,0 @@
package task
// HasCyclicDep checks if a task tree has any cyclic dependency
func HasCyclicDep(m map[string]*Task) bool {
visits := make(map[string]struct{}, len(m))
var checkCyclicDep func(string, *Task) bool
checkCyclicDep = func(name string, t *Task) bool {
if _, ok := visits[name]; ok {
return false
}
visits[name] = struct{}{}
defer delete(visits, name)
for _, d := range t.Deps {
if !checkCyclicDep(d, m[d]) {
return false
}
}
return true
}
for k, v := range m {
if !checkCyclicDep(k, v) {
return true
}
}
return false
}

View File

@@ -1,36 +0,0 @@
package task_test
import (
"testing"
"github.com/go-task/task"
)
func TestCyclicDepCheck(t *testing.T) {
isCyclic := map[string]*task.Task{
"task-a": &task.Task{
Deps: []string{"task-b"},
},
"task-b": &task.Task{
Deps: []string{"task-a"},
},
}
if !task.HasCyclicDep(isCyclic) {
t.Error("Task should be cyclic")
}
isNotCyclic := map[string]*task.Task{
"task-a": &task.Task{
Deps: []string{"task-c"},
},
"task-b": &task.Task{
Deps: []string{"task-c"},
},
"task-c": &task.Task{},
}
if task.HasCyclicDep(isNotCyclic) {
t.Error("Task should not be cyclic")
}
}

View File

@@ -1,15 +1,21 @@
package task
import (
"errors"
"fmt"
)
var (
// ErrTaskfileAlreadyExists is returned on creating a Taskfile if one already exists
ErrTaskfileAlreadyExists = errors.New("task: A Taskfile already exists")
)
type taskFileNotFound struct {
taskFile string
}
func (err taskFileNotFound) Error() string {
return fmt.Sprintf(`task: No task file found (is it named "%s"?)`, err.taskFile)
return fmt.Sprintf(`task: No task file found (is it named "%s"?). Use "task --init" to create a new one`, err.taskFile)
}
type taskNotFoundError struct {
@@ -44,3 +50,27 @@ type cantWatchNoSourcesError struct {
func (err *cantWatchNoSourcesError) Error() string {
return fmt.Sprintf(`task: Can't watch task "%s" because it has no specified sources`, err.taskName)
}
type dynamicVarError struct {
cause error
cmd string
}
func (err *dynamicVarError) Error() string {
return fmt.Sprintf(`task: Command "%s" in taskvars file failed: %s`, err.cmd, err.cause)
}
// MaximumTaskCallExceededError is returned when a task is called too
// many times. In this case you probably have a cyclic dependendy or
// infinite loop
type MaximumTaskCallExceededError struct {
task string
}
func (e *MaximumTaskCallExceededError) Error() string {
return fmt.Sprintf(
`task: maximum task call exceeded (%d) for task "%s": probably an cyclic dep or infinite loop`,
MaximumTaskCall,
e.task,
)
}

View File

@@ -1,11 +0,0 @@
{
"hello": {
"cmds": [
"echo \"I am going to write a file named 'output.txt' now.\"",
"echo \"hello\" > output.txt"
],
"generates": [
"output.txt"
]
}
}

View File

@@ -1,6 +0,0 @@
[hello]
cmds = [
"echo \"I am going to write a file named 'output.txt' now.\"",
"echo \"hello\" > output.txt"
]
generates = ["output.txt"]

View File

@@ -32,7 +32,7 @@ func RunCommand(opts *RunCommandOptions) error {
return ErrNilOptions
}
p, err := syntax.Parse(strings.NewReader(opts.Command), "", 0)
p, err := syntax.NewParser().Parse(strings.NewReader(opts.Command), "")
if err != nil {
return err
}
@@ -46,8 +46,5 @@ func RunCommand(opts *RunCommandOptions) error {
Stdout: opts.Stdout,
Stderr: opts.Stderr,
}
if err = r.Run(); err != nil {
return err
}
return nil
return r.Run()
}

11
file.go
View File

@@ -2,6 +2,7 @@ package task
import (
"os"
"path/filepath"
"time"
"github.com/mattn/go-zglob"
@@ -20,8 +21,11 @@ func maxTime(a, b time.Time) time.Time {
return b
}
func getPatternsMinTime(patterns []string) (m time.Time, err error) {
func getPatternsMinTime(dir string, patterns []string) (m time.Time, err error) {
for _, p := range patterns {
if !filepath.IsAbs(p) {
p = filepath.Join(dir, p)
}
mp, err := getPatternMinTime(p)
if err != nil {
return time.Time{}, err
@@ -30,8 +34,11 @@ func getPatternsMinTime(patterns []string) (m time.Time, err error) {
}
return
}
func getPatternsMaxTime(patterns []string) (m time.Time, err error) {
func getPatternsMaxTime(dir string, patterns []string) (m time.Time, err error) {
for _, p := range patterns {
if !filepath.IsAbs(p) {
p = filepath.Join(dir, p)
}
mp, err := getPatternMaxTime(p)
if err != nil {
return time.Time{}, err

View File

@@ -1,10 +0,0 @@
build:
binary: task
main: cmd/task/task.go
goos:
- windows
- darwin
- linux
goarch:
- 386
- amd64

16
help.go
View File

@@ -2,28 +2,28 @@ package task
import (
"fmt"
"os"
"sort"
"text/tabwriter"
)
func printExistingTasksHelp() {
tasks := tasksWithDesc()
// PrintTasksHelp prints help os tasks that have a description
func (e *Executor) PrintTasksHelp() {
tasks := e.tasksWithDesc()
if len(tasks) == 0 {
return
}
fmt.Println("Available tasks for this project:")
e.println("Available tasks for this project:")
// Format in tab-separated columns with a tab stop of 8.
w := tabwriter.NewWriter(os.Stdout, 0, 8, 0, '\t', 0)
w := tabwriter.NewWriter(e.Stdout, 0, 8, 0, '\t', 0)
for _, task := range tasks {
fmt.Fprintln(w, fmt.Sprintf("- %s:\t%s", task, Tasks[task].Desc))
fmt.Fprintln(w, fmt.Sprintf("* %s:\t%s", task, e.Tasks[task].Desc))
}
w.Flush()
}
func tasksWithDesc() (tasks []string) {
for name, task := range Tasks {
func (e *Executor) tasksWithDesc() (tasks []string) {
for name, task := range e.Tasks {
if task.Desc != "" {
tasks = append(tasks, name)
}

32
init.go Normal file
View File

@@ -0,0 +1,32 @@
package task
import (
"io/ioutil"
"log"
"os"
"path/filepath"
)
const defaultTaskfile = `# github.com/go-task/task
default:
cmds:
- echo "Hello, World!"
`
// InitTaskfile Taskfile creates a new Taskfile
func InitTaskfile(path string) error {
for _, f := range []string{"Taskfile.yml", "Taskfile.toml", "Taskfile.json"} {
f = filepath.Join(path, f)
if _, err := os.Stat(f); err == nil {
return ErrTaskfileAlreadyExists
}
}
f := filepath.Join(path, "Taskfile.yml")
if err := ioutil.WriteFile(f, []byte(defaultTaskfile), 0666); err != nil {
return err
}
log.Printf("Taskfile.yml created in the current directory")
return nil
}

25
log.go Normal file
View File

@@ -0,0 +1,25 @@
package task
import (
"fmt"
)
func (e *Executor) println(args ...interface{}) {
fmt.Fprintln(e.Stdout, args...)
}
func (e *Executor) printfln(format string, args ...interface{}) {
fmt.Fprintf(e.Stdout, format+"\n", args...)
}
func (e *Executor) verbosePrintln(args ...interface{}) {
if e.Verbose {
e.println(args...)
}
}
func (e *Executor) verbosePrintfln(format string, args ...interface{}) {
if e.Verbose {
e.printfln(format, args...)
}
}

View File

@@ -1,45 +0,0 @@
package task
import (
"encoding/json"
"fmt"
"io/ioutil"
"runtime"
"github.com/BurntSushi/toml"
"github.com/imdario/mergo"
"gopkg.in/yaml.v2"
)
func readTaskfile() (map[string]*Task, error) {
initialTasks, err := readTaskfileData(TaskFilePath)
if err != nil {
return nil, err
}
mergeTasks, err := readTaskfileData(fmt.Sprintf("%s_%s", TaskFilePath, runtime.GOOS))
if err != nil {
switch err.(type) {
default:
return nil, err
case taskFileNotFound:
return initialTasks, nil
}
}
if err := mergo.MapWithOverwrite(&initialTasks, mergeTasks); err != nil {
return nil, err
}
return initialTasks, nil
}
func readTaskfileData(path string) (tasks map[string]*Task, err error) {
if b, err := ioutil.ReadFile(path + ".yml"); err == nil {
return tasks, yaml.Unmarshal(b, &tasks)
}
if b, err := ioutil.ReadFile(path + ".json"); err == nil {
return tasks, json.Unmarshal(b, &tasks)
}
if b, err := ioutil.ReadFile(path + ".toml"); err == nil {
return tasks, toml.Unmarshal(b, &tasks)
}
return nil, taskFileNotFound{path}
}

423
task.go
View File

@@ -4,182 +4,266 @@ import (
"bytes"
"context"
"fmt"
"log"
"io"
"os"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"github.com/go-task/task/execext"
"github.com/spf13/pflag"
"golang.org/x/sync/errgroup"
)
var (
const (
// TaskFilePath is the default Taskfile
TaskFilePath = "Taskfile"
// Force (--force or -f flag) forces a task to run even when it's up-to-date
Force bool
// Watch (--watch or -w flag) enables watch of a task
Watch bool
// Tasks constains the tasks parsed from Taskfile
Tasks = make(map[string]*Task)
// MaximumTaskCall is the max number of times a task can be called.
// This exists to prevent infinite loops on cyclic dependencies
MaximumTaskCall = 100
)
// Executor executes a Taskfile
type Executor struct {
Tasks Tasks
Dir string
Force bool
Watch bool
Verbose bool
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
taskvars Vars
watchingFiles map[string]struct{}
dynamicCache map[string]string
muDynamicCache sync.Mutex
}
// Tasks representas a group of tasks
type Tasks map[string]*Task
// Task represents a task
type Task struct {
Cmds []string
Deps []string
Cmds []*Cmd
Deps []*Dep
Desc string
Sources []string
Generates []string
Status []string
Dir string
Vars map[string]string
Vars Vars
Set string
Env map[string]string
Env Vars
callCount int32
}
// Run runs Task
func Run() {
log.SetFlags(0)
args := pflag.Args()
if len(args) == 0 {
log.Println("task: No argument given, trying default task")
args = []string{"default"}
func (e *Executor) Run(args ...string) error {
if e.Stdin == nil {
e.Stdin = os.Stdin
}
if e.Stdout == nil {
e.Stdout = os.Stdout
}
if e.Stderr == nil {
e.Stderr = os.Stderr
}
var err error
Tasks, err = readTaskfile()
if err != nil {
log.Fatal(err)
}
if HasCyclicDep(Tasks) {
log.Fatal("Cyclic dependency detected")
if e.dynamicCache == nil {
e.dynamicCache = make(map[string]string, 10)
}
// check if given tasks exist
for _, a := range args {
if _, ok := Tasks[a]; !ok {
var err error = &taskNotFoundError{taskName: a}
fmt.Println(err)
printExistingTasksHelp()
return
if _, ok := e.Tasks[a]; !ok {
// FIXME: move to the main package
e.PrintTasksHelp()
return &taskNotFoundError{taskName: a}
}
}
if Watch {
if err := WatchTasks(args); err != nil {
log.Fatal(err)
}
return
}
for _, a := range args {
if err = RunTask(context.Background(), a); err != nil {
log.Fatal(err)
}
}
}
// RunTask runs a task by its name
func RunTask(ctx context.Context, name string) error {
t, ok := Tasks[name]
if !ok {
return &taskNotFoundError{name}
}
if err := t.runDeps(ctx); err != nil {
return err
}
if !Force && t.isUpToDate() {
log.Printf(`task: Task "%s" is up to date`, name)
return nil
}
for i := range t.Cmds {
if err := t.runCommand(ctx, i); err != nil {
return &taskRunError{name, err}
}
}
return nil
}
func (t *Task) runDeps(ctx context.Context) error {
vars, err := t.handleVariables()
if err != nil {
return err
}
g, ctx := errgroup.WithContext(ctx)
for _, d := range t.Deps {
dep := d
g.Go(func() error {
dep, err := ReplaceVariables(dep, vars)
if err != nil {
return err
}
if err = RunTask(ctx, dep); err != nil {
return err
}
return nil
})
}
if err = g.Wait(); err != nil {
return err
}
return nil
}
func (t *Task) isUpToDate() bool {
if len(t.Sources) == 0 || len(t.Generates) == 0 {
return false
}
sourcesMaxTime, err := getPatternsMaxTime(t.Sources)
if err != nil || sourcesMaxTime.IsZero() {
return false
}
generatesMinTime, err := getPatternsMinTime(t.Generates)
if err != nil || generatesMinTime.IsZero() {
return false
}
return generatesMinTime.After(sourcesMaxTime)
}
func (t *Task) runCommand(ctx context.Context, i int) error {
vars, err := t.handleVariables()
if err != nil {
return err
}
c, err := ReplaceVariables(t.Cmds[i], vars)
if err != nil {
return err
}
if strings.HasPrefix(c, "^") {
c = strings.TrimPrefix(c, "^")
if err = RunTask(ctx, c); err != nil {
if e.Watch {
if err := e.watchTasks(args...); err != nil {
return err
}
return nil
}
dir, err := ReplaceVariables(t.Dir, vars)
for _, a := range args {
if err := e.RunTask(context.Background(), Call{Task: a, Vars: e.taskvars}); err != nil {
return err
}
}
return nil
}
// RunTask runs a task by its name
func (e *Executor) RunTask(ctx context.Context, call Call) error {
t, ok := e.Tasks[call.Task]
if !ok {
return &taskNotFoundError{call.Task}
}
if atomic.AddInt32(&t.callCount, 1) >= MaximumTaskCall {
return &MaximumTaskCallExceededError{task: call.Task}
}
var err error
call.Vars, err = e.getVariables(call)
if err != nil {
return err
}
envs, err := t.getEnviron(vars)
if err := e.runDeps(ctx, call); err != nil {
return err
}
// FIXME: doing again, since a var may have been overriden
// using the `set:` attribute of a dependecy.
// Remove this when `set` (that is deprecated) be removed
call.Vars, err = e.getVariables(call)
if err != nil {
return err
}
if !e.Force {
upToDate, err := e.isTaskUpToDate(ctx, call)
if err != nil {
return err
}
if upToDate {
e.printfln(`task: Task "%s" is up to date`, call.Task)
return nil
}
}
for i := range t.Cmds {
if err := e.runCommand(ctx, call, i); err != nil {
return &taskRunError{call.Task, err}
}
}
return nil
}
func (e *Executor) runDeps(ctx context.Context, call Call) error {
g, ctx := errgroup.WithContext(ctx)
t := e.Tasks[call.Task]
for _, d := range t.Deps {
d := d
g.Go(func() error {
c, err := e.toCall(d.Task, d.Vars, call)
if err != nil {
return err
}
return e.RunTask(ctx, c)
})
}
return g.Wait()
}
func (e *Executor) isTaskUpToDate(ctx context.Context, call Call) (bool, error) {
t := e.Tasks[call.Task]
if len(t.Status) > 0 {
return e.isUpToDateStatus(ctx, call)
}
return e.isUpToDateTimestamp(ctx, call)
}
func (e *Executor) isUpToDateStatus(ctx context.Context, call Call) (bool, error) {
t := e.Tasks[call.Task]
environ, err := e.getEnviron(call)
if err != nil {
return false, err
}
dir, err := e.getTaskDir(call)
if err != nil {
return false, err
}
status, err := e.ReplaceSliceVariables(t.Status, call)
if err != nil {
return false, err
}
for _, s := range status {
err = execext.RunCommand(&execext.RunCommandOptions{
Context: ctx,
Command: s,
Dir: dir,
Env: environ,
})
if err != nil {
return false, nil
}
}
return true, nil
}
func (e *Executor) isUpToDateTimestamp(ctx context.Context, call Call) (bool, error) {
t := e.Tasks[call.Task]
if len(t.Sources) == 0 || len(t.Generates) == 0 {
return false, nil
}
dir, err := e.getTaskDir(call)
if err != nil {
return false, err
}
sources, err := e.ReplaceSliceVariables(t.Sources, call)
if err != nil {
return false, err
}
generates, err := e.ReplaceSliceVariables(t.Generates, call)
if err != nil {
return false, err
}
sourcesMaxTime, err := getPatternsMaxTime(dir, sources)
if err != nil || sourcesMaxTime.IsZero() {
return false, nil
}
generatesMinTime, err := getPatternsMinTime(dir, generates)
if err != nil || generatesMinTime.IsZero() {
return false, nil
}
return !generatesMinTime.Before(sourcesMaxTime), nil
}
func (e *Executor) runCommand(ctx context.Context, call Call, i int) error {
t := e.Tasks[call.Task]
cmd := t.Cmds[i]
if cmd.Cmd == "" {
c, err := e.toCall(cmd.Task, cmd.Vars, call)
if err != nil {
return err
}
return e.RunTask(ctx, c)
}
c, err := e.ReplaceVariables(cmd.Cmd, call)
if err != nil {
return err
}
dir, err := e.getTaskDir(call)
if err != nil {
return err
}
envs, err := e.getEnviron(call)
if err != nil {
return err
}
@@ -188,28 +272,59 @@ func (t *Task) runCommand(ctx context.Context, i int) error {
Command: c,
Dir: dir,
Env: envs,
Stdin: os.Stdin,
Stderr: os.Stderr,
Stdin: e.Stdin,
Stderr: e.Stderr,
}
if t.Set == "" {
log.Println(c)
opts.Stdout = os.Stdout
e.println(c)
if t.Set != "" {
var stdout bytes.Buffer
opts.Stdout = &stdout
if err = execext.RunCommand(opts); err != nil {
return err
}
} else {
buff := bytes.NewBuffer(nil)
opts.Stdout = buff
if err = execext.RunCommand(opts); err != nil {
return err
}
os.Setenv(t.Set, strings.TrimSpace(buff.String()))
return os.Setenv(t.Set, strings.TrimSpace(stdout.String()))
}
return nil
opts.Stdout = e.Stdout
return execext.RunCommand(opts)
}
func (t *Task) getEnviron(vars map[string]string) ([]string, error) {
func (e *Executor) toCall(task string, vs Vars, call Call) (Call, error) {
task, err := e.ReplaceVariables(task, call)
if err != nil {
return Call{}, err
}
newVars := make(Vars, len(vs))
for k, v := range vs {
static, err := e.ReplaceVariables(v.Static, call)
if err != nil {
return Call{}, err
}
sh, err := e.ReplaceVariables(v.Sh, call)
if err != nil {
return Call{}, err
}
newVars[k] = Var{Static: static, Sh: sh}
}
return Call{Task: task, Vars: newVars}, nil
}
func (e *Executor) getTaskDir(call Call) (string, error) {
t := e.Tasks[call.Task]
taskDir, err := e.ReplaceVariables(t.Dir, call)
if err != nil {
return "", err
}
return filepath.Join(e.Dir, taskDir), nil
}
func (e *Executor) getEnviron(call Call) ([]string, error) {
t := e.Tasks[call.Task]
if t.Env == nil {
return nil, nil
}
@@ -217,15 +332,11 @@ func (t *Task) getEnviron(vars map[string]string) ([]string, error) {
envs := os.Environ()
for k, v := range t.Env {
replacedValue, err := ReplaceVariables(v, vars)
env, err := e.ReplaceVariables(fmt.Sprintf("%s=%s", k, v), call)
if err != nil {
return nil, err
}
replacedKey, err := ReplaceVariables(k, vars)
if err != nil {
return nil, err
}
envs = append(envs, fmt.Sprintf("%s=%s", replacedKey, replacedValue))
envs = append(envs, env)
}
return envs, nil
}

View File

@@ -1,12 +1,17 @@
package task_test
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/go-task/task"
"github.com/stretchr/testify/assert"
)
func TestDeps(t *testing.T) {
@@ -31,12 +36,13 @@ func TestDeps(t *testing.T) {
_ = os.Remove(filepath.Join(dir, f))
}
c := exec.Command("task")
c.Dir = dir
if err := c.Run(); err != nil {
t.Error(err)
return
e := &task.Executor{
Dir: dir,
Stdout: ioutil.Discard,
Stderr: ioutil.Discard,
}
assert.NoError(t, e.ReadTaskfile())
assert.NoError(t, e.Run("default"))
for _, f := range files {
f = filepath.Join(dir, f)
@@ -55,8 +61,10 @@ func TestVars(t *testing.T) {
}{
{"foo.txt", "foo"},
{"bar.txt", "bar"},
{"baz.txt", "baz"},
{"foo2.txt", "foo2"},
{"bar2.txt", "bar2"},
{"baz2.txt", "baz2"},
{"equal.txt", "foo=bar"},
}
@@ -64,13 +72,13 @@ func TestVars(t *testing.T) {
_ = os.Remove(filepath.Join(dir, f.file))
}
c := exec.Command("task")
c.Dir = dir
if err := c.Run(); err != nil {
t.Error(err)
return
e := &task.Executor{
Dir: dir,
Stdout: ioutil.Discard,
Stderr: ioutil.Discard,
}
assert.NoError(t, e.ReadTaskfile())
assert.NoError(t, e.Run("default"))
for _, f := range files {
d, err := ioutil.ReadFile(filepath.Join(dir, f.file))
@@ -98,13 +106,13 @@ func TestTaskCall(t *testing.T) {
_ = os.Remove(filepath.Join(dir, f))
}
c := exec.Command("task")
c.Dir = dir
if err := c.Run(); err != nil {
t.Error(err)
return
e := &task.Executor{
Dir: dir,
Stdout: ioutil.Discard,
Stderr: ioutil.Discard,
}
assert.NoError(t, e.ReadTaskfile())
assert.NoError(t, e.Run("default"))
for _, f := range files {
if _, err := os.Stat(filepath.Join(dir, f)); err != nil {
@@ -112,3 +120,154 @@ func TestTaskCall(t *testing.T) {
}
}
}
func TestStatus(t *testing.T) {
const dir = "testdata/status"
var file = filepath.Join(dir, "foo.txt")
_ = os.Remove(file)
if _, err := os.Stat(file); err == nil {
t.Errorf("File should not exists: %v", err)
}
e := &task.Executor{
Dir: dir,
Stdout: ioutil.Discard,
Stderr: ioutil.Discard,
}
assert.NoError(t, e.ReadTaskfile())
assert.NoError(t, e.Run("gen-foo"))
if _, err := os.Stat(file); err != nil {
t.Errorf("File should exists: %v", err)
}
buff := bytes.NewBuffer(nil)
e.Stdout, e.Stderr = buff, buff
assert.NoError(t, e.Run("gen-foo"))
if buff.String() != `task: Task "gen-foo" is up to date`+"\n" {
t.Errorf("Wrong output message: %s", buff.String())
}
}
func TestGenerates(t *testing.T) {
var srcTask = "sub/src.txt"
var relTask = "rel.txt"
var absTask = "abs.txt"
// This test does not work with a relative dir.
dir, err := filepath.Abs("testdata/generates")
assert.NoError(t, err)
var srcFile = filepath.Join(dir, srcTask)
for _, task := range []string{srcTask, relTask, absTask} {
path := filepath.Join(dir, task)
_ = os.Remove(path)
if _, err := os.Stat(path); err == nil {
t.Errorf("File should not exists: %v", err)
}
}
buff := bytes.NewBuffer(nil)
e := &task.Executor{
Dir: dir,
Stdout: buff,
Stderr: buff,
}
assert.NoError(t, e.ReadTaskfile())
for _, task := range []string{relTask, absTask} {
var destFile = filepath.Join(dir, task)
var upToDate = fmt.Sprintf("task: Task \"%s\" is up to date\n", srcTask) +
fmt.Sprintf("task: Task \"%s\" is up to date\n", task)
// Run task for the first time.
assert.NoError(t, e.Run(task))
if _, err := os.Stat(srcFile); err != nil {
t.Errorf("File should exists: %v", err)
}
if _, err := os.Stat(destFile); err != nil {
t.Errorf("File should exists: %v", err)
}
// Ensure task was not incorrectly found to be up-to-date on first run.
if buff.String() == upToDate {
t.Errorf("Wrong output message: %s", buff.String())
}
buff.Reset()
// Re-run task to ensure it's now found to be up-to-date.
assert.NoError(t, e.Run(task))
if buff.String() != upToDate {
t.Errorf("Wrong output message: %s", buff.String())
}
buff.Reset()
}
}
func TestInit(t *testing.T) {
const dir = "testdata/init"
var file = filepath.Join(dir, "Taskfile.yml")
_ = os.Remove(file)
if _, err := os.Stat(file); err == nil {
t.Errorf("Taskfile.yml should not exists")
}
if err := task.InitTaskfile(dir); err != nil {
t.Error(err)
}
if _, err := os.Stat(file); err != nil {
t.Errorf("Taskfile.yml should exists")
}
}
func TestParams(t *testing.T) {
const dir = "testdata/params"
var files = []struct {
file string
content string
}{
{"hello.txt", "Hello\n"},
{"world.txt", "World\n"},
{"exclamation.txt", "!\n"},
{"dep1.txt", "Dependence1\n"},
{"dep2.txt", "Dependence2\n"},
{"spanish.txt", "¡Holla mundo!\n"},
{"spanish-dep.txt", "¡Holla dependencia!\n"},
{"portuguese.txt", "Olá, mundo!\n"},
}
for _, f := range files {
_ = os.Remove(filepath.Join(dir, f.file))
}
e := task.Executor{
Dir: dir,
Stdout: ioutil.Discard,
Stderr: ioutil.Discard,
}
assert.NoError(t, e.ReadTaskfile())
assert.NoError(t, e.Run("default"))
for _, f := range files {
content, err := ioutil.ReadFile(filepath.Join(dir, f.file))
assert.NoError(t, err)
assert.Equal(t, f.content, string(content))
}
}
func TestCyclicDep(t *testing.T) {
const dir = "testdata/cyclic"
e := task.Executor{
Dir: dir,
Stdout: ioutil.Discard,
Stderr: ioutil.Discard,
}
assert.NoError(t, e.ReadTaskfile())
assert.IsType(t, &task.MaximumTaskCallExceededError{}, e.Run("task-1"))
}

66
taskfile.go Normal file
View File

@@ -0,0 +1,66 @@
package task
import (
"fmt"
"io/ioutil"
"path/filepath"
"runtime"
"github.com/imdario/mergo"
"gopkg.in/yaml.v2"
)
// ReadTaskfile parses Taskfile from the disk
func (e *Executor) ReadTaskfile() error {
path := filepath.Join(e.Dir, TaskFilePath)
var err error
e.Tasks, err = e.readTaskfileData(path)
if err != nil {
return err
}
osTasks, err := e.readTaskfileData(fmt.Sprintf("%s_%s", path, runtime.GOOS))
if err != nil {
switch err.(type) {
case taskFileNotFound:
default:
return err
}
}
if err := mergo.MapWithOverwrite(&e.Tasks, osTasks); err != nil {
return err
}
return e.readTaskvars()
}
func (e *Executor) readTaskfileData(path string) (tasks map[string]*Task, err error) {
if b, err := ioutil.ReadFile(path + ".yml"); err == nil {
return tasks, yaml.UnmarshalStrict(b, &tasks)
}
return nil, taskFileNotFound{path}
}
func (e *Executor) readTaskvars() error {
var (
file = filepath.Join(e.Dir, TaskvarsFilePath)
osSpecificFile = fmt.Sprintf("%s_%s", file, runtime.GOOS)
)
if b, err := ioutil.ReadFile(file + ".yml"); err == nil {
if err := yaml.UnmarshalStrict(b, &e.taskvars); err != nil {
return err
}
}
if b, err := ioutil.ReadFile(osSpecificFile + ".yml"); err == nil {
osTaskvars := make(Vars, 10)
if err := yaml.UnmarshalStrict(b, &osTaskvars); err != nil {
return err
}
for k, v := range osTaskvars {
e.taskvars[k] = v
}
}
return nil
}

7
testdata/cyclic/Taskfile.yml vendored Normal file
View File

@@ -0,0 +1,7 @@
task-1:
deps:
- task: task-2
task-2:
deps:
- task: task-1

1
testdata/generates/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*.txt

31
testdata/generates/Taskfile.yml vendored Normal file
View File

@@ -0,0 +1,31 @@
abs.txt:
desc: generates dest file based on absolute paths
deps:
- sub/src.txt
dir: sub
cmds:
- cat src.txt > '{{.BUILD_DIR}}/abs.txt'
sources:
- src.txt
generates:
- "{{.BUILD_DIR}}/abs.txt"
rel.txt:
desc: generates dest file based on relative paths
deps:
- sub/src.txt
dir: sub
cmds:
- cat src.txt > '../rel.txt'
sources:
- src.txt
generates:
- "../rel.txt"
sub/src.txt:
desc: generate source file
cmds:
- mkdir -p sub
- echo "hello world" > sub/src.txt
status:
- test -f sub/src.txt

1
testdata/generates/Taskvars.yml vendored Normal file
View File

@@ -0,0 +1 @@
BUILD_DIR: $pwd

1
testdata/init/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*.yml

1
testdata/params/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*.txt

26
testdata/params/Taskfile.yml vendored Normal file
View File

@@ -0,0 +1,26 @@
default:
vars:
SPANISH: ¡Holla mundo!
PORTUGUESE: "{{.PORTUGUESE}}"
deps:
- task: write-file
vars: {CONTENT: Dependence1, FILE: dep1.txt}
- task: write-file
vars: {CONTENT: Dependence2, FILE: dep2.txt}
- task: write-file
vars: {CONTENT: "{{.SPANISH|replace \"mundo\" \"dependencia\"}}", FILE: spanish-dep.txt}
cmds:
- task: write-file
vars: {CONTENT: Hello, FILE: hello.txt}
- task: write-file
vars: {CONTENT: "$echo 'World'", FILE: world.txt}
- task: write-file
vars: {CONTENT: "!", FILE: exclamation.txt}
- task: write-file
vars: {CONTENT: "{{.SPANISH}}", FILE: spanish.txt}
- task: write-file
vars: {CONTENT: "{{.PORTUGUESE}}", FILE: portuguese.txt}
write-file:
cmds:
- echo {{.CONTENT}} > {{.FILE}}

1
testdata/params/Taskvars.yml vendored Normal file
View File

@@ -0,0 +1 @@
PORTUGUESE: Olá, mundo!

1
testdata/status/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*.txt

5
testdata/status/Taskfile.yml vendored Normal file
View File

@@ -0,0 +1,5 @@
gen-foo:
cmds:
- touch foo.txt
status:
- test -f foo.txt

View File

@@ -6,12 +6,16 @@ hello:
cmds:
- echo {{.FOO}} > foo.txt
- echo {{.BAR}} > bar.txt
- echo {{.BAZ}} > baz.txt
- echo {{.FOO2}} > foo2.txt
- echo {{.BAR2}} > bar2.txt
- echo {{.BAZ2}} > baz2.txt
- echo {{.EQUAL}} > equal.txt
vars:
FOO: foo
BAR: $echo bar
BAZ:
sh: echo baz
set-equal:
set: EQUAL

View File

@@ -1,2 +1,4 @@
FOO2: foo2
BAR2: $echo bar2
BAZ2:
sh: echo baz2

View File

@@ -1,157 +0,0 @@
package task
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"text/template"
"github.com/go-task/task/execext"
"github.com/BurntSushi/toml"
"github.com/Masterminds/sprig"
"gopkg.in/yaml.v2"
)
var (
// TaskvarsFilePath file containing additional variables
TaskvarsFilePath = "Taskvars"
// ErrMultilineResultCmd is returned when a command returns multiline result
ErrMultilineResultCmd = errors.New("Got multiline result from command")
)
var varCmds = make(map[string]string)
func handleDynamicVariableContent(value string) (string, error) {
if value == "" || value[0] != '$' {
return value, nil
}
if result, ok := varCmds[value]; ok {
return result, nil
}
buff := bytes.NewBuffer(nil)
opts := &execext.RunCommandOptions{
Command: strings.TrimPrefix(value, "$"),
Stdout: buff,
Stderr: os.Stderr,
}
if err := execext.RunCommand(opts); err != nil {
return "", err
}
result := buff.String()
result = strings.TrimSuffix(result, "\n")
if strings.ContainsRune(result, '\n') {
return "", ErrMultilineResultCmd
}
result = strings.TrimSpace(result)
varCmds[value] = result
return result, nil
}
func (t *Task) handleVariables() (map[string]string, error) {
localVariables := make(map[string]string)
for key, value := range t.Vars {
val, err := handleDynamicVariableContent(value)
if err != nil {
return nil, err
}
localVariables[key] = val
}
if fileVariables, err := readTaskvarsFile(); err == nil {
for key, value := range fileVariables {
val, err := handleDynamicVariableContent(value)
if err != nil {
return nil, err
}
localVariables[key] = val
}
} else {
return nil, err
}
for key, value := range getEnvironmentVariables() {
localVariables[key] = value
}
return localVariables, nil
}
var templateFuncs template.FuncMap
func init() {
taskFuncs := template.FuncMap{
"OS": func() string { return runtime.GOOS },
"ARCH": func() string { return runtime.GOARCH },
// historical reasons
"IsSH": func() bool { return true },
"FromSlash": func(path string) string {
return filepath.FromSlash(path)
},
"ToSlash": func(path string) string {
return filepath.ToSlash(path)
},
}
templateFuncs = sprig.TxtFuncMap()
for k, v := range taskFuncs {
templateFuncs[k] = v
}
}
// ReplaceVariables writes vars into initial string
func ReplaceVariables(initial string, vars map[string]string) (string, error) {
t, err := template.New("").Funcs(templateFuncs).Parse(initial)
if err != nil {
return "", err
}
b := bytes.NewBuffer(nil)
if err = t.Execute(b, vars); err != nil {
return "", err
}
return b.String(), nil
}
// GetEnvironmentVariables returns environment variables as map
func getEnvironmentVariables() map[string]string {
var (
env = os.Environ()
m = make(map[string]string, len(env))
)
for _, e := range env {
keyVal := strings.SplitN(e, "=", 2)
key, val := keyVal[0], keyVal[1]
m[key] = val
}
return m
}
func readTaskvarsFile() (map[string]string, error) {
var variables map[string]string
if b, err := ioutil.ReadFile(TaskvarsFilePath + ".yml"); err == nil {
if err := yaml.Unmarshal(b, &variables); err != nil {
return nil, err
}
return variables, nil
}
if b, err := ioutil.ReadFile(TaskvarsFilePath + ".json"); err == nil {
if err := json.Unmarshal(b, &variables); err != nil {
return nil, err
}
return variables, nil
}
if b, err := ioutil.ReadFile(TaskvarsFilePath + ".toml"); err == nil {
if err := toml.Unmarshal(b, &variables); err != nil {
return nil, err
}
return variables, nil
}
return variables, nil
}

219
variables.go Normal file
View File

@@ -0,0 +1,219 @@
package task
import (
"bytes"
"errors"
"os"
"path/filepath"
"runtime"
"strings"
"text/template"
"github.com/go-task/task/execext"
"github.com/Masterminds/sprig"
)
var (
// TaskvarsFilePath file containing additional variables
TaskvarsFilePath = "Taskvars"
// ErrMultilineResultCmd is returned when a command returns multiline result
ErrMultilineResultCmd = errors.New("Got multiline result from command")
)
// Vars is a string[string] variables map
type Vars map[string]Var
// Var represents either a static or dynamic variable
type Var struct {
Static string
Sh string
}
func (vs Vars) toStringMap() (m map[string]string) {
m = make(map[string]string, len(vs))
for k, v := range vs {
m[k] = v.Static
}
return
}
var (
// ErrCantUnmarshalVar is returned for invalid var YAML
ErrCantUnmarshalVar = errors.New("task: can't unmarshal var value")
)
// UnmarshalYAML implements yaml.Unmarshaler interface
func (v *Var) UnmarshalYAML(unmarshal func(interface{}) error) error {
var str string
if err := unmarshal(&str); err == nil {
if strings.HasPrefix(str, "$") {
v.Sh = strings.TrimPrefix(str, "$")
} else {
v.Static = str
}
return nil
}
var sh struct {
Sh string
}
if err := unmarshal(&sh); err == nil {
v.Sh = sh.Sh
return nil
}
return ErrCantUnmarshalVar
}
var (
templateFuncs template.FuncMap
)
func init() {
taskFuncs := template.FuncMap{
"OS": func() string { return runtime.GOOS },
"ARCH": func() string { return runtime.GOARCH },
// historical reasons
"IsSH": func() bool { return true },
"FromSlash": func(path string) string {
return filepath.FromSlash(path)
},
"ToSlash": func(path string) string {
return filepath.ToSlash(path)
},
"ExeExt": func() string {
if runtime.GOOS == "windows" {
return ".exe"
}
return ""
},
}
templateFuncs = sprig.TxtFuncMap()
for k, v := range taskFuncs {
templateFuncs[k] = v
}
}
// ReplaceVariables writes vars into initial string
func (e *Executor) ReplaceVariables(initial string, call Call) (string, error) {
if initial == "" {
return initial, nil
}
templ, err := template.New("").Funcs(templateFuncs).Parse(initial)
if err != nil {
return "", err
}
var b bytes.Buffer
if err = templ.Execute(&b, call.Vars.toStringMap()); err != nil {
return "", err
}
return b.String(), nil
}
// ReplaceSliceVariables writes vars into initial string slice
func (e *Executor) ReplaceSliceVariables(initials []string, call Call) ([]string, error) {
result := make([]string, len(initials))
for i, s := range initials {
var err error
result[i], err = e.ReplaceVariables(s, call)
if err != nil {
return nil, err
}
}
return result, nil
}
func (e *Executor) getVariables(call Call) (Vars, error) {
t := e.Tasks[call.Task]
result := make(Vars, len(t.Vars)+len(e.taskvars)+len(call.Vars))
merge := func(vars Vars, runTemplate bool) error {
for k, v := range vars {
if runTemplate {
var err error
v.Static, err = e.ReplaceVariables(v.Static, call)
if err != nil {
return err
}
v.Sh, err = e.ReplaceVariables(v.Sh, call)
if err != nil {
return err
}
}
v, err := e.handleDynamicVariableContent(v)
if err != nil {
return err
}
result[k] = Var{Static: v}
}
return nil
}
if err := merge(e.taskvars, false); err != nil {
return nil, err
}
if err := merge(t.Vars, true); err != nil {
return nil, err
}
if err := merge(getEnvironmentVariables(), false); err != nil {
return nil, err
}
if err := merge(call.Vars, false); err != nil {
return nil, err
}
return result, nil
}
// GetEnvironmentVariables returns environment variables as map
func getEnvironmentVariables() Vars {
var (
env = os.Environ()
m = make(Vars, len(env))
)
for _, e := range env {
keyVal := strings.SplitN(e, "=", 2)
key, val := keyVal[0], keyVal[1]
m[key] = Var{Static: val}
}
return m
}
func (e *Executor) handleDynamicVariableContent(v Var) (string, error) {
if v.Static != "" {
return v.Static, nil
}
e.muDynamicCache.Lock()
defer e.muDynamicCache.Unlock()
if result, ok := e.dynamicCache[v.Sh]; ok {
return result, nil
}
var stdout bytes.Buffer
opts := &execext.RunCommandOptions{
Command: v.Sh,
Dir: e.Dir,
Stdout: &stdout,
Stderr: e.Stderr,
}
if err := execext.RunCommand(opts); err != nil {
return "", &dynamicVarError{cause: err, cmd: opts.Command}
}
result := strings.TrimSuffix(stdout.String(), "\n")
if strings.ContainsRune(result, '\n') {
return "", ErrMultilineResultCmd
}
result = strings.TrimSpace(result)
e.verbosePrintfln(`task: dynamic variable: "%s", result: "%s"`, v.Sh, result)
e.dynamicCache[v.Sh] = result
return result, nil
}

View File

@@ -1,3 +0,0 @@
Compatible with TOML version
[v0.2.0](https://github.com/mojombo/toml/blob/master/versions/toml-v0.2.0.md)

View File

@@ -1,14 +0,0 @@
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.

View File

@@ -1,19 +0,0 @@
install:
go install ./...
test: install
go test -v
toml-test toml-test-decoder
toml-test -encoder toml-test-encoder
fmt:
gofmt -w *.go */*.go
colcheck *.go */*.go
tags:
find ./ -name '*.go' -print0 | xargs -0 gotags > TAGS
push:
git push origin master
git push github master

View File

@@ -1,220 +0,0 @@
## TOML parser and encoder for Go with reflection
TOML stands for Tom's Obvious, Minimal Language. This Go package provides a
reflection interface similar to Go's standard library `json` and `xml`
packages. This package also supports the `encoding.TextUnmarshaler` and
`encoding.TextMarshaler` interfaces so that you can define custom data
representations. (There is an example of this below.)
Spec: https://github.com/mojombo/toml
Compatible with TOML version
[v0.2.0](https://github.com/toml-lang/toml/blob/master/versions/en/toml-v0.2.0.md)
Documentation: http://godoc.org/github.com/BurntSushi/toml
Installation:
```bash
go get github.com/BurntSushi/toml
```
Try the toml validator:
```bash
go get github.com/BurntSushi/toml/cmd/tomlv
tomlv some-toml-file.toml
```
[![Build status](https://api.travis-ci.org/BurntSushi/toml.png)](https://travis-ci.org/BurntSushi/toml)
### Testing
This package passes all tests in
[toml-test](https://github.com/BurntSushi/toml-test) for both the decoder
and the encoder.
### Examples
This package works similarly to how the Go standard library handles `XML`
and `JSON`. Namely, data is loaded into Go values via reflection.
For the simplest example, consider some TOML file as just a list of keys
and values:
```toml
Age = 25
Cats = [ "Cauchy", "Plato" ]
Pi = 3.14
Perfection = [ 6, 28, 496, 8128 ]
DOB = 1987-07-05T05:45:00Z
```
Which could be defined in Go as:
```go
type Config struct {
Age int
Cats []string
Pi float64
Perfection []int
DOB time.Time // requires `import time`
}
```
And then decoded with:
```go
var conf Config
if _, err := toml.Decode(tomlData, &conf); err != nil {
// handle error
}
```
You can also use struct tags if your struct field name doesn't map to a TOML
key value directly:
```toml
some_key_NAME = "wat"
```
```go
type TOML struct {
ObscureKey string `toml:"some_key_NAME"`
}
```
### Using the `encoding.TextUnmarshaler` interface
Here's an example that automatically parses duration strings into
`time.Duration` values:
```toml
[[song]]
name = "Thunder Road"
duration = "4m49s"
[[song]]
name = "Stairway to Heaven"
duration = "8m03s"
```
Which can be decoded with:
```go
type song struct {
Name string
Duration duration
}
type songs struct {
Song []song
}
var favorites songs
if _, err := toml.Decode(blob, &favorites); err != nil {
log.Fatal(err)
}
for _, s := range favorites.Song {
fmt.Printf("%s (%s)\n", s.Name, s.Duration)
}
```
And you'll also need a `duration` type that satisfies the
`encoding.TextUnmarshaler` interface:
```go
type duration struct {
time.Duration
}
func (d *duration) UnmarshalText(text []byte) error {
var err error
d.Duration, err = time.ParseDuration(string(text))
return err
}
```
### More complex usage
Here's an example of how to load the example from the official spec page:
```toml
# This is a TOML document. Boom.
title = "TOML Example"
[owner]
name = "Tom Preston-Werner"
organization = "GitHub"
bio = "GitHub Cofounder & CEO\nLikes tater tots and beer."
dob = 1979-05-27T07:32:00Z # First class dates? Why not?
[database]
server = "192.168.1.1"
ports = [ 8001, 8001, 8002 ]
connection_max = 5000
enabled = true
[servers]
# You can indent as you please. Tabs or spaces. TOML don't care.
[servers.alpha]
ip = "10.0.0.1"
dc = "eqdc10"
[servers.beta]
ip = "10.0.0.2"
dc = "eqdc10"
[clients]
data = [ ["gamma", "delta"], [1, 2] ] # just an update to make sure parsers support it
# Line breaks are OK when inside arrays
hosts = [
"alpha",
"omega"
]
```
And the corresponding Go types are:
```go
type tomlConfig struct {
Title string
Owner ownerInfo
DB database `toml:"database"`
Servers map[string]server
Clients clients
}
type ownerInfo struct {
Name string
Org string `toml:"organization"`
Bio string
DOB time.Time
}
type database struct {
Server string
Ports []int
ConnMax int `toml:"connection_max"`
Enabled bool
}
type server struct {
IP string
DC string
}
type clients struct {
Data [][]interface{}
Hosts []string
}
```
Note that a case insensitive match will be tried if an exact match can't be
found.
A working example of the above can be found in `_examples/example.{go,toml}`.

View File

@@ -1,509 +0,0 @@
package toml
import (
"fmt"
"io"
"io/ioutil"
"math"
"reflect"
"strings"
"time"
)
func e(format string, args ...interface{}) error {
return fmt.Errorf("toml: "+format, args...)
}
// Unmarshaler is the interface implemented by objects that can unmarshal a
// TOML description of themselves.
type Unmarshaler interface {
UnmarshalTOML(interface{}) error
}
// Unmarshal decodes the contents of `p` in TOML format into a pointer `v`.
func Unmarshal(p []byte, v interface{}) error {
_, err := Decode(string(p), v)
return err
}
// Primitive is a TOML value that hasn't been decoded into a Go value.
// When using the various `Decode*` functions, the type `Primitive` may
// be given to any value, and its decoding will be delayed.
//
// A `Primitive` value can be decoded using the `PrimitiveDecode` function.
//
// The underlying representation of a `Primitive` value is subject to change.
// Do not rely on it.
//
// N.B. Primitive values are still parsed, so using them will only avoid
// the overhead of reflection. They can be useful when you don't know the
// exact type of TOML data until run time.
type Primitive struct {
undecoded interface{}
context Key
}
// DEPRECATED!
//
// Use MetaData.PrimitiveDecode instead.
func PrimitiveDecode(primValue Primitive, v interface{}) error {
md := MetaData{decoded: make(map[string]bool)}
return md.unify(primValue.undecoded, rvalue(v))
}
// PrimitiveDecode is just like the other `Decode*` functions, except it
// decodes a TOML value that has already been parsed. Valid primitive values
// can *only* be obtained from values filled by the decoder functions,
// including this method. (i.e., `v` may contain more `Primitive`
// values.)
//
// Meta data for primitive values is included in the meta data returned by
// the `Decode*` functions with one exception: keys returned by the Undecoded
// method will only reflect keys that were decoded. Namely, any keys hidden
// behind a Primitive will be considered undecoded. Executing this method will
// update the undecoded keys in the meta data. (See the example.)
func (md *MetaData) PrimitiveDecode(primValue Primitive, v interface{}) error {
md.context = primValue.context
defer func() { md.context = nil }()
return md.unify(primValue.undecoded, rvalue(v))
}
// Decode will decode the contents of `data` in TOML format into a pointer
// `v`.
//
// TOML hashes correspond to Go structs or maps. (Dealer's choice. They can be
// used interchangeably.)
//
// TOML arrays of tables correspond to either a slice of structs or a slice
// of maps.
//
// TOML datetimes correspond to Go `time.Time` values.
//
// All other TOML types (float, string, int, bool and array) correspond
// to the obvious Go types.
//
// An exception to the above rules is if a type implements the
// encoding.TextUnmarshaler interface. In this case, any primitive TOML value
// (floats, strings, integers, booleans and datetimes) will be converted to
// a byte string and given to the value's UnmarshalText method. See the
// Unmarshaler example for a demonstration with time duration strings.
//
// Key mapping
//
// TOML keys can map to either keys in a Go map or field names in a Go
// struct. The special `toml` struct tag may be used to map TOML keys to
// struct fields that don't match the key name exactly. (See the example.)
// A case insensitive match to struct names will be tried if an exact match
// can't be found.
//
// The mapping between TOML values and Go values is loose. That is, there
// may exist TOML values that cannot be placed into your representation, and
// there may be parts of your representation that do not correspond to
// TOML values. This loose mapping can be made stricter by using the IsDefined
// and/or Undecoded methods on the MetaData returned.
//
// This decoder will not handle cyclic types. If a cyclic type is passed,
// `Decode` will not terminate.
func Decode(data string, v interface{}) (MetaData, error) {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Ptr {
return MetaData{}, e("Decode of non-pointer %s", reflect.TypeOf(v))
}
if rv.IsNil() {
return MetaData{}, e("Decode of nil %s", reflect.TypeOf(v))
}
p, err := parse(data)
if err != nil {
return MetaData{}, err
}
md := MetaData{
p.mapping, p.types, p.ordered,
make(map[string]bool, len(p.ordered)), nil,
}
return md, md.unify(p.mapping, indirect(rv))
}
// DecodeFile is just like Decode, except it will automatically read the
// contents of the file at `fpath` and decode it for you.
func DecodeFile(fpath string, v interface{}) (MetaData, error) {
bs, err := ioutil.ReadFile(fpath)
if err != nil {
return MetaData{}, err
}
return Decode(string(bs), v)
}
// DecodeReader is just like Decode, except it will consume all bytes
// from the reader and decode it for you.
func DecodeReader(r io.Reader, v interface{}) (MetaData, error) {
bs, err := ioutil.ReadAll(r)
if err != nil {
return MetaData{}, err
}
return Decode(string(bs), v)
}
// unify performs a sort of type unification based on the structure of `rv`,
// which is the client representation.
//
// Any type mismatch produces an error. Finding a type that we don't know
// how to handle produces an unsupported type error.
func (md *MetaData) unify(data interface{}, rv reflect.Value) error {
// Special case. Look for a `Primitive` value.
if rv.Type() == reflect.TypeOf((*Primitive)(nil)).Elem() {
// Save the undecoded data and the key context into the primitive
// value.
context := make(Key, len(md.context))
copy(context, md.context)
rv.Set(reflect.ValueOf(Primitive{
undecoded: data,
context: context,
}))
return nil
}
// Special case. Unmarshaler Interface support.
if rv.CanAddr() {
if v, ok := rv.Addr().Interface().(Unmarshaler); ok {
return v.UnmarshalTOML(data)
}
}
// Special case. Handle time.Time values specifically.
// TODO: Remove this code when we decide to drop support for Go 1.1.
// This isn't necessary in Go 1.2 because time.Time satisfies the encoding
// interfaces.
if rv.Type().AssignableTo(rvalue(time.Time{}).Type()) {
return md.unifyDatetime(data, rv)
}
// Special case. Look for a value satisfying the TextUnmarshaler interface.
if v, ok := rv.Interface().(TextUnmarshaler); ok {
return md.unifyText(data, v)
}
// BUG(burntsushi)
// The behavior here is incorrect whenever a Go type satisfies the
// encoding.TextUnmarshaler interface but also corresponds to a TOML
// hash or array. In particular, the unmarshaler should only be applied
// to primitive TOML values. But at this point, it will be applied to
// all kinds of values and produce an incorrect error whenever those values
// are hashes or arrays (including arrays of tables).
k := rv.Kind()
// laziness
if k >= reflect.Int && k <= reflect.Uint64 {
return md.unifyInt(data, rv)
}
switch k {
case reflect.Ptr:
elem := reflect.New(rv.Type().Elem())
err := md.unify(data, reflect.Indirect(elem))
if err != nil {
return err
}
rv.Set(elem)
return nil
case reflect.Struct:
return md.unifyStruct(data, rv)
case reflect.Map:
return md.unifyMap(data, rv)
case reflect.Array:
return md.unifyArray(data, rv)
case reflect.Slice:
return md.unifySlice(data, rv)
case reflect.String:
return md.unifyString(data, rv)
case reflect.Bool:
return md.unifyBool(data, rv)
case reflect.Interface:
// we only support empty interfaces.
if rv.NumMethod() > 0 {
return e("unsupported type %s", rv.Type())
}
return md.unifyAnything(data, rv)
case reflect.Float32:
fallthrough
case reflect.Float64:
return md.unifyFloat64(data, rv)
}
return e("unsupported type %s", rv.Kind())
}
func (md *MetaData) unifyStruct(mapping interface{}, rv reflect.Value) error {
tmap, ok := mapping.(map[string]interface{})
if !ok {
if mapping == nil {
return nil
}
return e("type mismatch for %s: expected table but found %T",
rv.Type().String(), mapping)
}
for key, datum := range tmap {
var f *field
fields := cachedTypeFields(rv.Type())
for i := range fields {
ff := &fields[i]
if ff.name == key {
f = ff
break
}
if f == nil && strings.EqualFold(ff.name, key) {
f = ff
}
}
if f != nil {
subv := rv
for _, i := range f.index {
subv = indirect(subv.Field(i))
}
if isUnifiable(subv) {
md.decoded[md.context.add(key).String()] = true
md.context = append(md.context, key)
if err := md.unify(datum, subv); err != nil {
return err
}
md.context = md.context[0 : len(md.context)-1]
} else if f.name != "" {
// Bad user! No soup for you!
return e("cannot write unexported field %s.%s",
rv.Type().String(), f.name)
}
}
}
return nil
}
func (md *MetaData) unifyMap(mapping interface{}, rv reflect.Value) error {
tmap, ok := mapping.(map[string]interface{})
if !ok {
if tmap == nil {
return nil
}
return badtype("map", mapping)
}
if rv.IsNil() {
rv.Set(reflect.MakeMap(rv.Type()))
}
for k, v := range tmap {
md.decoded[md.context.add(k).String()] = true
md.context = append(md.context, k)
rvkey := indirect(reflect.New(rv.Type().Key()))
rvval := reflect.Indirect(reflect.New(rv.Type().Elem()))
if err := md.unify(v, rvval); err != nil {
return err
}
md.context = md.context[0 : len(md.context)-1]
rvkey.SetString(k)
rv.SetMapIndex(rvkey, rvval)
}
return nil
}
func (md *MetaData) unifyArray(data interface{}, rv reflect.Value) error {
datav := reflect.ValueOf(data)
if datav.Kind() != reflect.Slice {
if !datav.IsValid() {
return nil
}
return badtype("slice", data)
}
sliceLen := datav.Len()
if sliceLen != rv.Len() {
return e("expected array length %d; got TOML array of length %d",
rv.Len(), sliceLen)
}
return md.unifySliceArray(datav, rv)
}
func (md *MetaData) unifySlice(data interface{}, rv reflect.Value) error {
datav := reflect.ValueOf(data)
if datav.Kind() != reflect.Slice {
if !datav.IsValid() {
return nil
}
return badtype("slice", data)
}
n := datav.Len()
if rv.IsNil() || rv.Cap() < n {
rv.Set(reflect.MakeSlice(rv.Type(), n, n))
}
rv.SetLen(n)
return md.unifySliceArray(datav, rv)
}
func (md *MetaData) unifySliceArray(data, rv reflect.Value) error {
sliceLen := data.Len()
for i := 0; i < sliceLen; i++ {
v := data.Index(i).Interface()
sliceval := indirect(rv.Index(i))
if err := md.unify(v, sliceval); err != nil {
return err
}
}
return nil
}
func (md *MetaData) unifyDatetime(data interface{}, rv reflect.Value) error {
if _, ok := data.(time.Time); ok {
rv.Set(reflect.ValueOf(data))
return nil
}
return badtype("time.Time", data)
}
func (md *MetaData) unifyString(data interface{}, rv reflect.Value) error {
if s, ok := data.(string); ok {
rv.SetString(s)
return nil
}
return badtype("string", data)
}
func (md *MetaData) unifyFloat64(data interface{}, rv reflect.Value) error {
if num, ok := data.(float64); ok {
switch rv.Kind() {
case reflect.Float32:
fallthrough
case reflect.Float64:
rv.SetFloat(num)
default:
panic("bug")
}
return nil
}
return badtype("float", data)
}
func (md *MetaData) unifyInt(data interface{}, rv reflect.Value) error {
if num, ok := data.(int64); ok {
if rv.Kind() >= reflect.Int && rv.Kind() <= reflect.Int64 {
switch rv.Kind() {
case reflect.Int, reflect.Int64:
// No bounds checking necessary.
case reflect.Int8:
if num < math.MinInt8 || num > math.MaxInt8 {
return e("value %d is out of range for int8", num)
}
case reflect.Int16:
if num < math.MinInt16 || num > math.MaxInt16 {
return e("value %d is out of range for int16", num)
}
case reflect.Int32:
if num < math.MinInt32 || num > math.MaxInt32 {
return e("value %d is out of range for int32", num)
}
}
rv.SetInt(num)
} else if rv.Kind() >= reflect.Uint && rv.Kind() <= reflect.Uint64 {
unum := uint64(num)
switch rv.Kind() {
case reflect.Uint, reflect.Uint64:
// No bounds checking necessary.
case reflect.Uint8:
if num < 0 || unum > math.MaxUint8 {
return e("value %d is out of range for uint8", num)
}
case reflect.Uint16:
if num < 0 || unum > math.MaxUint16 {
return e("value %d is out of range for uint16", num)
}
case reflect.Uint32:
if num < 0 || unum > math.MaxUint32 {
return e("value %d is out of range for uint32", num)
}
}
rv.SetUint(unum)
} else {
panic("unreachable")
}
return nil
}
return badtype("integer", data)
}
func (md *MetaData) unifyBool(data interface{}, rv reflect.Value) error {
if b, ok := data.(bool); ok {
rv.SetBool(b)
return nil
}
return badtype("boolean", data)
}
func (md *MetaData) unifyAnything(data interface{}, rv reflect.Value) error {
rv.Set(reflect.ValueOf(data))
return nil
}
func (md *MetaData) unifyText(data interface{}, v TextUnmarshaler) error {
var s string
switch sdata := data.(type) {
case TextMarshaler:
text, err := sdata.MarshalText()
if err != nil {
return err
}
s = string(text)
case fmt.Stringer:
s = sdata.String()
case string:
s = sdata
case bool:
s = fmt.Sprintf("%v", sdata)
case int64:
s = fmt.Sprintf("%d", sdata)
case float64:
s = fmt.Sprintf("%f", sdata)
default:
return badtype("primitive (string-like)", data)
}
if err := v.UnmarshalText([]byte(s)); err != nil {
return err
}
return nil
}
// rvalue returns a reflect.Value of `v`. All pointers are resolved.
func rvalue(v interface{}) reflect.Value {
return indirect(reflect.ValueOf(v))
}
// indirect returns the value pointed to by a pointer.
// Pointers are followed until the value is not a pointer.
// New values are allocated for each nil pointer.
//
// An exception to this rule is if the value satisfies an interface of
// interest to us (like encoding.TextUnmarshaler).
func indirect(v reflect.Value) reflect.Value {
if v.Kind() != reflect.Ptr {
if v.CanSet() {
pv := v.Addr()
if _, ok := pv.Interface().(TextUnmarshaler); ok {
return pv
}
}
return v
}
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
}
return indirect(reflect.Indirect(v))
}
func isUnifiable(rv reflect.Value) bool {
if rv.CanSet() {
return true
}
if _, ok := rv.Interface().(TextUnmarshaler); ok {
return true
}
return false
}
func badtype(expected string, data interface{}) error {
return e("cannot load TOML value of type %T into a Go %s", data, expected)
}

View File

@@ -1,121 +0,0 @@
package toml
import "strings"
// MetaData allows access to meta information about TOML data that may not
// be inferrable via reflection. In particular, whether a key has been defined
// and the TOML type of a key.
type MetaData struct {
mapping map[string]interface{}
types map[string]tomlType
keys []Key
decoded map[string]bool
context Key // Used only during decoding.
}
// IsDefined returns true if the key given exists in the TOML data. The key
// should be specified hierarchially. e.g.,
//
// // access the TOML key 'a.b.c'
// IsDefined("a", "b", "c")
//
// IsDefined will return false if an empty key given. Keys are case sensitive.
func (md *MetaData) IsDefined(key ...string) bool {
if len(key) == 0 {
return false
}
var hash map[string]interface{}
var ok bool
var hashOrVal interface{} = md.mapping
for _, k := range key {
if hash, ok = hashOrVal.(map[string]interface{}); !ok {
return false
}
if hashOrVal, ok = hash[k]; !ok {
return false
}
}
return true
}
// Type returns a string representation of the type of the key specified.
//
// Type will return the empty string if given an empty key or a key that
// does not exist. Keys are case sensitive.
func (md *MetaData) Type(key ...string) string {
fullkey := strings.Join(key, ".")
if typ, ok := md.types[fullkey]; ok {
return typ.typeString()
}
return ""
}
// Key is the type of any TOML key, including key groups. Use (MetaData).Keys
// to get values of this type.
type Key []string
func (k Key) String() string {
return strings.Join(k, ".")
}
func (k Key) maybeQuotedAll() string {
var ss []string
for i := range k {
ss = append(ss, k.maybeQuoted(i))
}
return strings.Join(ss, ".")
}
func (k Key) maybeQuoted(i int) string {
quote := false
for _, c := range k[i] {
if !isBareKeyChar(c) {
quote = true
break
}
}
if quote {
return "\"" + strings.Replace(k[i], "\"", "\\\"", -1) + "\""
}
return k[i]
}
func (k Key) add(piece string) Key {
newKey := make(Key, len(k)+1)
copy(newKey, k)
newKey[len(k)] = piece
return newKey
}
// Keys returns a slice of every key in the TOML data, including key groups.
// Each key is itself a slice, where the first element is the top of the
// hierarchy and the last is the most specific.
//
// The list will have the same order as the keys appeared in the TOML data.
//
// All keys returned are non-empty.
func (md *MetaData) Keys() []Key {
return md.keys
}
// Undecoded returns all keys that have not been decoded in the order in which
// they appear in the original TOML document.
//
// This includes keys that haven't been decoded because of a Primitive value.
// Once the Primitive value is decoded, the keys will be considered decoded.
//
// Also note that decoding into an empty interface will result in no decoding,
// and so no keys will be considered decoded.
//
// In this sense, the Undecoded keys correspond to keys in the TOML document
// that do not have a concrete type in your representation.
func (md *MetaData) Undecoded() []Key {
undecoded := make([]Key, 0, len(md.keys))
for _, key := range md.keys {
if !md.decoded[key.String()] {
undecoded = append(undecoded, key)
}
}
return undecoded
}

View File

@@ -1,27 +0,0 @@
/*
Package toml provides facilities for decoding and encoding TOML configuration
files via reflection. There is also support for delaying decoding with
the Primitive type, and querying the set of keys in a TOML document with the
MetaData type.
The specification implemented: https://github.com/mojombo/toml
The sub-command github.com/BurntSushi/toml/cmd/tomlv can be used to verify
whether a file is a valid TOML document. It can also be used to print the
type of each key in a TOML document.
Testing
There are two important types of tests used for this package. The first is
contained inside '*_test.go' files and uses the standard Go unit testing
framework. These tests are primarily devoted to holistically testing the
decoder and encoder.
The second type of testing is used to verify the implementation's adherence
to the TOML specification. These tests have been factored into their own
project: https://github.com/BurntSushi/toml-test
The reason the tests are in a separate project is so that they can be used by
any implementation of TOML. Namely, it is language agnostic.
*/
package toml

View File

@@ -1,568 +0,0 @@
package toml
import (
"bufio"
"errors"
"fmt"
"io"
"reflect"
"sort"
"strconv"
"strings"
"time"
)
type tomlEncodeError struct{ error }
var (
errArrayMixedElementTypes = errors.New(
"toml: cannot encode array with mixed element types")
errArrayNilElement = errors.New(
"toml: cannot encode array with nil element")
errNonString = errors.New(
"toml: cannot encode a map with non-string key type")
errAnonNonStruct = errors.New(
"toml: cannot encode an anonymous field that is not a struct")
errArrayNoTable = errors.New(
"toml: TOML array element cannot contain a table")
errNoKey = errors.New(
"toml: top-level values must be Go maps or structs")
errAnything = errors.New("") // used in testing
)
var quotedReplacer = strings.NewReplacer(
"\t", "\\t",
"\n", "\\n",
"\r", "\\r",
"\"", "\\\"",
"\\", "\\\\",
)
// Encoder controls the encoding of Go values to a TOML document to some
// io.Writer.
//
// The indentation level can be controlled with the Indent field.
type Encoder struct {
// A single indentation level. By default it is two spaces.
Indent string
// hasWritten is whether we have written any output to w yet.
hasWritten bool
w *bufio.Writer
}
// NewEncoder returns a TOML encoder that encodes Go values to the io.Writer
// given. By default, a single indentation level is 2 spaces.
func NewEncoder(w io.Writer) *Encoder {
return &Encoder{
w: bufio.NewWriter(w),
Indent: " ",
}
}
// Encode writes a TOML representation of the Go value to the underlying
// io.Writer. If the value given cannot be encoded to a valid TOML document,
// then an error is returned.
//
// The mapping between Go values and TOML values should be precisely the same
// as for the Decode* functions. Similarly, the TextMarshaler interface is
// supported by encoding the resulting bytes as strings. (If you want to write
// arbitrary binary data then you will need to use something like base64 since
// TOML does not have any binary types.)
//
// When encoding TOML hashes (i.e., Go maps or structs), keys without any
// sub-hashes are encoded first.
//
// If a Go map is encoded, then its keys are sorted alphabetically for
// deterministic output. More control over this behavior may be provided if
// there is demand for it.
//
// Encoding Go values without a corresponding TOML representation---like map
// types with non-string keys---will cause an error to be returned. Similarly
// for mixed arrays/slices, arrays/slices with nil elements, embedded
// non-struct types and nested slices containing maps or structs.
// (e.g., [][]map[string]string is not allowed but []map[string]string is OK
// and so is []map[string][]string.)
func (enc *Encoder) Encode(v interface{}) error {
rv := eindirect(reflect.ValueOf(v))
if err := enc.safeEncode(Key([]string{}), rv); err != nil {
return err
}
return enc.w.Flush()
}
func (enc *Encoder) safeEncode(key Key, rv reflect.Value) (err error) {
defer func() {
if r := recover(); r != nil {
if terr, ok := r.(tomlEncodeError); ok {
err = terr.error
return
}
panic(r)
}
}()
enc.encode(key, rv)
return nil
}
func (enc *Encoder) encode(key Key, rv reflect.Value) {
// Special case. Time needs to be in ISO8601 format.
// Special case. If we can marshal the type to text, then we used that.
// Basically, this prevents the encoder for handling these types as
// generic structs (or whatever the underlying type of a TextMarshaler is).
switch rv.Interface().(type) {
case time.Time, TextMarshaler:
enc.keyEqElement(key, rv)
return
}
k := rv.Kind()
switch k {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32,
reflect.Uint64,
reflect.Float32, reflect.Float64, reflect.String, reflect.Bool:
enc.keyEqElement(key, rv)
case reflect.Array, reflect.Slice:
if typeEqual(tomlArrayHash, tomlTypeOfGo(rv)) {
enc.eArrayOfTables(key, rv)
} else {
enc.keyEqElement(key, rv)
}
case reflect.Interface:
if rv.IsNil() {
return
}
enc.encode(key, rv.Elem())
case reflect.Map:
if rv.IsNil() {
return
}
enc.eTable(key, rv)
case reflect.Ptr:
if rv.IsNil() {
return
}
enc.encode(key, rv.Elem())
case reflect.Struct:
enc.eTable(key, rv)
default:
panic(e("unsupported type for key '%s': %s", key, k))
}
}
// eElement encodes any value that can be an array element (primitives and
// arrays).
func (enc *Encoder) eElement(rv reflect.Value) {
switch v := rv.Interface().(type) {
case time.Time:
// Special case time.Time as a primitive. Has to come before
// TextMarshaler below because time.Time implements
// encoding.TextMarshaler, but we need to always use UTC.
enc.wf(v.UTC().Format("2006-01-02T15:04:05Z"))
return
case TextMarshaler:
// Special case. Use text marshaler if it's available for this value.
if s, err := v.MarshalText(); err != nil {
encPanic(err)
} else {
enc.writeQuoted(string(s))
}
return
}
switch rv.Kind() {
case reflect.Bool:
enc.wf(strconv.FormatBool(rv.Bool()))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
reflect.Int64:
enc.wf(strconv.FormatInt(rv.Int(), 10))
case reflect.Uint, reflect.Uint8, reflect.Uint16,
reflect.Uint32, reflect.Uint64:
enc.wf(strconv.FormatUint(rv.Uint(), 10))
case reflect.Float32:
enc.wf(floatAddDecimal(strconv.FormatFloat(rv.Float(), 'f', -1, 32)))
case reflect.Float64:
enc.wf(floatAddDecimal(strconv.FormatFloat(rv.Float(), 'f', -1, 64)))
case reflect.Array, reflect.Slice:
enc.eArrayOrSliceElement(rv)
case reflect.Interface:
enc.eElement(rv.Elem())
case reflect.String:
enc.writeQuoted(rv.String())
default:
panic(e("unexpected primitive type: %s", rv.Kind()))
}
}
// By the TOML spec, all floats must have a decimal with at least one
// number on either side.
func floatAddDecimal(fstr string) string {
if !strings.Contains(fstr, ".") {
return fstr + ".0"
}
return fstr
}
func (enc *Encoder) writeQuoted(s string) {
enc.wf("\"%s\"", quotedReplacer.Replace(s))
}
func (enc *Encoder) eArrayOrSliceElement(rv reflect.Value) {
length := rv.Len()
enc.wf("[")
for i := 0; i < length; i++ {
elem := rv.Index(i)
enc.eElement(elem)
if i != length-1 {
enc.wf(", ")
}
}
enc.wf("]")
}
func (enc *Encoder) eArrayOfTables(key Key, rv reflect.Value) {
if len(key) == 0 {
encPanic(errNoKey)
}
for i := 0; i < rv.Len(); i++ {
trv := rv.Index(i)
if isNil(trv) {
continue
}
panicIfInvalidKey(key)
enc.newline()
enc.wf("%s[[%s]]", enc.indentStr(key), key.maybeQuotedAll())
enc.newline()
enc.eMapOrStruct(key, trv)
}
}
func (enc *Encoder) eTable(key Key, rv reflect.Value) {
panicIfInvalidKey(key)
if len(key) == 1 {
// Output an extra new line between top-level tables.
// (The newline isn't written if nothing else has been written though.)
enc.newline()
}
if len(key) > 0 {
enc.wf("%s[%s]", enc.indentStr(key), key.maybeQuotedAll())
enc.newline()
}
enc.eMapOrStruct(key, rv)
}
func (enc *Encoder) eMapOrStruct(key Key, rv reflect.Value) {
switch rv := eindirect(rv); rv.Kind() {
case reflect.Map:
enc.eMap(key, rv)
case reflect.Struct:
enc.eStruct(key, rv)
default:
panic("eTable: unhandled reflect.Value Kind: " + rv.Kind().String())
}
}
func (enc *Encoder) eMap(key Key, rv reflect.Value) {
rt := rv.Type()
if rt.Key().Kind() != reflect.String {
encPanic(errNonString)
}
// Sort keys so that we have deterministic output. And write keys directly
// underneath this key first, before writing sub-structs or sub-maps.
var mapKeysDirect, mapKeysSub []string
for _, mapKey := range rv.MapKeys() {
k := mapKey.String()
if typeIsHash(tomlTypeOfGo(rv.MapIndex(mapKey))) {
mapKeysSub = append(mapKeysSub, k)
} else {
mapKeysDirect = append(mapKeysDirect, k)
}
}
var writeMapKeys = func(mapKeys []string) {
sort.Strings(mapKeys)
for _, mapKey := range mapKeys {
mrv := rv.MapIndex(reflect.ValueOf(mapKey))
if isNil(mrv) {
// Don't write anything for nil fields.
continue
}
enc.encode(key.add(mapKey), mrv)
}
}
writeMapKeys(mapKeysDirect)
writeMapKeys(mapKeysSub)
}
func (enc *Encoder) eStruct(key Key, rv reflect.Value) {
// Write keys for fields directly under this key first, because if we write
// a field that creates a new table, then all keys under it will be in that
// table (not the one we're writing here).
rt := rv.Type()
var fieldsDirect, fieldsSub [][]int
var addFields func(rt reflect.Type, rv reflect.Value, start []int)
addFields = func(rt reflect.Type, rv reflect.Value, start []int) {
for i := 0; i < rt.NumField(); i++ {
f := rt.Field(i)
// skip unexported fields
if f.PkgPath != "" && !f.Anonymous {
continue
}
frv := rv.Field(i)
if f.Anonymous {
t := f.Type
switch t.Kind() {
case reflect.Struct:
// Treat anonymous struct fields with
// tag names as though they are not
// anonymous, like encoding/json does.
if getOptions(f.Tag).name == "" {
addFields(t, frv, f.Index)
continue
}
case reflect.Ptr:
if t.Elem().Kind() == reflect.Struct &&
getOptions(f.Tag).name == "" {
if !frv.IsNil() {
addFields(t.Elem(), frv.Elem(), f.Index)
}
continue
}
// Fall through to the normal field encoding logic below
// for non-struct anonymous fields.
}
}
if typeIsHash(tomlTypeOfGo(frv)) {
fieldsSub = append(fieldsSub, append(start, f.Index...))
} else {
fieldsDirect = append(fieldsDirect, append(start, f.Index...))
}
}
}
addFields(rt, rv, nil)
var writeFields = func(fields [][]int) {
for _, fieldIndex := range fields {
sft := rt.FieldByIndex(fieldIndex)
sf := rv.FieldByIndex(fieldIndex)
if isNil(sf) {
// Don't write anything for nil fields.
continue
}
opts := getOptions(sft.Tag)
if opts.skip {
continue
}
keyName := sft.Name
if opts.name != "" {
keyName = opts.name
}
if opts.omitempty && isEmpty(sf) {
continue
}
if opts.omitzero && isZero(sf) {
continue
}
enc.encode(key.add(keyName), sf)
}
}
writeFields(fieldsDirect)
writeFields(fieldsSub)
}
// tomlTypeName returns the TOML type name of the Go value's type. It is
// used to determine whether the types of array elements are mixed (which is
// forbidden). If the Go value is nil, then it is illegal for it to be an array
// element, and valueIsNil is returned as true.
// Returns the TOML type of a Go value. The type may be `nil`, which means
// no concrete TOML type could be found.
func tomlTypeOfGo(rv reflect.Value) tomlType {
if isNil(rv) || !rv.IsValid() {
return nil
}
switch rv.Kind() {
case reflect.Bool:
return tomlBool
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32,
reflect.Uint64:
return tomlInteger
case reflect.Float32, reflect.Float64:
return tomlFloat
case reflect.Array, reflect.Slice:
if typeEqual(tomlHash, tomlArrayType(rv)) {
return tomlArrayHash
}
return tomlArray
case reflect.Ptr, reflect.Interface:
return tomlTypeOfGo(rv.Elem())
case reflect.String:
return tomlString
case reflect.Map:
return tomlHash
case reflect.Struct:
switch rv.Interface().(type) {
case time.Time:
return tomlDatetime
case TextMarshaler:
return tomlString
default:
return tomlHash
}
default:
panic("unexpected reflect.Kind: " + rv.Kind().String())
}
}
// tomlArrayType returns the element type of a TOML array. The type returned
// may be nil if it cannot be determined (e.g., a nil slice or a zero length
// slize). This function may also panic if it finds a type that cannot be
// expressed in TOML (such as nil elements, heterogeneous arrays or directly
// nested arrays of tables).
func tomlArrayType(rv reflect.Value) tomlType {
if isNil(rv) || !rv.IsValid() || rv.Len() == 0 {
return nil
}
firstType := tomlTypeOfGo(rv.Index(0))
if firstType == nil {
encPanic(errArrayNilElement)
}
rvlen := rv.Len()
for i := 1; i < rvlen; i++ {
elem := rv.Index(i)
switch elemType := tomlTypeOfGo(elem); {
case elemType == nil:
encPanic(errArrayNilElement)
case !typeEqual(firstType, elemType):
encPanic(errArrayMixedElementTypes)
}
}
// If we have a nested array, then we must make sure that the nested
// array contains ONLY primitives.
// This checks arbitrarily nested arrays.
if typeEqual(firstType, tomlArray) || typeEqual(firstType, tomlArrayHash) {
nest := tomlArrayType(eindirect(rv.Index(0)))
if typeEqual(nest, tomlHash) || typeEqual(nest, tomlArrayHash) {
encPanic(errArrayNoTable)
}
}
return firstType
}
type tagOptions struct {
skip bool // "-"
name string
omitempty bool
omitzero bool
}
func getOptions(tag reflect.StructTag) tagOptions {
t := tag.Get("toml")
if t == "-" {
return tagOptions{skip: true}
}
var opts tagOptions
parts := strings.Split(t, ",")
opts.name = parts[0]
for _, s := range parts[1:] {
switch s {
case "omitempty":
opts.omitempty = true
case "omitzero":
opts.omitzero = true
}
}
return opts
}
func isZero(rv reflect.Value) bool {
switch rv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return rv.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return rv.Uint() == 0
case reflect.Float32, reflect.Float64:
return rv.Float() == 0.0
}
return false
}
func isEmpty(rv reflect.Value) bool {
switch rv.Kind() {
case reflect.Array, reflect.Slice, reflect.Map, reflect.String:
return rv.Len() == 0
case reflect.Bool:
return !rv.Bool()
}
return false
}
func (enc *Encoder) newline() {
if enc.hasWritten {
enc.wf("\n")
}
}
func (enc *Encoder) keyEqElement(key Key, val reflect.Value) {
if len(key) == 0 {
encPanic(errNoKey)
}
panicIfInvalidKey(key)
enc.wf("%s%s = ", enc.indentStr(key), key.maybeQuoted(len(key)-1))
enc.eElement(val)
enc.newline()
}
func (enc *Encoder) wf(format string, v ...interface{}) {
if _, err := fmt.Fprintf(enc.w, format, v...); err != nil {
encPanic(err)
}
enc.hasWritten = true
}
func (enc *Encoder) indentStr(key Key) string {
return strings.Repeat(enc.Indent, len(key)-1)
}
func encPanic(err error) {
panic(tomlEncodeError{err})
}
func eindirect(v reflect.Value) reflect.Value {
switch v.Kind() {
case reflect.Ptr, reflect.Interface:
return eindirect(v.Elem())
default:
return v
}
}
func isNil(rv reflect.Value) bool {
switch rv.Kind() {
case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
return rv.IsNil()
default:
return false
}
}
func panicIfInvalidKey(key Key) {
for _, k := range key {
if len(k) == 0 {
encPanic(e("Key '%s' is not a valid table name. Key names "+
"cannot be empty.", key.maybeQuotedAll()))
}
}
}
func isValidKeyName(s string) bool {
return len(s) != 0
}

View File

@@ -1,19 +0,0 @@
// +build go1.2
package toml
// In order to support Go 1.1, we define our own TextMarshaler and
// TextUnmarshaler types. For Go 1.2+, we just alias them with the
// standard library interfaces.
import (
"encoding"
)
// TextMarshaler is a synonym for encoding.TextMarshaler. It is defined here
// so that Go 1.1 can be supported.
type TextMarshaler encoding.TextMarshaler
// TextUnmarshaler is a synonym for encoding.TextUnmarshaler. It is defined
// here so that Go 1.1 can be supported.
type TextUnmarshaler encoding.TextUnmarshaler

View File

@@ -1,18 +0,0 @@
// +build !go1.2
package toml
// These interfaces were introduced in Go 1.2, so we add them manually when
// compiling for Go 1.1.
// TextMarshaler is a synonym for encoding.TextMarshaler. It is defined here
// so that Go 1.1 can be supported.
type TextMarshaler interface {
MarshalText() (text []byte, err error)
}
// TextUnmarshaler is a synonym for encoding.TextUnmarshaler. It is defined
// here so that Go 1.1 can be supported.
type TextUnmarshaler interface {
UnmarshalText(text []byte) error
}

View File

@@ -1,858 +0,0 @@
package toml
import (
"fmt"
"strings"
"unicode"
"unicode/utf8"
)
type itemType int
const (
itemError itemType = iota
itemNIL // used in the parser to indicate no type
itemEOF
itemText
itemString
itemRawString
itemMultilineString
itemRawMultilineString
itemBool
itemInteger
itemFloat
itemDatetime
itemArray // the start of an array
itemArrayEnd
itemTableStart
itemTableEnd
itemArrayTableStart
itemArrayTableEnd
itemKeyStart
itemCommentStart
)
const (
eof = 0
tableStart = '['
tableEnd = ']'
arrayTableStart = '['
arrayTableEnd = ']'
tableSep = '.'
keySep = '='
arrayStart = '['
arrayEnd = ']'
arrayValTerm = ','
commentStart = '#'
stringStart = '"'
stringEnd = '"'
rawStringStart = '\''
rawStringEnd = '\''
)
type stateFn func(lx *lexer) stateFn
type lexer struct {
input string
start int
pos int
width int
line int
state stateFn
items chan item
// A stack of state functions used to maintain context.
// The idea is to reuse parts of the state machine in various places.
// For example, values can appear at the top level or within arbitrarily
// nested arrays. The last state on the stack is used after a value has
// been lexed. Similarly for comments.
stack []stateFn
}
type item struct {
typ itemType
val string
line int
}
func (lx *lexer) nextItem() item {
for {
select {
case item := <-lx.items:
return item
default:
lx.state = lx.state(lx)
}
}
}
func lex(input string) *lexer {
lx := &lexer{
input: input + "\n",
state: lexTop,
line: 1,
items: make(chan item, 10),
stack: make([]stateFn, 0, 10),
}
return lx
}
func (lx *lexer) push(state stateFn) {
lx.stack = append(lx.stack, state)
}
func (lx *lexer) pop() stateFn {
if len(lx.stack) == 0 {
return lx.errorf("BUG in lexer: no states to pop.")
}
last := lx.stack[len(lx.stack)-1]
lx.stack = lx.stack[0 : len(lx.stack)-1]
return last
}
func (lx *lexer) current() string {
return lx.input[lx.start:lx.pos]
}
func (lx *lexer) emit(typ itemType) {
lx.items <- item{typ, lx.current(), lx.line}
lx.start = lx.pos
}
func (lx *lexer) emitTrim(typ itemType) {
lx.items <- item{typ, strings.TrimSpace(lx.current()), lx.line}
lx.start = lx.pos
}
func (lx *lexer) next() (r rune) {
if lx.pos >= len(lx.input) {
lx.width = 0
return eof
}
if lx.input[lx.pos] == '\n' {
lx.line++
}
r, lx.width = utf8.DecodeRuneInString(lx.input[lx.pos:])
lx.pos += lx.width
return r
}
// ignore skips over the pending input before this point.
func (lx *lexer) ignore() {
lx.start = lx.pos
}
// backup steps back one rune. Can be called only once per call of next.
func (lx *lexer) backup() {
lx.pos -= lx.width
if lx.pos < len(lx.input) && lx.input[lx.pos] == '\n' {
lx.line--
}
}
// accept consumes the next rune if it's equal to `valid`.
func (lx *lexer) accept(valid rune) bool {
if lx.next() == valid {
return true
}
lx.backup()
return false
}
// peek returns but does not consume the next rune in the input.
func (lx *lexer) peek() rune {
r := lx.next()
lx.backup()
return r
}
// skip ignores all input that matches the given predicate.
func (lx *lexer) skip(pred func(rune) bool) {
for {
r := lx.next()
if pred(r) {
continue
}
lx.backup()
lx.ignore()
return
}
}
// errorf stops all lexing by emitting an error and returning `nil`.
// Note that any value that is a character is escaped if it's a special
// character (new lines, tabs, etc.).
func (lx *lexer) errorf(format string, values ...interface{}) stateFn {
lx.items <- item{
itemError,
fmt.Sprintf(format, values...),
lx.line,
}
return nil
}
// lexTop consumes elements at the top level of TOML data.
func lexTop(lx *lexer) stateFn {
r := lx.next()
if isWhitespace(r) || isNL(r) {
return lexSkip(lx, lexTop)
}
switch r {
case commentStart:
lx.push(lexTop)
return lexCommentStart
case tableStart:
return lexTableStart
case eof:
if lx.pos > lx.start {
return lx.errorf("Unexpected EOF.")
}
lx.emit(itemEOF)
return nil
}
// At this point, the only valid item can be a key, so we back up
// and let the key lexer do the rest.
lx.backup()
lx.push(lexTopEnd)
return lexKeyStart
}
// lexTopEnd is entered whenever a top-level item has been consumed. (A value
// or a table.) It must see only whitespace, and will turn back to lexTop
// upon a new line. If it sees EOF, it will quit the lexer successfully.
func lexTopEnd(lx *lexer) stateFn {
r := lx.next()
switch {
case r == commentStart:
// a comment will read to a new line for us.
lx.push(lexTop)
return lexCommentStart
case isWhitespace(r):
return lexTopEnd
case isNL(r):
lx.ignore()
return lexTop
case r == eof:
lx.ignore()
return lexTop
}
return lx.errorf("Expected a top-level item to end with a new line, "+
"comment or EOF, but got %q instead.", r)
}
// lexTable lexes the beginning of a table. Namely, it makes sure that
// it starts with a character other than '.' and ']'.
// It assumes that '[' has already been consumed.
// It also handles the case that this is an item in an array of tables.
// e.g., '[[name]]'.
func lexTableStart(lx *lexer) stateFn {
if lx.peek() == arrayTableStart {
lx.next()
lx.emit(itemArrayTableStart)
lx.push(lexArrayTableEnd)
} else {
lx.emit(itemTableStart)
lx.push(lexTableEnd)
}
return lexTableNameStart
}
func lexTableEnd(lx *lexer) stateFn {
lx.emit(itemTableEnd)
return lexTopEnd
}
func lexArrayTableEnd(lx *lexer) stateFn {
if r := lx.next(); r != arrayTableEnd {
return lx.errorf("Expected end of table array name delimiter %q, "+
"but got %q instead.", arrayTableEnd, r)
}
lx.emit(itemArrayTableEnd)
return lexTopEnd
}
func lexTableNameStart(lx *lexer) stateFn {
lx.skip(isWhitespace)
switch r := lx.peek(); {
case r == tableEnd || r == eof:
return lx.errorf("Unexpected end of table name. (Table names cannot " +
"be empty.)")
case r == tableSep:
return lx.errorf("Unexpected table separator. (Table names cannot " +
"be empty.)")
case r == stringStart || r == rawStringStart:
lx.ignore()
lx.push(lexTableNameEnd)
return lexValue // reuse string lexing
default:
return lexBareTableName
}
}
// lexBareTableName lexes the name of a table. It assumes that at least one
// valid character for the table has already been read.
func lexBareTableName(lx *lexer) stateFn {
r := lx.next()
if isBareKeyChar(r) {
return lexBareTableName
}
lx.backup()
lx.emit(itemText)
return lexTableNameEnd
}
// lexTableNameEnd reads the end of a piece of a table name, optionally
// consuming whitespace.
func lexTableNameEnd(lx *lexer) stateFn {
lx.skip(isWhitespace)
switch r := lx.next(); {
case isWhitespace(r):
return lexTableNameEnd
case r == tableSep:
lx.ignore()
return lexTableNameStart
case r == tableEnd:
return lx.pop()
default:
return lx.errorf("Expected '.' or ']' to end table name, but got %q "+
"instead.", r)
}
}
// lexKeyStart consumes a key name up until the first non-whitespace character.
// lexKeyStart will ignore whitespace.
func lexKeyStart(lx *lexer) stateFn {
r := lx.peek()
switch {
case r == keySep:
return lx.errorf("Unexpected key separator %q.", keySep)
case isWhitespace(r) || isNL(r):
lx.next()
return lexSkip(lx, lexKeyStart)
case r == stringStart || r == rawStringStart:
lx.ignore()
lx.emit(itemKeyStart)
lx.push(lexKeyEnd)
return lexValue // reuse string lexing
default:
lx.ignore()
lx.emit(itemKeyStart)
return lexBareKey
}
}
// lexBareKey consumes the text of a bare key. Assumes that the first character
// (which is not whitespace) has not yet been consumed.
func lexBareKey(lx *lexer) stateFn {
switch r := lx.next(); {
case isBareKeyChar(r):
return lexBareKey
case isWhitespace(r):
lx.backup()
lx.emit(itemText)
return lexKeyEnd
case r == keySep:
lx.backup()
lx.emit(itemText)
return lexKeyEnd
default:
return lx.errorf("Bare keys cannot contain %q.", r)
}
}
// lexKeyEnd consumes the end of a key and trims whitespace (up to the key
// separator).
func lexKeyEnd(lx *lexer) stateFn {
switch r := lx.next(); {
case r == keySep:
return lexSkip(lx, lexValue)
case isWhitespace(r):
return lexSkip(lx, lexKeyEnd)
default:
return lx.errorf("Expected key separator %q, but got %q instead.",
keySep, r)
}
}
// lexValue starts the consumption of a value anywhere a value is expected.
// lexValue will ignore whitespace.
// After a value is lexed, the last state on the next is popped and returned.
func lexValue(lx *lexer) stateFn {
// We allow whitespace to precede a value, but NOT new lines.
// In array syntax, the array states are responsible for ignoring new
// lines.
r := lx.next()
switch {
case isWhitespace(r):
return lexSkip(lx, lexValue)
case isDigit(r):
lx.backup() // avoid an extra state and use the same as above
return lexNumberOrDateStart
}
switch r {
case arrayStart:
lx.ignore()
lx.emit(itemArray)
return lexArrayValue
case stringStart:
if lx.accept(stringStart) {
if lx.accept(stringStart) {
lx.ignore() // Ignore """
return lexMultilineString
}
lx.backup()
}
lx.ignore() // ignore the '"'
return lexString
case rawStringStart:
if lx.accept(rawStringStart) {
if lx.accept(rawStringStart) {
lx.ignore() // Ignore """
return lexMultilineRawString
}
lx.backup()
}
lx.ignore() // ignore the "'"
return lexRawString
case '+', '-':
return lexNumberStart
case '.': // special error case, be kind to users
return lx.errorf("Floats must start with a digit, not '.'.")
}
if unicode.IsLetter(r) {
// Be permissive here; lexBool will give a nice error if the
// user wrote something like
// x = foo
// (i.e. not 'true' or 'false' but is something else word-like.)
lx.backup()
return lexBool
}
return lx.errorf("Expected value but found %q instead.", r)
}
// lexArrayValue consumes one value in an array. It assumes that '[' or ','
// have already been consumed. All whitespace and new lines are ignored.
func lexArrayValue(lx *lexer) stateFn {
r := lx.next()
switch {
case isWhitespace(r) || isNL(r):
return lexSkip(lx, lexArrayValue)
case r == commentStart:
lx.push(lexArrayValue)
return lexCommentStart
case r == arrayValTerm:
return lx.errorf("Unexpected array value terminator %q.",
arrayValTerm)
case r == arrayEnd:
return lexArrayEnd
}
lx.backup()
lx.push(lexArrayValueEnd)
return lexValue
}
// lexArrayValueEnd consumes the cruft between values of an array. Namely,
// it ignores whitespace and expects either a ',' or a ']'.
func lexArrayValueEnd(lx *lexer) stateFn {
r := lx.next()
switch {
case isWhitespace(r) || isNL(r):
return lexSkip(lx, lexArrayValueEnd)
case r == commentStart:
lx.push(lexArrayValueEnd)
return lexCommentStart
case r == arrayValTerm:
lx.ignore()
return lexArrayValue // move on to the next value
case r == arrayEnd:
return lexArrayEnd
}
return lx.errorf("Expected an array value terminator %q or an array "+
"terminator %q, but got %q instead.", arrayValTerm, arrayEnd, r)
}
// lexArrayEnd finishes the lexing of an array. It assumes that a ']' has
// just been consumed.
func lexArrayEnd(lx *lexer) stateFn {
lx.ignore()
lx.emit(itemArrayEnd)
return lx.pop()
}
// lexString consumes the inner contents of a string. It assumes that the
// beginning '"' has already been consumed and ignored.
func lexString(lx *lexer) stateFn {
r := lx.next()
switch {
case isNL(r):
return lx.errorf("Strings cannot contain new lines.")
case r == '\\':
lx.push(lexString)
return lexStringEscape
case r == stringEnd:
lx.backup()
lx.emit(itemString)
lx.next()
lx.ignore()
return lx.pop()
}
return lexString
}
// lexMultilineString consumes the inner contents of a string. It assumes that
// the beginning '"""' has already been consumed and ignored.
func lexMultilineString(lx *lexer) stateFn {
r := lx.next()
switch {
case r == '\\':
return lexMultilineStringEscape
case r == stringEnd:
if lx.accept(stringEnd) {
if lx.accept(stringEnd) {
lx.backup()
lx.backup()
lx.backup()
lx.emit(itemMultilineString)
lx.next()
lx.next()
lx.next()
lx.ignore()
return lx.pop()
}
lx.backup()
}
}
return lexMultilineString
}
// lexRawString consumes a raw string. Nothing can be escaped in such a string.
// It assumes that the beginning "'" has already been consumed and ignored.
func lexRawString(lx *lexer) stateFn {
r := lx.next()
switch {
case isNL(r):
return lx.errorf("Strings cannot contain new lines.")
case r == rawStringEnd:
lx.backup()
lx.emit(itemRawString)
lx.next()
lx.ignore()
return lx.pop()
}
return lexRawString
}
// lexMultilineRawString consumes a raw string. Nothing can be escaped in such
// a string. It assumes that the beginning "'" has already been consumed and
// ignored.
func lexMultilineRawString(lx *lexer) stateFn {
r := lx.next()
switch {
case r == rawStringEnd:
if lx.accept(rawStringEnd) {
if lx.accept(rawStringEnd) {
lx.backup()
lx.backup()
lx.backup()
lx.emit(itemRawMultilineString)
lx.next()
lx.next()
lx.next()
lx.ignore()
return lx.pop()
}
lx.backup()
}
}
return lexMultilineRawString
}
// lexMultilineStringEscape consumes an escaped character. It assumes that the
// preceding '\\' has already been consumed.
func lexMultilineStringEscape(lx *lexer) stateFn {
// Handle the special case first:
if isNL(lx.next()) {
return lexMultilineString
}
lx.backup()
lx.push(lexMultilineString)
return lexStringEscape(lx)
}
func lexStringEscape(lx *lexer) stateFn {
r := lx.next()
switch r {
case 'b':
fallthrough
case 't':
fallthrough
case 'n':
fallthrough
case 'f':
fallthrough
case 'r':
fallthrough
case '"':
fallthrough
case '\\':
return lx.pop()
case 'u':
return lexShortUnicodeEscape
case 'U':
return lexLongUnicodeEscape
}
return lx.errorf("Invalid escape character %q. Only the following "+
"escape characters are allowed: "+
"\\b, \\t, \\n, \\f, \\r, \\\", \\/, \\\\, "+
"\\uXXXX and \\UXXXXXXXX.", r)
}
func lexShortUnicodeEscape(lx *lexer) stateFn {
var r rune
for i := 0; i < 4; i++ {
r = lx.next()
if !isHexadecimal(r) {
return lx.errorf("Expected four hexadecimal digits after '\\u', "+
"but got '%s' instead.", lx.current())
}
}
return lx.pop()
}
func lexLongUnicodeEscape(lx *lexer) stateFn {
var r rune
for i := 0; i < 8; i++ {
r = lx.next()
if !isHexadecimal(r) {
return lx.errorf("Expected eight hexadecimal digits after '\\U', "+
"but got '%s' instead.", lx.current())
}
}
return lx.pop()
}
// lexNumberOrDateStart consumes either an integer, a float, or datetime.
func lexNumberOrDateStart(lx *lexer) stateFn {
r := lx.next()
if isDigit(r) {
return lexNumberOrDate
}
switch r {
case '_':
return lexNumber
case 'e', 'E':
return lexFloat
case '.':
return lx.errorf("Floats must start with a digit, not '.'.")
}
return lx.errorf("Expected a digit but got %q.", r)
}
// lexNumberOrDate consumes either an integer, float or datetime.
func lexNumberOrDate(lx *lexer) stateFn {
r := lx.next()
if isDigit(r) {
return lexNumberOrDate
}
switch r {
case '-':
return lexDatetime
case '_':
return lexNumber
case '.', 'e', 'E':
return lexFloat
}
lx.backup()
lx.emit(itemInteger)
return lx.pop()
}
// lexDatetime consumes a Datetime, to a first approximation.
// The parser validates that it matches one of the accepted formats.
func lexDatetime(lx *lexer) stateFn {
r := lx.next()
if isDigit(r) {
return lexDatetime
}
switch r {
case '-', 'T', ':', '.', 'Z':
return lexDatetime
}
lx.backup()
lx.emit(itemDatetime)
return lx.pop()
}
// lexNumberStart consumes either an integer or a float. It assumes that a sign
// has already been read, but that *no* digits have been consumed.
// lexNumberStart will move to the appropriate integer or float states.
func lexNumberStart(lx *lexer) stateFn {
// We MUST see a digit. Even floats have to start with a digit.
r := lx.next()
if !isDigit(r) {
if r == '.' {
return lx.errorf("Floats must start with a digit, not '.'.")
}
return lx.errorf("Expected a digit but got %q.", r)
}
return lexNumber
}
// lexNumber consumes an integer or a float after seeing the first digit.
func lexNumber(lx *lexer) stateFn {
r := lx.next()
if isDigit(r) {
return lexNumber
}
switch r {
case '_':
return lexNumber
case '.', 'e', 'E':
return lexFloat
}
lx.backup()
lx.emit(itemInteger)
return lx.pop()
}
// lexFloat consumes the elements of a float. It allows any sequence of
// float-like characters, so floats emitted by the lexer are only a first
// approximation and must be validated by the parser.
func lexFloat(lx *lexer) stateFn {
r := lx.next()
if isDigit(r) {
return lexFloat
}
switch r {
case '_', '.', '-', '+', 'e', 'E':
return lexFloat
}
lx.backup()
lx.emit(itemFloat)
return lx.pop()
}
// lexBool consumes a bool string: 'true' or 'false.
func lexBool(lx *lexer) stateFn {
var rs []rune
for {
r := lx.next()
if r == eof || isWhitespace(r) || isNL(r) {
lx.backup()
break
}
rs = append(rs, r)
}
s := string(rs)
switch s {
case "true", "false":
lx.emit(itemBool)
return lx.pop()
}
return lx.errorf("Expected value but found %q instead.", s)
}
// lexCommentStart begins the lexing of a comment. It will emit
// itemCommentStart and consume no characters, passing control to lexComment.
func lexCommentStart(lx *lexer) stateFn {
lx.ignore()
lx.emit(itemCommentStart)
return lexComment
}
// lexComment lexes an entire comment. It assumes that '#' has been consumed.
// It will consume *up to* the first new line character, and pass control
// back to the last state on the stack.
func lexComment(lx *lexer) stateFn {
r := lx.peek()
if isNL(r) || r == eof {
lx.emit(itemText)
return lx.pop()
}
lx.next()
return lexComment
}
// lexSkip ignores all slurped input and moves on to the next state.
func lexSkip(lx *lexer, nextState stateFn) stateFn {
return func(lx *lexer) stateFn {
lx.ignore()
return nextState
}
}
// isWhitespace returns true if `r` is a whitespace character according
// to the spec.
func isWhitespace(r rune) bool {
return r == '\t' || r == ' '
}
func isNL(r rune) bool {
return r == '\n' || r == '\r'
}
func isDigit(r rune) bool {
return r >= '0' && r <= '9'
}
func isHexadecimal(r rune) bool {
return (r >= '0' && r <= '9') ||
(r >= 'a' && r <= 'f') ||
(r >= 'A' && r <= 'F')
}
func isBareKeyChar(r rune) bool {
return (r >= 'A' && r <= 'Z') ||
(r >= 'a' && r <= 'z') ||
(r >= '0' && r <= '9') ||
r == '_' ||
r == '-'
}
func (itype itemType) String() string {
switch itype {
case itemError:
return "Error"
case itemNIL:
return "NIL"
case itemEOF:
return "EOF"
case itemText:
return "Text"
case itemString, itemRawString, itemMultilineString, itemRawMultilineString:
return "String"
case itemBool:
return "Bool"
case itemInteger:
return "Integer"
case itemFloat:
return "Float"
case itemDatetime:
return "DateTime"
case itemTableStart:
return "TableStart"
case itemTableEnd:
return "TableEnd"
case itemKeyStart:
return "KeyStart"
case itemArray:
return "Array"
case itemArrayEnd:
return "ArrayEnd"
case itemCommentStart:
return "CommentStart"
}
panic(fmt.Sprintf("BUG: Unknown type '%d'.", int(itype)))
}
func (item item) String() string {
return fmt.Sprintf("(%s, %s)", item.typ.String(), item.val)
}

View File

@@ -1,557 +0,0 @@
package toml
import (
"fmt"
"strconv"
"strings"
"time"
"unicode"
"unicode/utf8"
)
type parser struct {
mapping map[string]interface{}
types map[string]tomlType
lx *lexer
// A list of keys in the order that they appear in the TOML data.
ordered []Key
// the full key for the current hash in scope
context Key
// the base key name for everything except hashes
currentKey string
// rough approximation of line number
approxLine int
// A map of 'key.group.names' to whether they were created implicitly.
implicits map[string]bool
}
type parseError string
func (pe parseError) Error() string {
return string(pe)
}
func parse(data string) (p *parser, err error) {
defer func() {
if r := recover(); r != nil {
var ok bool
if err, ok = r.(parseError); ok {
return
}
panic(r)
}
}()
p = &parser{
mapping: make(map[string]interface{}),
types: make(map[string]tomlType),
lx: lex(data),
ordered: make([]Key, 0),
implicits: make(map[string]bool),
}
for {
item := p.next()
if item.typ == itemEOF {
break
}
p.topLevel(item)
}
return p, nil
}
func (p *parser) panicf(format string, v ...interface{}) {
msg := fmt.Sprintf("Near line %d (last key parsed '%s'): %s",
p.approxLine, p.current(), fmt.Sprintf(format, v...))
panic(parseError(msg))
}
func (p *parser) next() item {
it := p.lx.nextItem()
if it.typ == itemError {
p.panicf("%s", it.val)
}
return it
}
func (p *parser) bug(format string, v ...interface{}) {
panic(fmt.Sprintf("BUG: "+format+"\n\n", v...))
}
func (p *parser) expect(typ itemType) item {
it := p.next()
p.assertEqual(typ, it.typ)
return it
}
func (p *parser) assertEqual(expected, got itemType) {
if expected != got {
p.bug("Expected '%s' but got '%s'.", expected, got)
}
}
func (p *parser) topLevel(item item) {
switch item.typ {
case itemCommentStart:
p.approxLine = item.line
p.expect(itemText)
case itemTableStart:
kg := p.next()
p.approxLine = kg.line
var key Key
for ; kg.typ != itemTableEnd && kg.typ != itemEOF; kg = p.next() {
key = append(key, p.keyString(kg))
}
p.assertEqual(itemTableEnd, kg.typ)
p.establishContext(key, false)
p.setType("", tomlHash)
p.ordered = append(p.ordered, key)
case itemArrayTableStart:
kg := p.next()
p.approxLine = kg.line
var key Key
for ; kg.typ != itemArrayTableEnd && kg.typ != itemEOF; kg = p.next() {
key = append(key, p.keyString(kg))
}
p.assertEqual(itemArrayTableEnd, kg.typ)
p.establishContext(key, true)
p.setType("", tomlArrayHash)
p.ordered = append(p.ordered, key)
case itemKeyStart:
kname := p.next()
p.approxLine = kname.line
p.currentKey = p.keyString(kname)
val, typ := p.value(p.next())
p.setValue(p.currentKey, val)
p.setType(p.currentKey, typ)
p.ordered = append(p.ordered, p.context.add(p.currentKey))
p.currentKey = ""
default:
p.bug("Unexpected type at top level: %s", item.typ)
}
}
// Gets a string for a key (or part of a key in a table name).
func (p *parser) keyString(it item) string {
switch it.typ {
case itemText:
return it.val
case itemString, itemMultilineString,
itemRawString, itemRawMultilineString:
s, _ := p.value(it)
return s.(string)
default:
p.bug("Unexpected key type: %s", it.typ)
panic("unreachable")
}
}
// value translates an expected value from the lexer into a Go value wrapped
// as an empty interface.
func (p *parser) value(it item) (interface{}, tomlType) {
switch it.typ {
case itemString:
return p.replaceEscapes(it.val), p.typeOfPrimitive(it)
case itemMultilineString:
trimmed := stripFirstNewline(stripEscapedWhitespace(it.val))
return p.replaceEscapes(trimmed), p.typeOfPrimitive(it)
case itemRawString:
return it.val, p.typeOfPrimitive(it)
case itemRawMultilineString:
return stripFirstNewline(it.val), p.typeOfPrimitive(it)
case itemBool:
switch it.val {
case "true":
return true, p.typeOfPrimitive(it)
case "false":
return false, p.typeOfPrimitive(it)
}
p.bug("Expected boolean value, but got '%s'.", it.val)
case itemInteger:
if !numUnderscoresOK(it.val) {
p.panicf("Invalid integer %q: underscores must be surrounded by digits",
it.val)
}
val := strings.Replace(it.val, "_", "", -1)
num, err := strconv.ParseInt(val, 10, 64)
if err != nil {
// Distinguish integer values. Normally, it'd be a bug if the lexer
// provides an invalid integer, but it's possible that the number is
// out of range of valid values (which the lexer cannot determine).
// So mark the former as a bug but the latter as a legitimate user
// error.
if e, ok := err.(*strconv.NumError); ok &&
e.Err == strconv.ErrRange {
p.panicf("Integer '%s' is out of the range of 64-bit "+
"signed integers.", it.val)
} else {
p.bug("Expected integer value, but got '%s'.", it.val)
}
}
return num, p.typeOfPrimitive(it)
case itemFloat:
parts := strings.FieldsFunc(it.val, func(r rune) bool {
switch r {
case '.', 'e', 'E':
return true
}
return false
})
for _, part := range parts {
if !numUnderscoresOK(part) {
p.panicf("Invalid float %q: underscores must be "+
"surrounded by digits", it.val)
}
}
if !numPeriodsOK(it.val) {
// As a special case, numbers like '123.' or '1.e2',
// which are valid as far as Go/strconv are concerned,
// must be rejected because TOML says that a fractional
// part consists of '.' followed by 1+ digits.
p.panicf("Invalid float %q: '.' must be followed "+
"by one or more digits", it.val)
}
val := strings.Replace(it.val, "_", "", -1)
num, err := strconv.ParseFloat(val, 64)
if err != nil {
if e, ok := err.(*strconv.NumError); ok &&
e.Err == strconv.ErrRange {
p.panicf("Float '%s' is out of the range of 64-bit "+
"IEEE-754 floating-point numbers.", it.val)
} else {
p.panicf("Invalid float value: %q", it.val)
}
}
return num, p.typeOfPrimitive(it)
case itemDatetime:
var t time.Time
var ok bool
var err error
for _, format := range []string{
"2006-01-02T15:04:05Z07:00",
"2006-01-02T15:04:05",
"2006-01-02",
} {
t, err = time.ParseInLocation(format, it.val, time.Local)
if err == nil {
ok = true
break
}
}
if !ok {
p.panicf("Invalid TOML Datetime: %q.", it.val)
}
return t, p.typeOfPrimitive(it)
case itemArray:
array := make([]interface{}, 0)
types := make([]tomlType, 0)
for it = p.next(); it.typ != itemArrayEnd; it = p.next() {
if it.typ == itemCommentStart {
p.expect(itemText)
continue
}
val, typ := p.value(it)
array = append(array, val)
types = append(types, typ)
}
return array, p.typeOfArray(types)
}
p.bug("Unexpected value type: %s", it.typ)
panic("unreachable")
}
// numUnderscoresOK checks whether each underscore in s is surrounded by
// characters that are not underscores.
func numUnderscoresOK(s string) bool {
accept := false
for _, r := range s {
if r == '_' {
if !accept {
return false
}
accept = false
continue
}
accept = true
}
return accept
}
// numPeriodsOK checks whether every period in s is followed by a digit.
func numPeriodsOK(s string) bool {
period := false
for _, r := range s {
if period && !isDigit(r) {
return false
}
period = r == '.'
}
return !period
}
// establishContext sets the current context of the parser,
// where the context is either a hash or an array of hashes. Which one is
// set depends on the value of the `array` parameter.
//
// Establishing the context also makes sure that the key isn't a duplicate, and
// will create implicit hashes automatically.
func (p *parser) establishContext(key Key, array bool) {
var ok bool
// Always start at the top level and drill down for our context.
hashContext := p.mapping
keyContext := make(Key, 0)
// We only need implicit hashes for key[0:-1]
for _, k := range key[0 : len(key)-1] {
_, ok = hashContext[k]
keyContext = append(keyContext, k)
// No key? Make an implicit hash and move on.
if !ok {
p.addImplicit(keyContext)
hashContext[k] = make(map[string]interface{})
}
// If the hash context is actually an array of tables, then set
// the hash context to the last element in that array.
//
// Otherwise, it better be a table, since this MUST be a key group (by
// virtue of it not being the last element in a key).
switch t := hashContext[k].(type) {
case []map[string]interface{}:
hashContext = t[len(t)-1]
case map[string]interface{}:
hashContext = t
default:
p.panicf("Key '%s' was already created as a hash.", keyContext)
}
}
p.context = keyContext
if array {
// If this is the first element for this array, then allocate a new
// list of tables for it.
k := key[len(key)-1]
if _, ok := hashContext[k]; !ok {
hashContext[k] = make([]map[string]interface{}, 0, 5)
}
// Add a new table. But make sure the key hasn't already been used
// for something else.
if hash, ok := hashContext[k].([]map[string]interface{}); ok {
hashContext[k] = append(hash, make(map[string]interface{}))
} else {
p.panicf("Key '%s' was already created and cannot be used as "+
"an array.", keyContext)
}
} else {
p.setValue(key[len(key)-1], make(map[string]interface{}))
}
p.context = append(p.context, key[len(key)-1])
}
// setValue sets the given key to the given value in the current context.
// It will make sure that the key hasn't already been defined, account for
// implicit key groups.
func (p *parser) setValue(key string, value interface{}) {
var tmpHash interface{}
var ok bool
hash := p.mapping
keyContext := make(Key, 0)
for _, k := range p.context {
keyContext = append(keyContext, k)
if tmpHash, ok = hash[k]; !ok {
p.bug("Context for key '%s' has not been established.", keyContext)
}
switch t := tmpHash.(type) {
case []map[string]interface{}:
// The context is a table of hashes. Pick the most recent table
// defined as the current hash.
hash = t[len(t)-1]
case map[string]interface{}:
hash = t
default:
p.bug("Expected hash to have type 'map[string]interface{}', but "+
"it has '%T' instead.", tmpHash)
}
}
keyContext = append(keyContext, key)
if _, ok := hash[key]; ok {
// Typically, if the given key has already been set, then we have
// to raise an error since duplicate keys are disallowed. However,
// it's possible that a key was previously defined implicitly. In this
// case, it is allowed to be redefined concretely. (See the
// `tests/valid/implicit-and-explicit-after.toml` test in `toml-test`.)
//
// But we have to make sure to stop marking it as an implicit. (So that
// another redefinition provokes an error.)
//
// Note that since it has already been defined (as a hash), we don't
// want to overwrite it. So our business is done.
if p.isImplicit(keyContext) {
p.removeImplicit(keyContext)
return
}
// Otherwise, we have a concrete key trying to override a previous
// key, which is *always* wrong.
p.panicf("Key '%s' has already been defined.", keyContext)
}
hash[key] = value
}
// setType sets the type of a particular value at a given key.
// It should be called immediately AFTER setValue.
//
// Note that if `key` is empty, then the type given will be applied to the
// current context (which is either a table or an array of tables).
func (p *parser) setType(key string, typ tomlType) {
keyContext := make(Key, 0, len(p.context)+1)
for _, k := range p.context {
keyContext = append(keyContext, k)
}
if len(key) > 0 { // allow type setting for hashes
keyContext = append(keyContext, key)
}
p.types[keyContext.String()] = typ
}
// addImplicit sets the given Key as having been created implicitly.
func (p *parser) addImplicit(key Key) {
p.implicits[key.String()] = true
}
// removeImplicit stops tagging the given key as having been implicitly
// created.
func (p *parser) removeImplicit(key Key) {
p.implicits[key.String()] = false
}
// isImplicit returns true if the key group pointed to by the key was created
// implicitly.
func (p *parser) isImplicit(key Key) bool {
return p.implicits[key.String()]
}
// current returns the full key name of the current context.
func (p *parser) current() string {
if len(p.currentKey) == 0 {
return p.context.String()
}
if len(p.context) == 0 {
return p.currentKey
}
return fmt.Sprintf("%s.%s", p.context, p.currentKey)
}
func stripFirstNewline(s string) string {
if len(s) == 0 || s[0] != '\n' {
return s
}
return s[1:]
}
func stripEscapedWhitespace(s string) string {
esc := strings.Split(s, "\\\n")
if len(esc) > 1 {
for i := 1; i < len(esc); i++ {
esc[i] = strings.TrimLeftFunc(esc[i], unicode.IsSpace)
}
}
return strings.Join(esc, "")
}
func (p *parser) replaceEscapes(str string) string {
var replaced []rune
s := []byte(str)
r := 0
for r < len(s) {
if s[r] != '\\' {
c, size := utf8.DecodeRune(s[r:])
r += size
replaced = append(replaced, c)
continue
}
r += 1
if r >= len(s) {
p.bug("Escape sequence at end of string.")
return ""
}
switch s[r] {
default:
p.bug("Expected valid escape code after \\, but got %q.", s[r])
return ""
case 'b':
replaced = append(replaced, rune(0x0008))
r += 1
case 't':
replaced = append(replaced, rune(0x0009))
r += 1
case 'n':
replaced = append(replaced, rune(0x000A))
r += 1
case 'f':
replaced = append(replaced, rune(0x000C))
r += 1
case 'r':
replaced = append(replaced, rune(0x000D))
r += 1
case '"':
replaced = append(replaced, rune(0x0022))
r += 1
case '\\':
replaced = append(replaced, rune(0x005C))
r += 1
case 'u':
// At this point, we know we have a Unicode escape of the form
// `uXXXX` at [r, r+5). (Because the lexer guarantees this
// for us.)
escaped := p.asciiEscapeToUnicode(s[r+1 : r+5])
replaced = append(replaced, escaped)
r += 5
case 'U':
// At this point, we know we have a Unicode escape of the form
// `uXXXX` at [r, r+9). (Because the lexer guarantees this
// for us.)
escaped := p.asciiEscapeToUnicode(s[r+1 : r+9])
replaced = append(replaced, escaped)
r += 9
}
}
return string(replaced)
}
func (p *parser) asciiEscapeToUnicode(bs []byte) rune {
s := string(bs)
hex, err := strconv.ParseUint(strings.ToLower(s), 16, 32)
if err != nil {
p.bug("Could not parse '%s' as a hexadecimal number, but the "+
"lexer claims it's OK: %s", s, err)
}
if !utf8.ValidRune(rune(hex)) {
p.panicf("Escaped character '\\u%s' is not valid UTF-8.", s)
}
return rune(hex)
}
func isStringType(ty itemType) bool {
return ty == itemString || ty == itemMultilineString ||
ty == itemRawString || ty == itemRawMultilineString
}

View File

@@ -1 +0,0 @@
au BufWritePost *.go silent!make tags > /dev/null 2>&1

View File

@@ -1,91 +0,0 @@
package toml
// tomlType represents any Go type that corresponds to a TOML type.
// While the first draft of the TOML spec has a simplistic type system that
// probably doesn't need this level of sophistication, we seem to be militating
// toward adding real composite types.
type tomlType interface {
typeString() string
}
// typeEqual accepts any two types and returns true if they are equal.
func typeEqual(t1, t2 tomlType) bool {
if t1 == nil || t2 == nil {
return false
}
return t1.typeString() == t2.typeString()
}
func typeIsHash(t tomlType) bool {
return typeEqual(t, tomlHash) || typeEqual(t, tomlArrayHash)
}
type tomlBaseType string
func (btype tomlBaseType) typeString() string {
return string(btype)
}
func (btype tomlBaseType) String() string {
return btype.typeString()
}
var (
tomlInteger tomlBaseType = "Integer"
tomlFloat tomlBaseType = "Float"
tomlDatetime tomlBaseType = "Datetime"
tomlString tomlBaseType = "String"
tomlBool tomlBaseType = "Bool"
tomlArray tomlBaseType = "Array"
tomlHash tomlBaseType = "Hash"
tomlArrayHash tomlBaseType = "ArrayHash"
)
// typeOfPrimitive returns a tomlType of any primitive value in TOML.
// Primitive values are: Integer, Float, Datetime, String and Bool.
//
// Passing a lexer item other than the following will cause a BUG message
// to occur: itemString, itemBool, itemInteger, itemFloat, itemDatetime.
func (p *parser) typeOfPrimitive(lexItem item) tomlType {
switch lexItem.typ {
case itemInteger:
return tomlInteger
case itemFloat:
return tomlFloat
case itemDatetime:
return tomlDatetime
case itemString:
return tomlString
case itemMultilineString:
return tomlString
case itemRawString:
return tomlString
case itemRawMultilineString:
return tomlString
case itemBool:
return tomlBool
}
p.bug("Cannot infer primitive type of lex item '%s'.", lexItem)
panic("unreachable")
}
// typeOfArray returns a tomlType for an array given a list of types of its
// values.
//
// In the current spec, if an array is homogeneous, then its type is always
// "Array". If the array is not homogeneous, an error is generated.
func (p *parser) typeOfArray(types []tomlType) tomlType {
// Empty arrays are cool.
if len(types) == 0 {
return tomlArray
}
theType := types[0]
for _, t := range types[1:] {
if !typeEqual(theType, t) {
p.panicf("Array contains values of type '%s' and '%s', but "+
"arrays must be homogeneous.", theType, t)
}
}
return tomlArray
}

View File

@@ -1,242 +0,0 @@
package toml
// Struct field handling is adapted from code in encoding/json:
//
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the Go distribution.
import (
"reflect"
"sort"
"sync"
)
// A field represents a single field found in a struct.
type field struct {
name string // the name of the field (`toml` tag included)
tag bool // whether field has a `toml` tag
index []int // represents the depth of an anonymous field
typ reflect.Type // the type of the field
}
// byName sorts field by name, breaking ties with depth,
// then breaking ties with "name came from toml tag", then
// breaking ties with index sequence.
type byName []field
func (x byName) Len() int { return len(x) }
func (x byName) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
func (x byName) Less(i, j int) bool {
if x[i].name != x[j].name {
return x[i].name < x[j].name
}
if len(x[i].index) != len(x[j].index) {
return len(x[i].index) < len(x[j].index)
}
if x[i].tag != x[j].tag {
return x[i].tag
}
return byIndex(x).Less(i, j)
}
// byIndex sorts field by index sequence.
type byIndex []field
func (x byIndex) Len() int { return len(x) }
func (x byIndex) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
func (x byIndex) Less(i, j int) bool {
for k, xik := range x[i].index {
if k >= len(x[j].index) {
return false
}
if xik != x[j].index[k] {
return xik < x[j].index[k]
}
}
return len(x[i].index) < len(x[j].index)
}
// typeFields returns a list of fields that TOML should recognize for the given
// type. The algorithm is breadth-first search over the set of structs to
// include - the top struct and then any reachable anonymous structs.
func typeFields(t reflect.Type) []field {
// Anonymous fields to explore at the current level and the next.
current := []field{}
next := []field{{typ: t}}
// Count of queued names for current level and the next.
count := map[reflect.Type]int{}
nextCount := map[reflect.Type]int{}
// Types already visited at an earlier level.
visited := map[reflect.Type]bool{}
// Fields found.
var fields []field
for len(next) > 0 {
current, next = next, current[:0]
count, nextCount = nextCount, map[reflect.Type]int{}
for _, f := range current {
if visited[f.typ] {
continue
}
visited[f.typ] = true
// Scan f.typ for fields to include.
for i := 0; i < f.typ.NumField(); i++ {
sf := f.typ.Field(i)
if sf.PkgPath != "" && !sf.Anonymous { // unexported
continue
}
opts := getOptions(sf.Tag)
if opts.skip {
continue
}
index := make([]int, len(f.index)+1)
copy(index, f.index)
index[len(f.index)] = i
ft := sf.Type
if ft.Name() == "" && ft.Kind() == reflect.Ptr {
// Follow pointer.
ft = ft.Elem()
}
// Record found field and index sequence.
if opts.name != "" || !sf.Anonymous || ft.Kind() != reflect.Struct {
tagged := opts.name != ""
name := opts.name
if name == "" {
name = sf.Name
}
fields = append(fields, field{name, tagged, index, ft})
if count[f.typ] > 1 {
// If there were multiple instances, add a second,
// so that the annihilation code will see a duplicate.
// It only cares about the distinction between 1 or 2,
// so don't bother generating any more copies.
fields = append(fields, fields[len(fields)-1])
}
continue
}
// Record new anonymous struct to explore in next round.
nextCount[ft]++
if nextCount[ft] == 1 {
f := field{name: ft.Name(), index: index, typ: ft}
next = append(next, f)
}
}
}
}
sort.Sort(byName(fields))
// Delete all fields that are hidden by the Go rules for embedded fields,
// except that fields with TOML tags are promoted.
// The fields are sorted in primary order of name, secondary order
// of field index length. Loop over names; for each name, delete
// hidden fields by choosing the one dominant field that survives.
out := fields[:0]
for advance, i := 0, 0; i < len(fields); i += advance {
// One iteration per name.
// Find the sequence of fields with the name of this first field.
fi := fields[i]
name := fi.name
for advance = 1; i+advance < len(fields); advance++ {
fj := fields[i+advance]
if fj.name != name {
break
}
}
if advance == 1 { // Only one field with this name
out = append(out, fi)
continue
}
dominant, ok := dominantField(fields[i : i+advance])
if ok {
out = append(out, dominant)
}
}
fields = out
sort.Sort(byIndex(fields))
return fields
}
// dominantField looks through the fields, all of which are known to
// have the same name, to find the single field that dominates the
// others using Go's embedding rules, modified by the presence of
// TOML tags. If there are multiple top-level fields, the boolean
// will be false: This condition is an error in Go and we skip all
// the fields.
func dominantField(fields []field) (field, bool) {
// The fields are sorted in increasing index-length order. The winner
// must therefore be one with the shortest index length. Drop all
// longer entries, which is easy: just truncate the slice.
length := len(fields[0].index)
tagged := -1 // Index of first tagged field.
for i, f := range fields {
if len(f.index) > length {
fields = fields[:i]
break
}
if f.tag {
if tagged >= 0 {
// Multiple tagged fields at the same level: conflict.
// Return no field.
return field{}, false
}
tagged = i
}
}
if tagged >= 0 {
return fields[tagged], true
}
// All remaining fields have the same length. If there's more than one,
// we have a conflict (two fields named "X" at the same level) and we
// return no field.
if len(fields) > 1 {
return field{}, false
}
return fields[0], true
}
var fieldCache struct {
sync.RWMutex
m map[reflect.Type][]field
}
// cachedTypeFields is like typeFields but uses a cache to avoid repeated work.
func cachedTypeFields(t reflect.Type) []field {
fieldCache.RLock()
f := fieldCache.m[t]
fieldCache.RUnlock()
if f != nil {
return f
}
// Compute fields without lock.
// Might duplicate effort but won't hold other computations back.
f = typeFields(t)
if f == nil {
f = []field{}
}
fieldCache.Lock()
if fieldCache.m == nil {
fieldCache.m = map[reflect.Type][]field{}
}
fieldCache.m[t] = f
fieldCache.Unlock()
return f
}

View File

@@ -1,3 +1,20 @@
# 1.3.1 (2017-07-10)
## Fixed
- Fixed #57: number comparisons in prerelease sometimes inaccurate
# 1.3.0 (2017-05-02)
## Added
- #45: Added json (un)marshaling support (thanks @mh-cbon)
- Stability marker. See https://masterminds.github.io/stability/
## Fixed
- #51: Fix handling of single digit tilde constraint (thanks @dgodd)
## Changed
- #55: The godoc icon moved from png to svg
# 1.2.3 (2017-04-03)
## Fixed

View File

@@ -9,7 +9,7 @@ The `semver` package provides the ability to work with [Semantic Versions](http:
[![Stability:
Active](https://masterminds.github.io/stability/active.svg)](https://masterminds.github.io/stability/active.html)
[![Build Status](https://travis-ci.org/Masterminds/semver.svg)](https://travis-ci.org/Masterminds/semver) [![Build status](https://ci.appveyor.com/api/projects/status/jfk66lib7hb985k8/branch/master?svg=true&passingText=windows%20build%20passing&failingText=windows%20build%20failing)](https://ci.appveyor.com/project/mattfarina/semver/branch/master) [![GoDoc](https://godoc.org/github.com/Masterminds/semver?status.png)](https://godoc.org/github.com/Masterminds/semver) [![Go Report Card](https://goreportcard.com/badge/github.com/Masterminds/semver)](https://goreportcard.com/report/github.com/Masterminds/semver)
[![Build Status](https://travis-ci.org/Masterminds/semver.svg)](https://travis-ci.org/Masterminds/semver) [![Build status](https://ci.appveyor.com/api/projects/status/jfk66lib7hb985k8/branch/master?svg=true&passingText=windows%20build%20passing&failingText=windows%20build%20failing)](https://ci.appveyor.com/project/mattfarina/semver/branch/master) [![GoDoc](https://godoc.org/github.com/Masterminds/semver?status.svg)](https://godoc.org/github.com/Masterminds/semver) [![Go Report Card](https://goreportcard.com/badge/github.com/Masterminds/semver)](https://goreportcard.com/report/github.com/Masterminds/semver)
## Parsing Semantic Versions

View File

@@ -175,7 +175,7 @@ func parseConstraint(c string) (*constraint, error) {
if isX(m[3]) {
ver = "0.0.0"
dirty = true
} else if isX(strings.TrimPrefix(m[4], ".")) {
} else if isX(strings.TrimPrefix(m[4], ".")) || m[4] == "" {
minorDirty = true
dirty = true
ver = fmt.Sprintf("%s.0.0%s", m[3], m[6])

View File

@@ -2,6 +2,7 @@ package semver
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"regexp"
@@ -291,6 +292,31 @@ func (v *Version) Compare(o *Version) int {
return comparePrerelease(ps, po)
}
// UnmarshalJSON implements JSON.Unmarshaler interface.
func (v *Version) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
temp, err := NewVersion(s)
if err != nil {
return err
}
v.major = temp.major
v.minor = temp.minor
v.patch = temp.patch
v.pre = temp.pre
v.metadata = temp.metadata
v.original = temp.original
temp = nil
return nil
}
// MarshalJSON implements JSON.Marshaler interface.
func (v *Version) MarshalJSON() ([]byte, error) {
return json.Marshal(v.String())
}
func compareSegment(v, o int64) int {
if v < o {
return -1
@@ -368,8 +394,29 @@ func comparePrePart(s, o string) int {
return -1
}
if s > o {
// When comparing strings "99" is greater than "103". To handle
// cases like this we need to detect numbers and compare them.
oi, n1 := strconv.ParseInt(o, 10, 64)
si, n2 := strconv.ParseInt(s, 10, 64)
// The case where both are strings compare the strings
if n1 != nil && n2 != nil {
if s > o {
return 1
}
return -1
} else if n1 != nil {
// o is a string and s is a number
return -1
} else if n2 != nil {
// s is a string and o is a number
return 1
}
// Both are numbers
if si > oi {
return 1
}
return -1
}

View File

@@ -1,5 +1,7 @@
package sprig
import "github.com/imdario/mergo"
func set(d map[string]interface{}, key string, value interface{}) map[string]interface{} {
d[key] = value
return d
@@ -72,3 +74,11 @@ func dict(v ...interface{}) map[string]interface{} {
}
return dict
}
func merge(dst map[string]interface{}, src map[string]interface{}) interface{} {
if err := mergo.Merge(&dst, src); err != nil {
// Swallow errors inside of a template.
return ""
}
return dst
}

View File

@@ -19,7 +19,7 @@ Date Functions
- date FORMAT TIME: Format a date, where a date is an integer type or a time.Time type, and
format is a time.Format formatting string.
- dateModify: Given a date, modify it with a duration: `date_modify "-1.5h" now`. If the duration doesn't
parse, it returns the time unaltered. See `time.ParseDuration` for info on duration strings.
parse, it returns the time unaltered. See `time.ParseDuration` for info on duration strings.
- now: Current time.Time, for feeding into date-related functions.
- htmlDate TIME: Format a date for use in the value field of an HTML "date" form element.
- dateInZone FORMAT TIME TZ: Like date, but takes three arguments: format, timestamp,
@@ -68,7 +68,7 @@ String Slice Functions:
- split: strings.Split, but as `split SEP STRING`. The results are returned
as a map with the indexes set to _N, where N is an integer starting from 0.
Use it like this: `{{$v := "foo/bar/baz" | split "/"}}{{$v._0}}` (Prints `foo`)
- splitList: strings.Split, but as `split SEP STRING`. The results are returned
- splitList: strings.Split, but as `split SEP STRING`. The results are returned
as an array.
- toStrings: convert a list to a list of strings. 'list 1 2 3 | toStrings' produces '["1" "2" "3"]'
- sortAlpha: sort a list lexicographically.
@@ -114,7 +114,7 @@ File Paths:
- base: Return the last element of a path. https://golang.org/pkg/path#Base
- dir: Remove the last element of a path. https://golang.org/pkg/path#Dir
- clean: Clean a path to the shortest equivalent name. (e.g. remove "foo/.."
from "foo/../bar.html") https://golang.org/pkg/path#Clean
from "foo/../bar.html") https://golang.org/pkg/path#Clean
- ext: https://golang.org/pkg/path#Ext
- isAbs: https://golang.org/pkg/path#IsAbs

View File

@@ -1,6 +1,7 @@
package sprig
import (
"errors"
"html/template"
"os"
"path"
@@ -10,6 +11,7 @@ import (
"time"
util "github.com/aokoli/goutils"
"github.com/huandu/xstrings"
)
// Produce the function map.
@@ -122,6 +124,9 @@ var genericMap = map[string]interface{}{
"randAscii": randAscii,
"randNumeric": randNumeric,
"swapcase": util.SwapCase,
"shuffle": xstrings.Shuffle,
"snakecase": xstrings.ToSnakeCase,
"camelcase": xstrings.ToCamelCase,
"wrap": func(l int, s string) string { return util.Wrap(s, l) },
"wrapWith": func(l int, sep, str string) string { return util.WrapCustom(str, l, sep, true) },
// Switch order so that "foobar" | contains "foo"
@@ -178,6 +183,9 @@ var genericMap = map[string]interface{}{
"biggest": max,
"max": max,
"min": min,
"ceil": ceil,
"floor": floor,
"round": round,
// string slices. Note that we reverse the order b/c that's better
// for template processing.
@@ -227,6 +235,7 @@ var genericMap = map[string]interface{}{
"keys": keys,
"pick": pick,
"omit": omit,
"merge": merge,
"append": push, "push": push,
"prepend": prepend,
@@ -249,4 +258,15 @@ var genericMap = map[string]interface{}{
// SemVer:
"semver": semver,
"semverCompare": semverCompare,
// Flow Control:
"fail": func(msg string) (string, error) { return "", errors.New(msg) },
// Regex
"regexMatch": regexMatch,
"regexFindAll": regexFindAll,
"regexFind": regexFind,
"regexReplaceAll": regexReplaceAll,
"regexReplaceAllLiteral": regexReplaceAllLiteral,
"regexSplit": regexSplit,
}

View File

@@ -1,8 +1,12 @@
hash: d366098dbe0d3a7bc5afdc55b91fb2a75e3443ff976e1a3b87a79e22cfead454
updated: 2017-04-07T09:31:43.889475111-06:00
hash: b9cc40bfd6dde74a94103b96700df1a9ab29a7fff5650216cf5a05f4fe72fb73
updated: 2017-05-02T16:01:04.617727646-06:00
imports:
- name: github.com/aokoli/goutils
version: 9c37978a95bd5c709a15883b6242714ea6709e64
- name: github.com/huandu/xstrings
version: 3959339b333561bf62a38b424fd41517c2c90f40
- name: github.com/imdario/mergo
version: 3e95a51e0639b4cf372f2ccf74c86749d747fbdc
- name: github.com/Masterminds/goutils
version: 45307ec16e3cd47cd841506c081f7afd8237d210
- name: github.com/Masterminds/semver
@@ -10,10 +14,20 @@ imports:
- name: github.com/satori/go.uuid
version: 879c5887cd475cd7864858769793b2ceb0d44feb
- name: github.com/stretchr/testify
version: 4d4bfba8f1d1027c4fdbe371823030df51419987
- name: golang.org/x/crypto
version: 420870623a70591d5e0b187c77c95455a1224ca6
version: e3a8ff8ce36581f87a15341206f205b1da467059
subpackages:
- assert
- name: golang.org/x/crypto
version: d172538b2cfce0c13cee31e647d0367aa8cd2486
subpackages:
- scrypt
- pbkdf2
devImports: []
- scrypt
testImports:
- name: github.com/davecgh/go-spew
version: 5215b55f46b2b919f50a1df0eaa5886afe4e3b3d
subpackages:
- spew
- name: github.com/pmezard/go-difflib
version: d8ed2627bdf02c080bf22230dbb337003b7aba2d
subpackages:
- difflib

View File

@@ -10,3 +10,6 @@ import:
- package: github.com/Masterminds/semver
version: v1.2.2
- package: github.com/stretchr/testify
- package: github.com/imdario/mergo
version: ~0.2.2
- package: github.com/huandu/xstrings

View File

@@ -127,3 +127,33 @@ func untilStep(start, stop, step int) []int {
}
return v
}
func floor(a interface{}) float64 {
aa := toFloat64(a)
return math.Floor(aa)
}
func ceil(a interface{}) float64 {
aa := toFloat64(a)
return math.Ceil(aa)
}
func round(a interface{}, p int, r_opt ...float64) float64 {
roundOn := .5
if len(r_opt) > 0 {
roundOn = r_opt[0]
}
val := toFloat64(a)
places := toFloat64(p)
var round float64
pow := math.Pow(10, places)
digit := pow * val
_, div := math.Modf(digit)
if div >= roundOn {
round = math.Ceil(digit)
} else {
round = math.Floor(digit)
}
return round / pow
}

35
vendor/github.com/Masterminds/sprig/regex.go generated vendored Normal file
View File

@@ -0,0 +1,35 @@
package sprig
import (
"regexp"
)
func regexMatch(regex string, s string) bool {
match, _ := regexp.MatchString(regex, s)
return match
}
func regexFindAll(regex string, s string, n int) []string {
r := regexp.MustCompile(regex)
return r.FindAllString(s, n)
}
func regexFind(regex string, s string) string {
r := regexp.MustCompile(regex)
return r.FindString(s)
}
func regexReplaceAll(regex string, s string, repl string) string {
r := regexp.MustCompile(regex)
return r.ReplaceAllString(s, repl)
}
func regexReplaceAllLiteral(regex string, s string, repl string) string {
r := regexp.MustCompile(regex)
return r.ReplaceAllLiteralString(s, repl)
}
func regexSplit(regex string, s string, n int) []string {
r := regexp.MustCompile(regex)
return r.Split(s, n)
}

8
vendor/github.com/aokoli/goutils/CHANGELOG.md generated vendored Normal file
View File

@@ -0,0 +1,8 @@
# 1.0.1 (2017-05-31)
## Fixed
- #21: Fix generation of alphanumeric strings (thanks @dbarranco)
# 1.0.0 (2014-04-30)
- Initial release.

View File

@@ -1,9 +1,10 @@
GoUtils
===========
[![Stability: Maintenance](https://masterminds.github.io/stability/maintenance.svg)](https://masterminds.github.io/stability/maintenance.html)
[![GoDoc](https://godoc.org/github.com/Masterminds/goutils?status.png)](https://godoc.org/github.com/Masterminds/goutils)
[![GoDoc](https://godoc.org/github.com/Masterminds/goutils?status.png)](https://godoc.org/github.com/Masterminds/goutils) [![Build Status](https://travis-ci.org/Masterminds/goutils.svg?branch=master)](https://travis-ci.org/Masterminds/goutils) [![Build status](https://ci.appveyor.com/api/projects/status/sc2b1ew0m7f0aiju?svg=true)](https://ci.appveyor.com/project/mattfarina/goutils)
GoUtils provides users with utility functions to manipulate strings in various ways. It is a Go implementation of some
GoUtils provides users with utility functions to manipulate strings in various ways. It is a Go implementation of some
string manipulation libraries of Java Apache Commons. GoUtils includes the following Java Apache Commons classes:
* WordUtils
* RandomStringUtils
@@ -13,11 +14,11 @@ string manipulation libraries of Java Apache Commons. GoUtils includes the follo
If you have Go set up on your system, from the GOPATH directory within the command line/terminal, enter this:
go get github.com/Masterminds/goutils
If you do not have Go set up on your system, please follow the [Go installation directions from the documenation](http://golang.org/doc/install), and then follow the instructions above to install GoUtils.
## Documentation
## Documentation
GoUtils doc is available here: [![GoDoc](https://godoc.org/github.com/Masterminds/goutils?status.png)](https://godoc.org/github.com/Masterminds/goutils)
@@ -25,12 +26,12 @@ GoUtils doc is available here: [![GoDoc](https://godoc.org/github.com/Mastermind
The code snippets below show examples of how to use GoUtils. Some functions return errors while others do not. The first instance below, which does not return an error, is the `Initials` function (located within the `wordutils.go` file).
package main
import (
"fmt"
"github.com/Masterminds/goutils"
)
func main() {
// EXAMPLE 1: A goutils function which returns no errors
@@ -40,35 +41,30 @@ The code snippets below show examples of how to use GoUtils. Some functions retu
Some functions return errors mainly due to illegal arguements used as parameters. The code example below illustrates how to deal with function that returns an error. In this instance, the function is the `Random` function (located within the `randomstringutils.go` file).
package main
import (
"fmt"
"github.com/Masterminds/goutils"
)
func main() {
// EXAMPLE 2: A goutils function which returns an error
rand1, err1 := goutils.Random (-1, 0, 0, true, true)
if err1 != nil {
if err1 != nil {
fmt.Println(err1) // Prints out error message because -1 was entered as the first parameter in goutils.Random(...)
} else {
fmt.Println(rand1)
fmt.Println(rand1)
}
}
## License
GoUtils is licensed under the Apache License, Version 2.0. Please check the LICENSE.txt file or visit http://www.apache.org/licenses/LICENSE-2.0 for a copy of the license.
GoUtils is licensed under the Apache License, Version 2.0. Please check the LICENSE.txt file or visit http://www.apache.org/licenses/LICENSE-2.0 for a copy of the license.
## Issue Reporting
Make suggestions or report issues using the Git issue tracker: https://github.com/Masterminds/goutils/issues
## Website
* [GoUtils webpage](http://Masterminds.github.io/goutils/)
## Mailing List
Contact [okolialex@gmail.com](mailto:okolialex@mail.com) to be added to the mailing list. You will get updates on the
status of the project and the potential direction it will be heading.

21
vendor/github.com/aokoli/goutils/appveyor.yml generated vendored Normal file
View File

@@ -0,0 +1,21 @@
version: build-{build}.{branch}
clone_folder: C:\gopath\src\github.com\Masterminds\goutils
shallow_clone: true
environment:
GOPATH: C:\gopath
platform:
- x64
build: off
install:
- go version
- go env
test_script:
- go test -v
deploy: off

View File

@@ -20,6 +20,7 @@ import (
"fmt"
"math"
"math/rand"
"regexp"
"time"
"unicode"
)
@@ -101,7 +102,24 @@ Returns:
error - an error stemming from an invalid parameter within underlying function, RandomSeed(...)
*/
func RandomAlphaNumeric(count int) (string, error) {
return Random(count, 0, 0, true, true)
RandomString, err := Random(count, 0, 0, true, true)
if err != nil {
return "", fmt.Errorf("Error: %s", err)
}
match, err := regexp.MatchString("([0-9]+)", RandomString)
if err != nil {
panic(err)
}
if !match {
//Get the position between 0 and the length of the string-1 to insert a random number
position := rand.Intn(count)
//Insert a random number between [0-9] in the position
RandomString = RandomString[:position] + string('0'+rand.Intn(10)) + RandomString[position+1:]
return RandomString, err
}
return RandomString, err
}
/*

23
vendor/github.com/huandu/xstrings/CONTRIBUTING.md generated vendored Normal file
View File

@@ -0,0 +1,23 @@
# Contributing #
Thanks for your contribution in advance. No matter what you will contribute to this project, pull request or bug report or feature discussion, it's always highly appreciated.
## New API or feature ##
I want to speak more about how to add new functions to this package.
Package `xstring` is a collection of useful string functions which should be implemented in Go. It's a bit subject to say which function should be included and which should not. I set up following rules in order to make it clear and as objective as possible.
* Rule 1: Only string algorithm, which takes string as input, can be included.
* Rule 2: If a function has been implemented in package `string`, it must not be included.
* Rule 3: If a function is not language neutral, it must not be included.
* Rule 4: If a function is a part of standard library in other languages, it can be included.
* Rule 5: If a function is quite useful in some famous framework or library, it can be included.
New function must be discussed in project issues before submitting any code. If a pull request with new functions is sent without any ref issue, it will be rejected.
## Pull request ##
Pull request is always welcome. Just make sure you have run `go fmt` and all test cases passed before submit.
If the pull request is to add a new API or feature, don't forget to update README.md and add new API in function list.

22
vendor/github.com/huandu/xstrings/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,22 @@
The MIT License (MIT)
Copyright (c) 2015 Huan Du
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

114
vendor/github.com/huandu/xstrings/README.md generated vendored Normal file
View File

@@ -0,0 +1,114 @@
# xstrings #
[![Build Status](https://travis-ci.org/huandu/xstrings.png?branch=master)](https://travis-ci.org/huandu/xstrings)
[![GoDoc](https://godoc.org/github.com/huandu/xstrings?status.svg)](https://godoc.org/github.com/huandu/xstrings)
Go package [xstrings](https://godoc.org/github.com/huandu/xstrings) is a collection of string functions, which are widely used in other languages but absent in Go package [strings](http://golang.org/pkg/strings).
All functions are well tested and carefully tuned for performance.
## Propose a new function ##
Please review [contributing guideline](CONTRIBUTING.md) and [create new issue](https://github.com/huandu/xstrings/issues) to state why it should be included.
## Install ##
Use `go get` to install this library.
go get github.com/huandu/xstrings
## API document ##
See [GoDoc](https://godoc.org/github.com/huandu/xstrings) for full document.
## Function list ##
Go functions have a unique naming style. One, who has experience in other language but new in Go, may have difficulties to find out right string function to use.
Here is a list of functions in [strings](http://golang.org/pkg/strings) and [xstrings](https://godoc.org/github.com/huandu/xstrings) with enough extra information about how to map these functions to their friends in other languages. Hope this list could be helpful for fresh gophers.
### Package `xstrings` functions ###
*Keep this table sorted by Function in ascending order.*
| Function | Friends | # |
| -------- | ------- | --- |
| [Center](https://godoc.org/github.com/huandu/xstrings#Center) | `str.center` in Python; `String#center` in Ruby | [#30](https://github.com/huandu/xstrings/issues/30) |
| [Count](https://godoc.org/github.com/huandu/xstrings#Count) | `String#count` in Ruby | [#16](https://github.com/huandu/xstrings/issues/16) |
| [Delete](https://godoc.org/github.com/huandu/xstrings#Delete) | `String#delete` in Ruby | [#17](https://github.com/huandu/xstrings/issues/17) |
| [ExpandTabs](https://godoc.org/github.com/huandu/xstrings#ExpandTabs) | `str.expandtabs` in Python | [#27](https://github.com/huandu/xstrings/issues/27) |
| [FirstRuneToLower](https://godoc.org/github.com/huandu/xstrings#FirstRuneToLower) | `lcfirst` in PHP or Perl | [#15](https://github.com/huandu/xstrings/issues/15) |
| [FirstRuneToUpper](https://godoc.org/github.com/huandu/xstrings#FirstRuneToUpper) | `String#capitalize` in Ruby; `ucfirst` in PHP or Perl | [#15](https://github.com/huandu/xstrings/issues/15) |
| [Insert](https://godoc.org/github.com/huandu/xstrings#Insert) | `String#insert` in Ruby | [#18](https://github.com/huandu/xstrings/issues/18) |
| [LastPartition](https://godoc.org/github.com/huandu/xstrings#LastPartition) | `str.rpartition` in Python; `String#rpartition` in Ruby | [#19](https://github.com/huandu/xstrings/issues/19) |
| [LeftJustify](https://godoc.org/github.com/huandu/xstrings#LeftJustify) | `str.ljust` in Python; `String#ljust` in Ruby | [#28](https://github.com/huandu/xstrings/issues/28) |
| [Len](https://godoc.org/github.com/huandu/xstrings#Len) | `mb_strlen` in PHP | [#23](https://github.com/huandu/xstrings/issues/23) |
| [Partition](https://godoc.org/github.com/huandu/xstrings#Partition) | `str.partition` in Python; `String#partition` in Ruby | [#10](https://github.com/huandu/xstrings/issues/10) |
| [Reverse](https://godoc.org/github.com/huandu/xstrings#Reverse) | `String#reverse` in Ruby; `strrev` in PHP; `reverse` in Perl | [#7](https://github.com/huandu/xstrings/issues/7) |
| [RightJustify](https://godoc.org/github.com/huandu/xstrings#RightJustify) | `str.rjust` in Python; `String#rjust` in Ruby | [#29](https://github.com/huandu/xstrings/issues/29) |
| [RuneWidth](https://godoc.org/github.com/huandu/xstrings#RuneWidth) | - | [#27](https://github.com/huandu/xstrings/issues/27) |
| [Scrub](https://godoc.org/github.com/huandu/xstrings#Scrub) | `String#scrub` in Ruby | [#20](https://github.com/huandu/xstrings/issues/20) |
| [Shuffle](https://godoc.org/github.com/huandu/xstrings#Shuffle) | `str_shuffle` in PHP | [#13](https://github.com/huandu/xstrings/issues/13) |
| [ShuffleSource](https://godoc.org/github.com/huandu/xstrings#ShuffleSource) | `str_shuffle` in PHP | [#13](https://github.com/huandu/xstrings/issues/13) |
| [Slice](https://godoc.org/github.com/huandu/xstrings#Slice) | `mb_substr` in PHP | [#9](https://github.com/huandu/xstrings/issues/9) |
| [Squeeze](https://godoc.org/github.com/huandu/xstrings#Squeeze) | `String#squeeze` in Ruby | [#11](https://github.com/huandu/xstrings/issues/11) |
| [Successor](https://godoc.org/github.com/huandu/xstrings#Successor) | `String#succ` or `String#next` in Ruby | [#22](https://github.com/huandu/xstrings/issues/22) |
| [SwapCase](https://godoc.org/github.com/huandu/xstrings#SwapCase) | `str.swapcase` in Python; `String#swapcase` in Ruby | [#12](https://github.com/huandu/xstrings/issues/12) |
| [ToCamelCase](https://godoc.org/github.com/huandu/xstrings#ToCamelCase) | `String#camelize` in RoR | [#1](https://github.com/huandu/xstrings/issues/1) |
| [ToSnakeCase](https://godoc.org/github.com/huandu/xstrings#ToSnakeCase) | `String#underscore` in RoR | [#1](https://github.com/huandu/xstrings/issues/1) |
| [Translate](https://godoc.org/github.com/huandu/xstrings#Translate) | `str.translate` in Python; `String#tr` in Ruby; `strtr` in PHP; `tr///` in Perl | [#21](https://github.com/huandu/xstrings/issues/21) |
| [Width](https://godoc.org/github.com/huandu/xstrings#Width) | `mb_strwidth` in PHP | [#26](https://github.com/huandu/xstrings/issues/26) |
| [WordCount](https://godoc.org/github.com/huandu/xstrings#WordCount) | `str_word_count` in PHP | [#14](https://github.com/huandu/xstrings/issues/14) |
| [WordSplit](https://godoc.org/github.com/huandu/xstrings#WordSplit) | - | [#14](https://github.com/huandu/xstrings/issues/14) |
### Package `strings` functions ###
*Keep this table sorted by Function in ascending order.*
| Function | Friends |
| -------- | ------- |
| [Contains](http://golang.org/pkg/strings/#Contains) | `String#include?` in Ruby |
| [ContainsAny](http://golang.org/pkg/strings/#ContainsAny) | - |
| [ContainsRune](http://golang.org/pkg/strings/#ContainsRune) | - |
| [Count](http://golang.org/pkg/strings/#Count) | `str.count` in Python; `substr_count` in PHP |
| [EqualFold](http://golang.org/pkg/strings/#EqualFold) | `stricmp` in PHP; `String#casecmp` in Ruby |
| [Fields](http://golang.org/pkg/strings/#Fields) | `str.split` in Python; `split` in Perl; `String#split` in Ruby |
| [FieldsFunc](http://golang.org/pkg/strings/#FieldsFunc) | - |
| [HasPrefix](http://golang.org/pkg/strings/#HasPrefix) | `str.startswith` in Python; `String#start_with?` in Ruby |
| [HasSuffix](http://golang.org/pkg/strings/#HasSuffix) | `str.endswith` in Python; `String#end_with?` in Ruby |
| [Index](http://golang.org/pkg/strings/#Index) | `str.index` in Python; `String#index` in Ruby; `strpos` in PHP; `index` in Perl |
| [IndexAny](http://golang.org/pkg/strings/#IndexAny) | - |
| [IndexByte](http://golang.org/pkg/strings/#IndexByte) | - |
| [IndexFunc](http://golang.org/pkg/strings/#IndexFunc) | - |
| [IndexRune](http://golang.org/pkg/strings/#IndexRune) | - |
| [Join](http://golang.org/pkg/strings/#Join) | `str.join` in Python; `Array#join` in Ruby; `implode` in PHP; `join` in Perl |
| [LastIndex](http://golang.org/pkg/strings/#LastIndex) | `str.rindex` in Python; `String#rindex`; `strrpos` in PHP; `rindex` in Perl |
| [LastIndexAny](http://golang.org/pkg/strings/#LastIndexAny) | - |
| [LastIndexFunc](http://golang.org/pkg/strings/#LastIndexFunc) | - |
| [Map](http://golang.org/pkg/strings/#Map) | `String#each_codepoint` in Ruby |
| [Repeat](http://golang.org/pkg/strings/#Repeat) | operator `*` in Python and Ruby; `str_repeat` in PHP |
| [Replace](http://golang.org/pkg/strings/#Replace) | `str.replace` in Python; `String#sub` in Ruby; `str_replace` in PHP |
| [Split](http://golang.org/pkg/strings/#Split) | `str.split` in Python; `String#split` in Ruby; `explode` in PHP; `split` in Perl |
| [SplitAfter](http://golang.org/pkg/strings/#SplitAfter) | - |
| [SplitAfterN](http://golang.org/pkg/strings/#SplitAfterN) | - |
| [SplitN](http://golang.org/pkg/strings/#SplitN) | `str.split` in Python; `String#split` in Ruby; `explode` in PHP; `split` in Perl |
| [Title](http://golang.org/pkg/strings/#Title) | `str.title` in Python |
| [ToLower](http://golang.org/pkg/strings/#ToLower) | `str.lower` in Python; `String#downcase` in Ruby; `strtolower` in PHP; `lc` in Perl |
| [ToLowerSpecial](http://golang.org/pkg/strings/#ToLowerSpecial) | - |
| [ToTitle](http://golang.org/pkg/strings/#ToTitle) | - |
| [ToTitleSpecial](http://golang.org/pkg/strings/#ToTitleSpecial) | - |
| [ToUpper](http://golang.org/pkg/strings/#ToUpper) | `str.upper` in Python; `String#upcase` in Ruby; `strtoupper` in PHP; `uc` in Perl |
| [ToUpperSpecial](http://golang.org/pkg/strings/#ToUpperSpecial) | - |
| [Trim](http://golang.org/pkg/strings/#Trim) | `str.strip` in Python; `String#strip` in Ruby; `trim` in PHP |
| [TrimFunc](http://golang.org/pkg/strings/#TrimFunc) | - |
| [TrimLeft](http://golang.org/pkg/strings/#TrimLeft) | `str.lstrip` in Python; `String#lstrip` in Ruby; `ltrim` in PHP |
| [TrimLeftFunc](http://golang.org/pkg/strings/#TrimLeftFunc) | - |
| [TrimPrefix](http://golang.org/pkg/strings/#TrimPrefix) | - |
| [TrimRight](http://golang.org/pkg/strings/#TrimRight) | `str.rstrip` in Python; `String#rstrip` in Ruby; `rtrim` in PHP |
| [TrimRightFunc](http://golang.org/pkg/strings/#TrimRightFunc) | - |
| [TrimSpace](http://golang.org/pkg/strings/#TrimSpace) | `str.strip` in Python; `String#strip` in Ruby; `trim` in PHP |
| [TrimSuffix](http://golang.org/pkg/strings/#TrimSuffix) | `String#chomp` in Ruby; `chomp` in Perl |
## License ##
This library is licensed under MIT license. See LICENSE for details.

25
vendor/github.com/huandu/xstrings/common.go generated vendored Normal file
View File

@@ -0,0 +1,25 @@
// Copyright 2015 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.
package xstrings
import (
"bytes"
)
const _BUFFER_INIT_GROW_SIZE_MAX = 2048
// Lazy initialize a buffer.
func allocBuffer(orig, cur string) *bytes.Buffer {
output := &bytes.Buffer{}
maxSize := len(orig) * 4
// Avoid to reserve too much memory at once.
if maxSize > _BUFFER_INIT_GROW_SIZE_MAX {
maxSize = _BUFFER_INIT_GROW_SIZE_MAX
}
output.Grow(maxSize)
output.WriteString(orig[:len(orig)-len(cur)])
return output
}

357
vendor/github.com/huandu/xstrings/convert.go generated vendored Normal file
View File

@@ -0,0 +1,357 @@
// Copyright 2015 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.
package xstrings
import (
"bytes"
"math/rand"
"unicode"
"unicode/utf8"
)
// ToCamelCase can convert all lower case characters behind underscores
// to upper case character.
// Underscore character will be removed in result except following cases.
// * More than 1 underscore.
// "a__b" => "A_B"
// * At the beginning of string.
// "_a" => "_A"
// * At the end of string.
// "ab_" => "Ab_"
func ToCamelCase(str string) string {
if len(str) == 0 {
return ""
}
buf := &bytes.Buffer{}
var r0, r1 rune
var size int
// leading '_' will appear in output.
for len(str) > 0 {
r0, size = utf8.DecodeRuneInString(str)
str = str[size:]
if r0 != '_' {
break
}
buf.WriteRune(r0)
}
if len(str) == 0 {
return buf.String()
}
buf.WriteRune(unicode.ToUpper(r0))
r0, size = utf8.DecodeRuneInString(str)
str = str[size:]
for len(str) > 0 {
r1 = r0
r0, size = utf8.DecodeRuneInString(str)
str = str[size:]
if r1 == '_' && r0 != '_' {
r0 = unicode.ToUpper(r0)
} else {
buf.WriteRune(r1)
}
}
buf.WriteRune(r0)
return buf.String()
}
// ToSnakeCase can convert all upper case characters in a string to
// underscore format.
//
// Some samples.
// "FirstName" => "first_name"
// "HTTPServer" => "http_server"
// "NoHTTPS" => "no_https"
// "GO_PATH" => "go_path"
// "GO PATH" => "go_path" // space is converted to underscore.
// "GO-PATH" => "go_path" // hyphen is converted to underscore.
func ToSnakeCase(str string) string {
if len(str) == 0 {
return ""
}
buf := &bytes.Buffer{}
var prev, r0, r1 rune
var size int
r0 = '_'
for len(str) > 0 {
prev = r0
r0, size = utf8.DecodeRuneInString(str)
str = str[size:]
switch {
case r0 == utf8.RuneError:
buf.WriteByte(byte(str[0]))
case unicode.IsUpper(r0):
if prev != '_' {
buf.WriteRune('_')
}
buf.WriteRune(unicode.ToLower(r0))
if len(str) == 0 {
break
}
r0, size = utf8.DecodeRuneInString(str)
str = str[size:]
if !unicode.IsUpper(r0) {
buf.WriteRune(r0)
break
}
// find next non-upper-case character and insert `_` properly.
// it's designed to convert `HTTPServer` to `http_server`.
// if there are more than 2 adjacent upper case characters in a word,
// treat them as an abbreviation plus a normal word.
for len(str) > 0 {
r1 = r0
r0, size = utf8.DecodeRuneInString(str)
str = str[size:]
if r0 == utf8.RuneError {
buf.WriteRune(unicode.ToLower(r1))
buf.WriteByte(byte(str[0]))
break
}
if !unicode.IsUpper(r0) {
if r0 == '_' || r0 == ' ' || r0 == '-' {
r0 = '_'
buf.WriteRune(unicode.ToLower(r1))
} else {
buf.WriteRune('_')
buf.WriteRune(unicode.ToLower(r1))
buf.WriteRune(r0)
}
break
}
buf.WriteRune(unicode.ToLower(r1))
}
if len(str) == 0 || r0 == '_' {
buf.WriteRune(unicode.ToLower(r0))
break
}
default:
if r0 == ' ' || r0 == '-' {
r0 = '_'
}
buf.WriteRune(r0)
}
}
return buf.String()
}
// SwapCase will swap characters case from upper to lower or lower to upper.
func SwapCase(str string) string {
var r rune
var size int
buf := &bytes.Buffer{}
for len(str) > 0 {
r, size = utf8.DecodeRuneInString(str)
switch {
case unicode.IsUpper(r):
buf.WriteRune(unicode.ToLower(r))
case unicode.IsLower(r):
buf.WriteRune(unicode.ToUpper(r))
default:
buf.WriteRune(r)
}
str = str[size:]
}
return buf.String()
}
// FirstRuneToUpper converts first rune to upper case if necessary.
func FirstRuneToUpper(str string) string {
if str == "" {
return str
}
r, size := utf8.DecodeRuneInString(str)
if !unicode.IsLower(r) {
return str
}
buf := &bytes.Buffer{}
buf.WriteRune(unicode.ToUpper(r))
buf.WriteString(str[size:])
return buf.String()
}
// FirstRuneToLower converts first rune to lower case if necessary.
func FirstRuneToLower(str string) string {
if str == "" {
return str
}
r, size := utf8.DecodeRuneInString(str)
if !unicode.IsUpper(r) {
return str
}
buf := &bytes.Buffer{}
buf.WriteRune(unicode.ToLower(r))
buf.WriteString(str[size:])
return buf.String()
}
// Shuffle randomizes runes in a string and returns the result.
// It uses default random source in `math/rand`.
func Shuffle(str string) string {
if str == "" {
return str
}
runes := []rune(str)
index := 0
for i := len(runes) - 1; i > 0; i-- {
index = rand.Intn(i + 1)
if i != index {
runes[i], runes[index] = runes[index], runes[i]
}
}
return string(runes)
}
// ShuffleSource randomizes runes in a string with given random source.
func ShuffleSource(str string, src rand.Source) string {
if str == "" {
return str
}
runes := []rune(str)
index := 0
r := rand.New(src)
for i := len(runes) - 1; i > 0; i-- {
index = r.Intn(i + 1)
if i != index {
runes[i], runes[index] = runes[index], runes[i]
}
}
return string(runes)
}
// Successor returns the successor to string.
//
// If there is one alphanumeric rune is found in string, increase the rune by 1.
// If increment generates a "carry", the rune to the left of it is incremented.
// This process repeats until there is no carry, adding an additional rune if necessary.
//
// If there is no alphanumeric rune, the rightmost rune will be increased by 1
// regardless whether the result is a valid rune or not.
//
// Only following characters are alphanumeric.
// * a - z
// * A - Z
// * 0 - 9
//
// Samples (borrowed from ruby's String#succ document):
// "abcd" => "abce"
// "THX1138" => "THX1139"
// "<<koala>>" => "<<koalb>>"
// "1999zzz" => "2000aaa"
// "ZZZ9999" => "AAAA0000"
// "***" => "**+"
func Successor(str string) string {
if str == "" {
return str
}
var r rune
var i int
carry := ' '
runes := []rune(str)
l := len(runes)
lastAlphanumeric := l
for i = l - 1; i >= 0; i-- {
r = runes[i]
if ('a' <= r && r <= 'y') ||
('A' <= r && r <= 'Y') ||
('0' <= r && r <= '8') {
runes[i]++
carry = ' '
lastAlphanumeric = i
break
}
switch r {
case 'z':
runes[i] = 'a'
carry = 'a'
lastAlphanumeric = i
case 'Z':
runes[i] = 'A'
carry = 'A'
lastAlphanumeric = i
case '9':
runes[i] = '0'
carry = '0'
lastAlphanumeric = i
}
}
// Needs to add one character for carry.
if i < 0 && carry != ' ' {
buf := &bytes.Buffer{}
buf.Grow(l + 4) // Reserve enough space for write.
if lastAlphanumeric != 0 {
buf.WriteString(str[:lastAlphanumeric])
}
buf.WriteRune(carry)
for _, r = range runes[lastAlphanumeric:] {
buf.WriteRune(r)
}
return buf.String()
}
// No alphanumeric character. Simply increase last rune's value.
if lastAlphanumeric == l {
runes[l-1]++
}
return string(runes)
}

120
vendor/github.com/huandu/xstrings/count.go generated vendored Normal file
View File

@@ -0,0 +1,120 @@
// Copyright 2015 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.
package xstrings
import (
"unicode"
"unicode/utf8"
)
// Get str's utf8 rune length.
func Len(str string) int {
return utf8.RuneCountInString(str)
}
// Count number of words in a string.
//
// Word is defined as a locale dependent string containing alphabetic characters,
// which may also contain but not start with `'` and `-` characters.
func WordCount(str string) int {
var r rune
var size, n int
inWord := false
for len(str) > 0 {
r, size = utf8.DecodeRuneInString(str)
switch {
case isAlphabet(r):
if !inWord {
inWord = true
n++
}
case inWord && (r == '\'' || r == '-'):
// Still in word.
default:
inWord = false
}
str = str[size:]
}
return n
}
const minCJKCharacter = '\u3400'
// Checks r is a letter but not CJK character.
func isAlphabet(r rune) bool {
if !unicode.IsLetter(r) {
return false
}
switch {
// Quick check for non-CJK character.
case r < minCJKCharacter:
return true
// Common CJK characters.
case r >= '\u4E00' && r <= '\u9FCC':
return false
// Rare CJK characters.
case r >= '\u3400' && r <= '\u4D85':
return false
// Rare and historic CJK characters.
case r >= '\U00020000' && r <= '\U0002B81D':
return false
}
return true
}
// Width returns string width in monotype font.
// Multi-byte characters are usually twice the width of single byte characters.
//
// Algorithm comes from `mb_strwidth` in PHP.
// http://php.net/manual/en/function.mb-strwidth.php
func Width(str string) int {
var r rune
var size, n int
for len(str) > 0 {
r, size = utf8.DecodeRuneInString(str)
n += RuneWidth(r)
str = str[size:]
}
return n
}
// RuneWidth returns character width in monotype font.
// Multi-byte characters are usually twice the width of single byte characters.
//
// Algorithm comes from `mb_strwidth` in PHP.
// http://php.net/manual/en/function.mb-strwidth.php
func RuneWidth(r rune) int {
switch {
case r == utf8.RuneError || r < '\x20':
return 0
case '\x20' <= r && r < '\u2000':
return 1
case '\u2000' <= r && r < '\uFF61':
return 2
case '\uFF61' <= r && r < '\uFFA0':
return 1
case '\uFFA0' <= r:
return 2
}
return 0
}

8
vendor/github.com/huandu/xstrings/doc.go generated vendored Normal file
View File

@@ -0,0 +1,8 @@
// Copyright 2015 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.
// Package `xstrings` is to provide string algorithms which are useful but not included in `strings` package.
// See project home page for details. https://github.com/huandu/xstrings
//
// Package `xstrings` assumes all strings are encoded in utf8.
package xstrings

170
vendor/github.com/huandu/xstrings/format.go generated vendored Normal file
View File

@@ -0,0 +1,170 @@
// Copyright 2015 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.
package xstrings
import (
"bytes"
"unicode/utf8"
)
// ExpandTabs can expand tabs ('\t') rune in str to one or more spaces dpending on
// current column and tabSize.
// The column number is reset to zero after each newline ('\n') occurring in the str.
//
// ExpandTabs uses RuneWidth to decide rune's width.
// For example, CJK characters will be treated as two characters.
//
// If tabSize <= 0, ExpandTabs panics with error.
//
// Samples:
// ExpandTabs("a\tbc\tdef\tghij\tk", 4) => "a bc def ghij k"
// ExpandTabs("abcdefg\thij\nk\tl", 4) => "abcdefg hij\nk l"
// ExpandTabs("z中\t文\tw", 4) => "z中 文 w"
func ExpandTabs(str string, tabSize int) string {
if tabSize <= 0 {
panic("tab size must be positive")
}
var r rune
var i, size, column, expand int
var output *bytes.Buffer
orig := str
for len(str) > 0 {
r, size = utf8.DecodeRuneInString(str)
if r == '\t' {
expand = tabSize - column%tabSize
if output == nil {
output = allocBuffer(orig, str)
}
for i = 0; i < expand; i++ {
output.WriteByte(byte(' '))
}
column += expand
} else {
if r == '\n' {
column = 0
} else {
column += RuneWidth(r)
}
if output != nil {
output.WriteRune(r)
}
}
str = str[size:]
}
if output == nil {
return orig
}
return output.String()
}
// LeftJustify returns a string with pad string at right side if str's rune length is smaller than length.
// If str's rune length is larger than length, str itself will be returned.
//
// If pad is an empty string, str will be returned.
//
// Samples:
// LeftJustify("hello", 4, " ") => "hello"
// LeftJustify("hello", 10, " ") => "hello "
// LeftJustify("hello", 10, "123") => "hello12312"
func LeftJustify(str string, length int, pad string) string {
l := Len(str)
if l >= length || pad == "" {
return str
}
remains := length - l
padLen := Len(pad)
output := &bytes.Buffer{}
output.Grow(len(str) + (remains/padLen+1)*len(pad))
output.WriteString(str)
writePadString(output, pad, padLen, remains)
return output.String()
}
// RightJustify returns a string with pad string at left side if str's rune length is smaller than length.
// If str's rune length is larger than length, str itself will be returned.
//
// If pad is an empty string, str will be returned.
//
// Samples:
// RightJustify("hello", 4, " ") => "hello"
// RightJustify("hello", 10, " ") => " hello"
// RightJustify("hello", 10, "123") => "12312hello"
func RightJustify(str string, length int, pad string) string {
l := Len(str)
if l >= length || pad == "" {
return str
}
remains := length - l
padLen := Len(pad)
output := &bytes.Buffer{}
output.Grow(len(str) + (remains/padLen+1)*len(pad))
writePadString(output, pad, padLen, remains)
output.WriteString(str)
return output.String()
}
// Center returns a string with pad string at both side if str's rune length is smaller than length.
// If str's rune length is larger than length, str itself will be returned.
//
// If pad is an empty string, str will be returned.
//
// Samples:
// Center("hello", 4, " ") => "hello"
// Center("hello", 10, " ") => " hello "
// Center("hello", 10, "123") => "12hello123"
func Center(str string, length int, pad string) string {
l := Len(str)
if l >= length || pad == "" {
return str
}
remains := length - l
padLen := Len(pad)
output := &bytes.Buffer{}
output.Grow(len(str) + (remains/padLen+1)*len(pad))
writePadString(output, pad, padLen, remains/2)
output.WriteString(str)
writePadString(output, pad, padLen, (remains+1)/2)
return output.String()
}
func writePadString(output *bytes.Buffer, pad string, padLen, remains int) {
var r rune
var size int
repeats := remains / padLen
for i := 0; i < repeats; i++ {
output.WriteString(pad)
}
remains = remains % padLen
if remains != 0 {
for i := 0; i < remains; i++ {
r, size = utf8.DecodeRuneInString(pad)
output.WriteRune(r)
pad = pad[size:]
}
}
}

217
vendor/github.com/huandu/xstrings/manipulate.go generated vendored Normal file
View File

@@ -0,0 +1,217 @@
// Copyright 2015 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.
package xstrings
import (
"bytes"
"strings"
"unicode/utf8"
)
// Reverse a utf8 encoded string.
func Reverse(str string) string {
var size int
tail := len(str)
buf := make([]byte, tail)
s := buf
for len(str) > 0 {
_, size = utf8.DecodeRuneInString(str)
tail -= size
s = append(s[:tail], []byte(str[:size])...)
str = str[size:]
}
return string(buf)
}
// Slice a string by rune.
//
// Start must satisfy 0 <= start <= rune length.
//
// End can be positive, zero or negative.
// If end >= 0, start and end must satisfy start <= end <= rune length.
// If end < 0, it means slice to the end of string.
//
// Otherwise, Slice will panic as out of range.
func Slice(str string, start, end int) string {
var size, startPos, endPos int
origin := str
if start < 0 || end > len(str) || (end >= 0 && start > end) {
panic("out of range")
}
if end >= 0 {
end -= start
}
for start > 0 && len(str) > 0 {
_, size = utf8.DecodeRuneInString(str)
start--
startPos += size
str = str[size:]
}
if end < 0 {
return origin[startPos:]
}
endPos = startPos
for end > 0 && len(str) > 0 {
_, size = utf8.DecodeRuneInString(str)
end--
endPos += size
str = str[size:]
}
if len(str) == 0 && (start > 0 || end > 0) {
panic("out of range")
}
return origin[startPos:endPos]
}
// Partition splits a string by sep into three parts.
// The return value is a slice of strings with head, match and tail.
//
// If str contains sep, for example "hello" and "l", Partition returns
// "he", "l", "lo"
//
// If str doesn't contain sep, for example "hello" and "x", Partition returns
// "hello", "", ""
func Partition(str, sep string) (head, match, tail string) {
index := strings.Index(str, sep)
if index == -1 {
head = str
return
}
head = str[:index]
match = str[index : index+len(sep)]
tail = str[index+len(sep):]
return
}
// LastPartition splits a string by last instance of sep into three parts.
// The return value is a slice of strings with head, match and tail.
//
// If str contains sep, for example "hello" and "l", LastPartition returns
// "hel", "l", "o"
//
// If str doesn't contain sep, for example "hello" and "x", LastPartition returns
// "", "", "hello"
func LastPartition(str, sep string) (head, match, tail string) {
index := strings.LastIndex(str, sep)
if index == -1 {
tail = str
return
}
head = str[:index]
match = str[index : index+len(sep)]
tail = str[index+len(sep):]
return
}
// Insert src into dst at given rune index.
// Index is counted by runes instead of bytes.
//
// If index is out of range of dst, panic with out of range.
func Insert(dst, src string, index int) string {
return Slice(dst, 0, index) + src + Slice(dst, index, -1)
}
// Scrubs invalid utf8 bytes with repl string.
// Adjacent invalid bytes are replaced only once.
func Scrub(str, repl string) string {
var buf *bytes.Buffer
var r rune
var size, pos int
var hasError bool
origin := str
for len(str) > 0 {
r, size = utf8.DecodeRuneInString(str)
if r == utf8.RuneError {
if !hasError {
if buf == nil {
buf = &bytes.Buffer{}
}
buf.WriteString(origin[:pos])
hasError = true
}
} else if hasError {
hasError = false
buf.WriteString(repl)
origin = origin[pos:]
pos = 0
}
pos += size
str = str[size:]
}
if buf != nil {
buf.WriteString(origin)
return buf.String()
}
// No invalid byte.
return origin
}
// Splits a string into words. Returns a slice of words.
// If there is no word in a string, return nil.
//
// Word is defined as a locale dependent string containing alphabetic characters,
// which may also contain but not start with `'` and `-` characters.
func WordSplit(str string) []string {
var word string
var words []string
var r rune
var size, pos int
inWord := false
for len(str) > 0 {
r, size = utf8.DecodeRuneInString(str)
switch {
case isAlphabet(r):
if !inWord {
inWord = true
word = str
pos = 0
}
case inWord && (r == '\'' || r == '-'):
// Still in word.
default:
if inWord {
inWord = false
words = append(words, word[:pos])
}
}
pos += size
str = str[size:]
}
if inWord {
words = append(words, word[:pos])
}
return words
}

545
vendor/github.com/huandu/xstrings/translate.go generated vendored Normal file
View File

@@ -0,0 +1,545 @@
// Copyright 2015 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.
package xstrings
import (
"bytes"
"unicode"
"unicode/utf8"
)
type runeRangeMap struct {
FromLo rune // Lower bound of range map.
FromHi rune // An inclusive higher bound of range map.
ToLo rune
ToHi rune
}
type runeDict struct {
Dict [unicode.MaxASCII + 1]rune
}
type runeMap map[rune]rune
// Translator can translate string with pre-compiled from and to patterns.
// If a from/to pattern pair needs to be used more than once, it's recommended
// to create a Translator and reuse it.
type Translator struct {
quickDict *runeDict // A quick dictionary to look up rune by index. Only availabe for latin runes.
runeMap runeMap // Rune map for translation.
ranges []*runeRangeMap // Ranges of runes.
mappedRune rune // If mappedRune >= 0, all matched runes are translated to the mappedRune.
reverted bool // If to pattern is empty, all matched characters will be deleted.
hasPattern bool
}
// NewTranslator creates new Translator through a from/to pattern pair.
func NewTranslator(from, to string) *Translator {
tr := &Translator{}
if from == "" {
return tr
}
reverted := from[0] == '^'
deletion := len(to) == 0
if reverted {
from = from[1:]
}
var fromStart, fromEnd, fromRangeStep rune
var toStart, toEnd, toRangeStep rune
var fromRangeSize, toRangeSize rune
var singleRunes []rune
// Update the to rune range.
updateRange := func() {
// No more rune to read in the to rune pattern.
if toEnd == utf8.RuneError {
return
}
if toRangeStep == 0 {
to, toStart, toEnd, toRangeStep = nextRuneRange(to, toEnd)
return
}
// Current range is not empty. Consume 1 rune from start.
if toStart != toEnd {
toStart += toRangeStep
return
}
// No more rune. Repeat the last rune.
if to == "" {
toEnd = utf8.RuneError
return
}
// Both start and end are used. Read two more runes from the to pattern.
to, toStart, toEnd, toRangeStep = nextRuneRange(to, utf8.RuneError)
}
if deletion {
toStart = utf8.RuneError
toEnd = utf8.RuneError
} else {
// If from pattern is reverted, only the last rune in the to pattern will be used.
if reverted {
var size int
for len(to) > 0 {
toStart, size = utf8.DecodeRuneInString(to)
to = to[size:]
}
toEnd = utf8.RuneError
} else {
to, toStart, toEnd, toRangeStep = nextRuneRange(to, utf8.RuneError)
}
}
fromEnd = utf8.RuneError
for len(from) > 0 {
from, fromStart, fromEnd, fromRangeStep = nextRuneRange(from, fromEnd)
// fromStart is a single character. Just map it with a rune in the to pattern.
if fromRangeStep == 0 {
singleRunes = tr.addRune(fromStart, toStart, singleRunes)
updateRange()
continue
}
for toEnd != utf8.RuneError && fromStart != fromEnd {
// If mapped rune is a single character instead of a range, simply shift first
// rune in the range.
if toRangeStep == 0 {
singleRunes = tr.addRune(fromStart, toStart, singleRunes)
updateRange()
fromStart += fromRangeStep
continue
}
fromRangeSize = (fromEnd - fromStart) * fromRangeStep
toRangeSize = (toEnd - toStart) * toRangeStep
// Not enough runes in the to pattern. Need to read more.
if fromRangeSize > toRangeSize {
fromStart, toStart = tr.addRuneRange(fromStart, fromStart+toRangeSize*fromRangeStep, toStart, toEnd, singleRunes)
fromStart += fromRangeStep
updateRange()
// Edge case: If fromRangeSize == toRangeSize + 1, the last fromStart value needs be considered
// as a single rune.
if fromStart == fromEnd {
singleRunes = tr.addRune(fromStart, toStart, singleRunes)
updateRange()
}
continue
}
fromStart, toStart = tr.addRuneRange(fromStart, fromEnd, toStart, toStart+fromRangeSize*toRangeStep, singleRunes)
updateRange()
break
}
if fromStart == fromEnd {
fromEnd = utf8.RuneError
continue
}
fromStart, toStart = tr.addRuneRange(fromStart, fromEnd, toStart, toStart, singleRunes)
fromEnd = utf8.RuneError
}
if fromEnd != utf8.RuneError {
singleRunes = tr.addRune(fromEnd, toStart, singleRunes)
}
tr.reverted = reverted
tr.mappedRune = -1
tr.hasPattern = true
// Translate RuneError only if in deletion or reverted mode.
if deletion || reverted {
tr.mappedRune = toStart
}
return tr
}
func (tr *Translator) addRune(from, to rune, singleRunes []rune) []rune {
if from <= unicode.MaxASCII {
if tr.quickDict == nil {
tr.quickDict = &runeDict{}
}
tr.quickDict.Dict[from] = to
} else {
if tr.runeMap == nil {
tr.runeMap = make(runeMap)
}
tr.runeMap[from] = to
}
singleRunes = append(singleRunes, from)
return singleRunes
}
func (tr *Translator) addRuneRange(fromLo, fromHi, toLo, toHi rune, singleRunes []rune) (rune, rune) {
var r rune
var rrm *runeRangeMap
if fromLo < fromHi {
rrm = &runeRangeMap{
FromLo: fromLo,
FromHi: fromHi,
ToLo: toLo,
ToHi: toHi,
}
} else {
rrm = &runeRangeMap{
FromLo: fromHi,
FromHi: fromLo,
ToLo: toHi,
ToHi: toLo,
}
}
// If there is any single rune conflicts with this rune range, clear single rune record.
for _, r = range singleRunes {
if rrm.FromLo <= r && r <= rrm.FromHi {
if r <= unicode.MaxASCII {
tr.quickDict.Dict[r] = 0
} else {
delete(tr.runeMap, r)
}
}
}
tr.ranges = append(tr.ranges, rrm)
return fromHi, toHi
}
func nextRuneRange(str string, last rune) (remaining string, start, end rune, rangeStep rune) {
var r rune
var size int
remaining = str
escaping := false
isRange := false
for len(remaining) > 0 {
r, size = utf8.DecodeRuneInString(remaining)
remaining = remaining[size:]
// Parse special characters.
if !escaping {
if r == '\\' {
escaping = true
continue
}
if r == '-' {
// Ignore slash at beginning of string.
if last == utf8.RuneError {
continue
}
start = last
isRange = true
continue
}
}
escaping = false
if last != utf8.RuneError {
// This is a range which start and end are the same.
// Considier it as a normal character.
if isRange && last == r {
isRange = false
continue
}
start = last
end = r
if isRange {
if start < end {
rangeStep = 1
} else {
rangeStep = -1
}
}
return
}
last = r
}
start = last
end = utf8.RuneError
return
}
// Translate str with a from/to pattern pair.
//
// See comment in Translate function for usage and samples.
func (tr *Translator) Translate(str string) string {
if !tr.hasPattern || str == "" {
return str
}
var r rune
var size int
var needTr bool
orig := str
var output *bytes.Buffer
for len(str) > 0 {
r, size = utf8.DecodeRuneInString(str)
r, needTr = tr.TranslateRune(r)
if needTr && output == nil {
output = allocBuffer(orig, str)
}
if r != utf8.RuneError && output != nil {
output.WriteRune(r)
}
str = str[size:]
}
// No character is translated.
if output == nil {
return orig
}
return output.String()
}
// TranslateRune return translated rune and true if r matches the from pattern.
// If r doesn't match the pattern, original r is returned and translated is false.
func (tr *Translator) TranslateRune(r rune) (result rune, translated bool) {
switch {
case tr.quickDict != nil:
if r <= unicode.MaxASCII {
result = tr.quickDict.Dict[r]
if result != 0 {
translated = true
if tr.mappedRune >= 0 {
result = tr.mappedRune
}
break
}
}
fallthrough
case tr.runeMap != nil:
var ok bool
if result, ok = tr.runeMap[r]; ok {
translated = true
if tr.mappedRune >= 0 {
result = tr.mappedRune
}
break
}
fallthrough
default:
var rrm *runeRangeMap
ranges := tr.ranges
for i := len(ranges) - 1; i >= 0; i-- {
rrm = ranges[i]
if rrm.FromLo <= r && r <= rrm.FromHi {
translated = true
if tr.mappedRune >= 0 {
result = tr.mappedRune
break
}
if rrm.ToLo < rrm.ToHi {
result = rrm.ToLo + r - rrm.FromLo
} else if rrm.ToLo > rrm.ToHi {
// ToHi can be smaller than ToLo if range is from higher to lower.
result = rrm.ToLo - r + rrm.FromLo
} else {
result = rrm.ToLo
}
break
}
}
}
if tr.reverted {
if !translated {
result = tr.mappedRune
}
translated = !translated
}
if !translated {
result = r
}
return
}
// HasPattern returns true if Translator has one pattern at least.
func (tr *Translator) HasPattern() bool {
return tr.hasPattern
}
// Translate str with the characters defined in from replaced by characters defined in to.
//
// From and to are patterns representing a set of characters. Pattern is defined as following.
//
// * Special characters
// * '-' means a range of runes, e.g.
// * "a-z" means all characters from 'a' to 'z' inclusive;
// * "z-a" means all characters from 'z' to 'a' inclusive.
// * '^' as first character means a set of all runes excepted listed, e.g.
// * "^a-z" means all characters except 'a' to 'z' inclusive.
// * '\' escapes special characters.
// * Normal character represents itself, e.g. "abc" is a set including 'a', 'b' and 'c'.
//
// Translate will try to find a 1:1 mapping from from to to.
// If to is smaller than from, last rune in to will be used to map "out of range" characters in from.
//
// Note that '^' only works in the from pattern. It will be considered as a normal character in the to pattern.
//
// If the to pattern is an empty string, Translate works exactly the same as Delete.
//
// Samples:
// Translate("hello", "aeiou", "12345") => "h2ll4"
// Translate("hello", "a-z", "A-Z") => "HELLO"
// Translate("hello", "z-a", "a-z") => "svool"
// Translate("hello", "aeiou", "*") => "h*ll*"
// Translate("hello", "^l", "*") => "**ll*"
// Translate("hello ^ world", `\^lo`, "*") => "he*** * w*r*d"
func Translate(str, from, to string) string {
tr := NewTranslator(from, to)
return tr.Translate(str)
}
// Delete runes in str matching the pattern.
// Pattern is defined in Translate function.
//
// Samples:
// Delete("hello", "aeiou") => "hll"
// Delete("hello", "a-k") => "llo"
// Delete("hello", "^a-k") => "he"
func Delete(str, pattern string) string {
tr := NewTranslator(pattern, "")
return tr.Translate(str)
}
// Count how many runes in str match the pattern.
// Pattern is defined in Translate function.
//
// Samples:
// Count("hello", "aeiou") => 3
// Count("hello", "a-k") => 3
// Count("hello", "^a-k") => 2
func Count(str, pattern string) int {
if pattern == "" || str == "" {
return 0
}
var r rune
var size int
var matched bool
tr := NewTranslator(pattern, "")
cnt := 0
for len(str) > 0 {
r, size = utf8.DecodeRuneInString(str)
str = str[size:]
if _, matched = tr.TranslateRune(r); matched {
cnt++
}
}
return cnt
}
// Squeeze deletes adjacent repeated runes in str.
// If pattern is not empty, only runes matching the pattern will be squeezed.
//
// Samples:
// Squeeze("hello", "") => "helo"
// Squeeze("hello", "m-z") => "hello"
func Squeeze(str, pattern string) string {
var last, r rune
var size int
var skipSqueeze, matched bool
var tr *Translator
var output *bytes.Buffer
orig := str
last = -1
if len(pattern) > 0 {
tr = NewTranslator(pattern, "")
}
for len(str) > 0 {
r, size = utf8.DecodeRuneInString(str)
// Need to squeeze the str.
if last == r && !skipSqueeze {
if tr != nil {
if _, matched = tr.TranslateRune(r); !matched {
skipSqueeze = true
}
}
if output == nil {
output = allocBuffer(orig, str)
}
if skipSqueeze {
output.WriteRune(r)
}
} else {
if output != nil {
output.WriteRune(r)
}
last = r
}
str = str[size:]
}
if output == nil {
return orig
}
return output.String()
}

View File

@@ -65,15 +65,27 @@ If you were using Mergo **before** April 6th 2015, please check your project wor
You can only merge same-type structs with exported fields initialized as zero value of their type and same-types maps. Mergo won't merge unexported (private) fields but will do recursively any exported one. Also maps will be merged recursively except for structs inside maps (because they are not addressable using Go reflection).
if err := mergo.Merge(&dst, src); err != nil {
// ...
}
```go
if err := mergo.Merge(&dst, src); err != nil {
// ...
}
```
Also, you can merge overwriting values using MergeWithOverwrite.
```go
if err := mergo.MergeWithOverwrite(&dst, src); err != nil {
// ...
}
```
Additionally, you can map a map[string]interface{} to a struct (and otherwise, from struct to map), following the same restrictions as in Merge(). Keys are capitalized to find each corresponding exported field.
if err := mergo.Map(&dst, srcMap); err != nil {
// ...
}
```go
if err := mergo.Map(&dst, srcMap); err != nil {
// ...
}
```
Warning: if you map a struct to map, it won't do it recursively. Don't expect Mergo to map struct members of your struct as map[string]interface{}. They will be just assigned as values.
@@ -97,11 +109,11 @@ type Foo struct {
func main() {
src := Foo{
A: "one",
B: 2,
}
dest := Foo{
A: "two",
B: 2,
}
mergo.Merge(&dest, src)

View File

@@ -68,7 +68,7 @@ func (r *Runner) arithm(expr syntax.ArithmExpr) int {
}
return binArit(x.Op, r.arithm(x.X), r.arithm(x.Y))
default:
r.errf("unexpected arithm expr: %T", x)
r.runErr(expr.Pos(), "unexpected arithm expr: %T", x)
return 0
}
}
@@ -105,7 +105,7 @@ func (r *Runner) assgnArit(b *syntax.BinaryArithm) int {
val ^= arg
case syntax.ShlAssgn:
val <<= uint(arg)
default: // syntax.ShrAssgn
case syntax.ShrAssgn:
val >>= uint(arg)
}
r.setVar(name, strconv.Itoa(val))

View File

@@ -5,34 +5,52 @@ package interp
import (
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"github.com/mvdan/sh/syntax"
)
func (r *Runner) builtin(pos syntax.Pos, name string, args []string) bool {
exit := 0
func isBuiltin(name string) bool {
switch name {
case "true", ":", "false", "exit", "set", "shift", "unset",
"echo", "printf", "break", "continue", "pwd", "cd",
"wait", "builtin", "trap", "type", "source", ".", "command",
"pushd", "popd", "umask", "alias", "unalias", "fg", "bg",
"getopts", "eval", "test", "[":
return true
}
return false
}
func (r *Runner) builtinCode(pos syntax.Pos, name string, args []string) int {
switch name {
case "true", ":":
case "false":
exit = 1
return 1
case "exit":
switch len(args) {
case 0:
r.lastExit()
case 1:
if n, err := strconv.Atoi(args[0]); err != nil {
r.runErr(pos, "invalid exit code: %q", args[0])
} else {
exit = n
r.err = ExitCode(n)
r.exit = n
}
default:
r.runErr(pos, "exit cannot take multiple arguments")
}
r.lastExit()
return r.exit
case "set":
r.args = args
rest, err := r.FromArgs(args...)
if err != nil {
r.errf("set: %v", err)
return 2
}
r.Params = rest
case "shift":
n := 1
switch len(args) {
@@ -45,19 +63,20 @@ func (r *Runner) builtin(pos syntax.Pos, name string, args []string) bool {
fallthrough
default:
r.errf("usage: shift [n]\n")
exit = 2
return 2
}
if len(r.args) < n {
n = len(r.args)
if n >= len(r.Params) {
r.Params = nil
} else {
r.Params = r.Params[n:]
}
r.args = r.args[n:]
case "unset":
for _, arg := range args {
r.delVar(arg)
}
case "echo":
newline := true
opts:
echoOpts:
for len(args) > 0 {
switch args[0] {
case "-n":
@@ -67,7 +86,7 @@ func (r *Runner) builtin(pos syntax.Pos, name string, args []string) bool {
// exactly what is the difference in
// what we write?
default:
break opts
break echoOpts
}
args = args[1:]
}
@@ -83,8 +102,7 @@ func (r *Runner) builtin(pos syntax.Pos, name string, args []string) bool {
case "printf":
if len(args) == 0 {
r.errf("usage: printf format [arguments]\n")
exit = 2
break
return 2
}
var a []interface{}
for _, arg := range args[1:] {
@@ -107,7 +125,7 @@ func (r *Runner) builtin(pos syntax.Pos, name string, args []string) bool {
fallthrough
default:
r.errf("usage: break [n]\n")
exit = 2
return 2
}
case "continue":
if !r.inLoop {
@@ -125,34 +143,29 @@ func (r *Runner) builtin(pos syntax.Pos, name string, args []string) bool {
fallthrough
default:
r.errf("usage: continue [n]\n")
exit = 2
return 2
}
case "pwd":
r.outf("%s\n", r.getVar("PWD"))
case "cd":
if len(args) > 1 {
r.errf("usage: cd [dir]\n")
exit = 2
break
}
var dir string
if len(args) == 0 {
switch len(args) {
case 0:
dir = r.getVar("HOME")
} else {
case 1:
dir = args[0]
default:
r.errf("usage: cd [dir]\n")
return 2
}
if !filepath.IsAbs(dir) {
dir = filepath.Join(r.Dir, dir)
}
_, err := os.Stat(dir)
if err != nil {
exit = 1
break
dir = r.relPath(dir)
if _, err := os.Stat(dir); err != nil {
return 1
}
r.Dir = dir
case "wait":
if len(args) > 0 {
r.errf("wait with args not handled yet")
r.runErr(pos, "wait with args not handled yet")
break
}
r.bgShells.Wait()
@@ -160,20 +173,91 @@ func (r *Runner) builtin(pos syntax.Pos, name string, args []string) bool {
if len(args) < 1 {
break
}
// TODO: pos
if !r.builtin(0, args[0], args[1:]) {
exit = 1
if !isBuiltin(args[0]) {
return 1
}
case "trap", "type", "source", "command", "pushd", "popd",
return r.builtinCode(pos, args[0], args[1:])
case "type":
anyNotFound := false
for _, arg := range args {
if _, ok := r.funcs[arg]; ok {
r.outf("%s is a function\n", arg)
continue
}
if isBuiltin(arg) {
r.outf("%s is a shell builtin\n", arg)
continue
}
if path, err := exec.LookPath(arg); err == nil {
r.outf("%s is %s\n", arg, path)
continue
}
r.errf("type: %s: not found\n", arg)
anyNotFound = true
}
if anyNotFound {
return 1
}
case "eval":
src := strings.Join(args, " ")
p := syntax.NewParser()
file, err := p.Parse(strings.NewReader(src), "")
if err != nil {
r.errf("eval: %v\n", err)
return 1
}
r2 := *r
r2.File = file
r2.Run()
return r2.exit
case "source", ".":
if len(args) < 1 {
r.runErr(pos, "source: need filename")
}
f, err := os.Open(r.relPath(args[0]))
if err != nil {
r.errf("eval: %v\n", err)
return 1
}
defer f.Close()
p := syntax.NewParser()
file, err := p.Parse(f, args[0])
if err != nil {
r.errf("eval: %v\n", err)
return 1
}
r2 := *r
r2.Params = args[1:]
r2.File = file
r2.Run()
return r2.exit
case "[":
if len(args) == 0 || args[len(args)-1] != "]" {
r.runErr(pos, "[: missing matching ]")
break
}
args = args[:len(args)-1]
fallthrough
case "test":
p := testParser{
rem: args,
err: func(format string, a ...interface{}) {
r.runErr(pos, format, a...)
},
}
p.next()
expr := p.classicTest("[", false)
return oneIf(r.bashTest(expr) == "")
case "trap", "command", "pushd", "popd",
"umask", "alias", "unalias", "fg", "bg", "getopts":
r.errf("unhandled builtin: %s", name)
// TODO(mvdan): we rely on the binary versions of these, we
// should eventually implement them as builtins like Bash for
// portability
// case "[", "test":
default:
return false
r.runErr(pos, "unhandled builtin: %s", name)
}
r.exit = exit
return true
return 0
}
func (r *Runner) relPath(path string) string {
if filepath.IsAbs(path) {
return path
}
return filepath.Join(r.Dir, path)
}

View File

@@ -12,6 +12,7 @@ import (
"os/exec"
"os/user"
"path"
"path/filepath"
"strconv"
"strings"
"sync"
@@ -43,6 +44,11 @@ type Runner struct {
// process's current directory.
Dir string
// Params are the current parameters, e.g. from running a shell
// file or calling a function. Accessible via the $@/$* family
// of vars.
Params []string
// Separate maps, note that bash allows a name to be both a var
// and a func simultaneously
vars map[string]varValue
@@ -51,9 +57,6 @@ type Runner struct {
// like vars, but local to a cmd i.e. "foo=bar prog args..."
cmdVars map[string]varValue
// Current arguments, if executing a function
args []string
// >0 to break or continue out of N enclosing loops
breakEnclosing, contnEnclosing int
@@ -70,6 +73,8 @@ type Runner struct {
// Context can be used to cancel the interpreter before it finishes
Context context.Context
stopOnCmdErr bool // set -e
}
// varValue can hold a string, an indexed array ([]string) or an
@@ -97,7 +102,6 @@ func (r *Runner) varInd(v varValue, e syntax.ArithmExpr) string {
return x
}
case []string:
// TODO: @ between double quotes
if w, ok := e.(*syntax.Word); ok {
if lit, ok := w.Parts[0].(*syntax.Lit); ok {
switch lit.Value {
@@ -119,19 +123,23 @@ type ExitCode uint8
func (e ExitCode) Error() string { return fmt.Sprintf("exit status %d", e) }
type RunError struct {
syntax.Position
Filename string
syntax.Pos
Text string
}
func (e RunError) Error() string {
return fmt.Sprintf("%s: %s", e.Position.String(), e.Text)
if e.Filename == "" {
return fmt.Sprintf("%s: %s", e.Pos.String(), e.Text)
}
return fmt.Sprintf("%s:%s: %s", e.Filename, e.Pos.String(), e.Text)
}
func (r *Runner) runErr(pos syntax.Pos, format string, a ...interface{}) {
if r.err == nil {
r.err = RunError{
Position: r.File.Position(pos),
Text: fmt.Sprintf(format, a...),
Pos: pos,
Text: fmt.Sprintf(format, a...),
}
}
}
@@ -184,6 +192,33 @@ func (r *Runner) setFunc(name string, body *syntax.Stmt) {
r.funcs[name] = body
}
// FromArgs populates the shell options and returns the remaining
// arguments. For example, running FromArgs("-e", "--", "foo") will set
// the "-e" option and return []string{"foo"}.
//
// This is similar to what the interpreter's "set" builtin does.
func (r *Runner) FromArgs(args ...string) ([]string, error) {
opts:
for len(args) > 0 {
opt := args[0]
if opt == "" || (opt[0] != '-' && opt[0] != '+') {
break
}
enable := opt[0] == '-'
switch opt[1:] {
case "-":
args = args[1:]
break opts
case "e":
r.stopOnCmdErr = enable
default:
return nil, fmt.Errorf("invalid option: %q", opt)
}
args = args[1:]
}
return args, nil
}
// Run starts the interpreter and returns any error.
func (r *Runner) Run() error {
if r.Context == nil {
@@ -208,7 +243,7 @@ func (r *Runner) Run() error {
}
r.Dir = dir
}
r.stmts(r.File.Stmts)
r.stmts(r.File.StmtList)
r.lastExit()
if r.err == ExitCode(0) {
r.err = nil
@@ -224,16 +259,72 @@ func (r *Runner) errf(format string, a ...interface{}) {
fmt.Fprintf(r.Stderr, format, a...)
}
func fieldJoin(parts []fieldPart) string {
var buf bytes.Buffer
for _, part := range parts {
buf.WriteString(part.val)
}
return buf.String()
}
func escapedGlob(parts []fieldPart) (escaped string, glob bool) {
var buf bytes.Buffer
for _, part := range parts {
for _, r := range part.val {
switch r {
case '*', '?', '\\', '[':
if part.quoted {
buf.WriteByte('\\')
} else {
glob = true
}
}
buf.WriteRune(r)
}
}
return buf.String(), glob
}
func (r *Runner) fields(words []*syntax.Word) []string {
fields := make([]string, 0, len(words))
baseDir, _ := escapedGlob([]fieldPart{{val: r.Dir}})
for _, word := range words {
fields = append(fields, r.wordParts(word.Parts, false)...)
for _, field := range r.wordFields(word.Parts, false) {
path, glob := escapedGlob(field)
var matches []string
abs := filepath.IsAbs(path)
if glob {
if !abs {
path = filepath.Join(baseDir, path)
}
matches, _ = filepath.Glob(path)
}
if len(matches) == 0 {
fields = append(fields, fieldJoin(field))
continue
}
for _, match := range matches {
if !abs {
match, _ = filepath.Rel(baseDir, match)
}
fields = append(fields, match)
}
}
}
return fields
}
func (r *Runner) loneWord(word *syntax.Word) string {
return strings.Join(r.wordParts(word.Parts, false), "")
if word == nil {
return ""
}
var buf bytes.Buffer
for _, field := range r.wordFields(word.Parts, false) {
for _, part := range field {
buf.WriteString(part.val)
}
}
return buf.String()
}
func (r *Runner) stop() bool {
@@ -264,34 +355,56 @@ func (r *Runner) stmt(st *syntax.Stmt) {
}
}
func (r *Runner) assignValue(word *syntax.Word) varValue {
if word == nil {
return nil
func (r *Runner) assignValue(as *syntax.Assign) varValue {
prev, _ := r.lookupVar(as.Name.Value)
if as.Value != nil {
s := r.loneWord(as.Value)
if !as.Append || prev == nil {
return s
}
switch x := prev.(type) {
case string:
return x + s
case []string:
if len(x) == 0 {
return []string{s}
}
x[0] += s
return x
}
return s
}
ae, ok := word.Parts[0].(*syntax.ArrayExpr)
if !ok {
return r.loneWord(word)
if as.Array != nil {
strs := make([]string, len(as.Array.Elems))
for i, elem := range as.Array.Elems {
strs[i] = r.loneWord(elem.Value)
}
if !as.Append || prev == nil {
return strs
}
switch x := prev.(type) {
case string:
return append([]string{x}, strs...)
case []string:
return append(x, strs...)
}
return strs
}
strs := make([]string, len(ae.List))
for i, w := range ae.List {
strs[i] = r.loneWord(w)
}
return strs
return nil
}
func (r *Runner) stmtSync(st *syntax.Stmt) {
oldVars := r.cmdVars
for _, as := range st.Assigns {
name := as.Name.Value
val := r.assignValue(as.Value)
val := r.assignValue(as)
if st.Cmd == nil {
r.setVar(name, val)
r.setVar(as.Name.Value, val)
continue
}
if r.cmdVars == nil {
r.cmdVars = make(map[string]varValue, len(st.Assigns))
}
r.cmdVars[name] = val
r.cmdVars[as.Name.Value] = val
}
oldIn, oldOut, oldErr := r.Stdin, r.Stdout, r.Stderr
for _, rd := range st.Redirs {
@@ -329,10 +442,10 @@ func (r *Runner) cmd(cm syntax.Command) {
}
switch x := cm.(type) {
case *syntax.Block:
r.stmts(x.Stmts)
r.stmts(x.StmtList)
case *syntax.Subshell:
r2 := *r
r2.stmts(x.Stmts)
r2.stmts(x.StmtList)
r.exit = r2.exit
case *syntax.CallExpr:
fields := r.fields(x.Args)
@@ -368,41 +481,19 @@ func (r *Runner) cmd(cm syntax.Command) {
pr.Close()
}
case *syntax.IfClause:
r.stmts(x.CondStmts)
r.stmts(x.Cond)
if r.exit == 0 {
r.stmts(x.ThenStmts)
r.stmts(x.Then)
return
}
for _, el := range x.Elifs {
r.stmts(el.CondStmts)
if r.exit == 0 {
r.stmts(el.ThenStmts)
return
}
}
r.stmts(x.ElseStmts)
if len(x.Elifs)+len(x.ElseStmts) == 0 {
r.exit = 0
}
r.exit = 0
r.stmts(x.Else)
case *syntax.WhileClause:
for r.err == nil {
r.stmts(x.CondStmts)
if r.exit != 0 {
r.exit = 0
break
}
if r.loopStmtsBroken(x.DoStmts) {
break
}
}
case *syntax.UntilClause:
for r.err == nil {
r.stmts(x.CondStmts)
if r.exit == 0 {
break
}
r.stmts(x.Cond)
stop := (r.exit == 0) == x.Until
r.exit = 0
if r.loopStmtsBroken(x.DoStmts) {
if stop || r.loopStmtsBroken(x.Do) {
break
}
}
@@ -410,16 +501,16 @@ func (r *Runner) cmd(cm syntax.Command) {
switch y := x.Loop.(type) {
case *syntax.WordIter:
name := y.Name.Value
for _, field := range r.fields(y.List) {
for _, field := range r.fields(y.Items) {
r.setVar(name, field)
if r.loopStmtsBroken(x.DoStmts) {
if r.loopStmtsBroken(x.Do) {
break
}
}
case *syntax.CStyleLoop:
r.arithm(y.Init)
for r.arithm(y.Cond) != 0 {
if r.loopStmtsBroken(x.DoStmts) {
if r.loopStmtsBroken(x.Do) {
break
}
r.arithm(y.Post)
@@ -441,13 +532,15 @@ func (r *Runner) cmd(cm syntax.Command) {
}
case *syntax.CaseClause:
str := r.loneWord(x.Word)
for _, pl := range x.List {
for _, word := range pl.Patterns {
pat := r.loneWord(word)
// TODO: error?
matched, _ := path.Match(pat, str)
if matched {
r.stmts(pl.Stmts)
for _, ci := range x.Items {
for _, word := range ci.Patterns {
var buf bytes.Buffer
for _, field := range r.wordFields(word.Parts, false) {
escaped, _ := escapedGlob(field)
buf.WriteString(escaped)
}
if match(buf.String(), str) {
r.stmts(ci.StmtList)
return
}
}
@@ -456,17 +549,32 @@ func (r *Runner) cmd(cm syntax.Command) {
if r.bashTest(x.X) == "" && r.exit == 0 {
r.exit = 1
}
case *syntax.DeclClause:
if len(x.Opts) > 0 {
r.runErr(cm.Pos(), "unhandled declare opts")
}
for _, as := range x.Assigns {
r.setVar(as.Name.Value, r.assignValue(as))
}
default:
r.errf("unhandled command node: %T", x)
r.runErr(cm.Pos(), "unhandled command node: %T", x)
}
if r.exit != 0 && r.stopOnCmdErr {
r.lastExit()
}
}
func (r *Runner) stmts(stmts []*syntax.Stmt) {
for _, stmt := range stmts {
func (r *Runner) stmts(sl syntax.StmtList) {
for _, stmt := range sl.Stmts {
r.stmt(stmt)
}
}
func match(pattern, name string) bool {
matched, _ := path.Match(pattern, name)
return matched
}
func (r *Runner) redir(rd *syntax.Redirect) (io.Closer, error) {
if rd.Hdoc != nil {
hdoc := r.loneWord(rd.Hdoc)
@@ -495,7 +603,7 @@ func (r *Runner) redir(rd *syntax.Redirect) (io.Closer, error) {
}
return nil, nil
case syntax.DplIn:
r.errf("unhandled redirect op: %v", rd.Op)
r.runErr(rd.Pos(), "unhandled redirect op: %v", rd.Op)
}
mode := os.O_RDONLY
switch rd.Op {
@@ -504,7 +612,7 @@ func (r *Runner) redir(rd *syntax.Redirect) (io.Closer, error) {
case syntax.RdrOut, syntax.RdrAll:
mode = os.O_RDWR | os.O_CREATE | os.O_TRUNC
}
f, err := os.OpenFile(arg, mode, 0644)
f, err := os.OpenFile(r.relPath(arg), mode, 0644)
if err != nil {
// TODO: print to stderr?
return nil, err
@@ -518,15 +626,15 @@ func (r *Runner) redir(rd *syntax.Redirect) (io.Closer, error) {
r.Stdout = f
r.Stderr = f
default:
r.errf("unhandled redirect op: %v", rd.Op)
r.runErr(rd.Pos(), "unhandled redirect op: %v", rd.Op)
}
return f, nil
}
func (r *Runner) loopStmtsBroken(stmts []*syntax.Stmt) bool {
func (r *Runner) loopStmtsBroken(sl syntax.StmtList) bool {
r.inLoop = true
defer func() { r.inLoop = false }()
for _, stmt := range stmts {
for _, stmt := range sl.Stmts {
r.stmt(stmt)
if r.contnEnclosing > 0 {
r.contnEnclosing--
@@ -540,15 +648,21 @@ func (r *Runner) loopStmtsBroken(stmts []*syntax.Stmt) bool {
return false
}
func (r *Runner) wordParts(wps []syntax.WordPart, quoted bool) []string {
var parts []string
var curBuf bytes.Buffer
type fieldPart struct {
val string
quoted bool
}
func (r *Runner) wordFields(wps []syntax.WordPart, quoted bool) [][]fieldPart {
var fields [][]fieldPart
var curField []fieldPart
allowEmpty := false
flush := func() {
if curBuf.Len() == 0 {
if len(curField) == 0 {
return
}
parts = append(parts, curBuf.String())
curBuf.Reset()
fields = append(fields, curField)
curField = nil
}
splitAdd := func(val string) {
// TODO: use IFS
@@ -556,70 +670,94 @@ func (r *Runner) wordParts(wps []syntax.WordPart, quoted bool) []string {
if i > 0 {
flush()
}
curBuf.WriteString(field)
curField = append(curField, fieldPart{val: field})
}
}
for _, wp := range wps {
for i, wp := range wps {
switch x := wp.(type) {
case *syntax.Lit:
curBuf.WriteString(x.Value)
s := x.Value
if i > 0 || len(s) == 0 || s[0] != '~' {
} else if len(s) < 2 || s[1] == '/' {
// TODO: ~someuser
s = r.getVar("HOME") + s[1:]
}
curField = append(curField, fieldPart{val: s})
case *syntax.SglQuoted:
curBuf.WriteString(x.Value)
allowEmpty = true
curField = append(curField, fieldPart{
quoted: true,
val: x.Value,
})
case *syntax.DblQuoted:
// TODO: @ between double quotes but not alone
allowEmpty = true
if len(x.Parts) == 1 {
pe, ok := x.Parts[0].(*syntax.ParamExp)
if ok && pe.Param.Value == "@" {
for i, arg := range r.args {
pe, _ := x.Parts[0].(*syntax.ParamExp)
if elems := r.quotedElems(pe); elems != nil {
for i, elem := range elems {
if i > 0 {
flush()
}
curBuf.WriteString(arg)
curField = append(curField, fieldPart{
quoted: true,
val: elem,
})
}
continue
}
}
for _, str := range r.wordParts(x.Parts, true) {
curBuf.WriteString(str)
for _, field := range r.wordFields(x.Parts, true) {
for _, part := range field {
curField = append(curField, fieldPart{
quoted: true,
val: part.val,
})
}
}
case *syntax.ParamExp:
val := r.paramExp(x)
if quoted {
curBuf.WriteString(val)
curField = append(curField, fieldPart{val: val})
} else {
splitAdd(val)
}
case *syntax.CmdSubst:
r2 := *r
var outBuf bytes.Buffer
r2.Stdout = &outBuf
r2.stmts(x.Stmts)
val := strings.TrimRight(outBuf.String(), "\n")
var buf bytes.Buffer
r2.Stdout = &buf
r2.stmts(x.StmtList)
val := strings.TrimRight(buf.String(), "\n")
if quoted {
curBuf.WriteString(val)
curField = append(curField, fieldPart{val: val})
} else {
splitAdd(val)
}
case *syntax.ArithmExp:
curBuf.WriteString(strconv.Itoa(r.arithm(x.X)))
curField = append(curField, fieldPart{
val: strconv.Itoa(r.arithm(x.X)),
})
default:
r.errf("unhandled word part: %T", x)
r.runErr(wp.Pos(), "unhandled word part: %T", x)
}
}
flush()
return parts
if allowEmpty && len(fields) == 0 {
fields = append(fields, []fieldPart{{}})
}
return fields
}
func (r *Runner) call(pos syntax.Pos, name string, args []string) {
if body := r.funcs[name]; body != nil {
// stack them to support nested func calls
oldArgs := r.args
r.args = args
oldParams := r.Params
r.Params = args
r.stmt(body)
r.args = oldArgs
r.Params = oldParams
return
}
if r.builtin(pos, name, args) {
if isBuiltin(name) {
r.exit = r.builtinCode(pos, name, args)
return
}
cmd := exec.CommandContext(r.Context, name, args...)
@@ -636,10 +774,9 @@ func (r *Runner) call(pos syntax.Pos, name string, args []string) {
case *exec.ExitError:
// started, but errored - default to 1 if OS
// doesn't have exit statuses
r.exit = 1
if status, ok := x.Sys().(syscall.WaitStatus); ok {
r.exit = status.ExitStatus()
} else {
r.exit = 1
}
case *exec.Error:
// did not start

View File

@@ -4,7 +4,6 @@
package interp
import (
"path"
"strconv"
"strings"
"unicode"
@@ -13,34 +12,57 @@ import (
"github.com/mvdan/sh/syntax"
)
func (r *Runner) quotedElems(pe *syntax.ParamExp) []string {
if pe == nil {
return nil
}
if pe.Param.Value == "@" {
return r.Params
}
w, _ := pe.Index.(*syntax.Word)
if w == nil || len(w.Parts) != 1 {
return nil
}
l, _ := w.Parts[0].(*syntax.Lit)
if l == nil || l.Value != "@" {
return nil
}
val, _ := r.lookupVar(pe.Param.Value)
switch x := val.(type) {
case []string:
return x
}
return nil
}
func (r *Runner) paramExp(pe *syntax.ParamExp) string {
name := pe.Param.Value
var val varValue
set := false
switch name {
case "#":
val = strconv.Itoa(len(r.args))
val = strconv.Itoa(len(r.Params))
case "*", "@":
val = strings.Join(r.args, " ")
val = strings.Join(r.Params, " ")
case "?":
val = strconv.Itoa(r.exit)
default:
if n, err := strconv.Atoi(name); err == nil {
if i := n - 1; i < len(r.args) {
val, set = r.args[i], true
if i := n - 1; i < len(r.Params) {
val, set = r.Params[i], true
}
} else {
val, set = r.lookupVar(name)
}
}
str := varStr(val)
if pe.Ind != nil {
str = r.varInd(val, pe.Ind.Expr)
if pe.Index != nil {
str = r.varInd(val, pe.Index)
}
switch {
case pe.Length:
str = strconv.Itoa(utf8.RuneCountInString(str))
case pe.Excl:
case pe.Indirect:
val, set = r.lookupVar(str)
str = varStr(val)
}
@@ -141,7 +163,7 @@ func (r *Runner) paramExp(pe *syntax.ParamExp) string {
str = string(rs)
case syntax.LowerAll:
str = strings.ToLower(str)
default: // syntax.OtherParamOps
case syntax.OtherParamOps:
switch arg {
case "Q":
str = strconv.Quote(str)
@@ -155,9 +177,9 @@ func (r *Runner) paramExp(pe *syntax.ParamExp) string {
}
str = string(rns)
case "P", "A", "a":
r.errf("unhandled @%s param expansion", arg)
r.runErr(pe.Pos(), "unhandled @%s param expansion", arg)
default:
r.errf("unexpected @%s param expansion", arg)
r.runErr(pe.Pos(), "unexpected @%s param expansion", arg)
}
}
}
@@ -173,7 +195,7 @@ func removePattern(str, pattern string, fromEnd, longest bool) string {
i = 0
}
for {
if m, _ := path.Match(pattern, s); m {
if match(pattern, s) {
last = str[i:]
if fromEnd {
last = str[:i]

View File

@@ -4,10 +4,9 @@
package interp
import (
"bytes"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"github.com/mvdan/sh/syntax"
@@ -21,6 +20,20 @@ func (r *Runner) bashTest(expr syntax.TestExpr) string {
case *syntax.ParenTest:
return r.bashTest(x.X)
case *syntax.BinaryTest:
switch x.Op {
case syntax.TsMatch, syntax.TsNoMatch:
str := r.loneWord(x.X.(*syntax.Word))
var buf bytes.Buffer
yw := x.Y.(*syntax.Word)
for _, field := range r.wordFields(yw.Parts, false) {
escaped, _ := escapedGlob(field)
buf.WriteString(escaped)
}
if match(buf.String(), str) == (x.Op == syntax.TsMatch) {
return "1"
}
return ""
}
if r.binTest(x.Op, r.bashTest(x.X), r.bashTest(x.Y)) {
return "1"
}
@@ -44,19 +57,19 @@ func (r *Runner) binTest(op syntax.BinTestOperator, x, y string) bool {
}
return re.MatchString(x)
case syntax.TsNewer:
i1, i2 := stat(x), stat(y)
i1, i2 := r.stat(x), r.stat(y)
if i1 == nil || i2 == nil {
return false
}
return i1.ModTime().After(i2.ModTime())
case syntax.TsOlder:
i1, i2 := stat(x), stat(y)
i1, i2 := r.stat(x), r.stat(y)
if i1 == nil || i2 == nil {
return false
}
return i1.ModTime().Before(i2.ModTime())
case syntax.TsDevIno:
i1, i2 := stat(x), stat(y)
i1, i2 := r.stat(x), r.stat(y)
return os.SameFile(i1, i2)
case syntax.TsEql:
return atoi(x) == atoi(y)
@@ -74,12 +87,6 @@ func (r *Runner) binTest(op syntax.BinTestOperator, x, y string) bool {
return x != "" && y != ""
case syntax.OrTest:
return x != "" || y != ""
case syntax.TsEqual:
m, _ := path.Match(y, x)
return m
case syntax.TsNequal:
m, _ := path.Match(y, x)
return !m
case syntax.TsBefore:
return x < y
default: // syntax.TsAfter
@@ -87,61 +94,60 @@ func (r *Runner) binTest(op syntax.BinTestOperator, x, y string) bool {
}
}
func stat(name string) os.FileInfo {
info, _ := os.Stat(name)
func (r *Runner) stat(name string) os.FileInfo {
info, _ := os.Stat(r.relPath(name))
return info
}
func statMode(name string, mode os.FileMode) bool {
info := stat(name)
func (r *Runner) statMode(name string, mode os.FileMode) bool {
info := r.stat(name)
return info != nil && info.Mode()&mode != 0
}
func (r *Runner) unTest(op syntax.UnTestOperator, x string) bool {
switch op {
case syntax.TsExists:
return stat(x) != nil
return r.stat(x) != nil
case syntax.TsRegFile:
info := stat(x)
info := r.stat(x)
return info != nil && info.Mode().IsRegular()
case syntax.TsDirect:
return statMode(x, os.ModeDir)
return r.statMode(x, os.ModeDir)
//case syntax.TsCharSp:
//case syntax.TsBlckSp:
case syntax.TsNmPipe:
return statMode(x, os.ModeNamedPipe)
return r.statMode(x, os.ModeNamedPipe)
case syntax.TsSocket:
return statMode(x, os.ModeSocket)
return r.statMode(x, os.ModeSocket)
case syntax.TsSmbLink:
info, _ := os.Lstat(x)
info, _ := os.Lstat(r.relPath(x))
return info != nil && info.Mode()&os.ModeSymlink != 0
case syntax.TsSticky:
return statMode(x, os.ModeSticky)
return r.statMode(x, os.ModeSticky)
case syntax.TsUIDSet:
return statMode(x, os.ModeSetuid)
return r.statMode(x, os.ModeSetuid)
case syntax.TsGIDSet:
return statMode(x, os.ModeSetgid)
return r.statMode(x, os.ModeSetgid)
//case syntax.TsGrpOwn:
//case syntax.TsUsrOwn:
//case syntax.TsModif:
case syntax.TsRead:
f, err := os.OpenFile(x, os.O_RDONLY, 0)
f, err := os.OpenFile(r.relPath(x), os.O_RDONLY, 0)
if err == nil {
f.Close()
}
return err == nil
case syntax.TsWrite:
f, err := os.OpenFile(x, os.O_WRONLY, 0)
f, err := os.OpenFile(r.relPath(x), os.O_WRONLY, 0)
if err == nil {
f.Close()
}
return err == nil
case syntax.TsExec:
// use an absolute path to not use $PATH
_, err := exec.LookPath(filepath.Join(r.Dir, x))
_, err := exec.LookPath(r.relPath(x))
return err == nil
case syntax.TsNoEmpty:
info := stat(x)
info := r.stat(x)
return info != nil && info.Size() > 0
//case syntax.TsFdTerm:
case syntax.TsEmpStr:
@@ -149,12 +155,14 @@ func (r *Runner) unTest(op syntax.UnTestOperator, x string) bool {
case syntax.TsNempStr:
return x != ""
//case syntax.TsOptSet:
//case syntax.TsVarSet:
case syntax.TsVarSet:
_, e := r.lookupVar(x)
return e
//case syntax.TsRefVar:
case syntax.TsNot:
return x == ""
default:
r.errf("unhandled unary test op: %v", op)
r.runErr(syntax.Pos{}, "unhandled unary test op: %v", op)
return false
}
}

182
vendor/github.com/mvdan/sh/interp/test_classic.go generated vendored Normal file
View File

@@ -0,0 +1,182 @@
// Copyright (c) 2017, Daniel Martí <mvdan@mvdan.cc>
// See LICENSE for licensing information
package interp
import (
"github.com/mvdan/sh/syntax"
)
const illegalTok = 0
type testParser struct {
eof bool
val string
rem []string
err func(format string, a ...interface{})
}
func (p *testParser) next() {
if p.eof || len(p.rem) == 0 {
p.eof = true
return
}
p.val = p.rem[0]
p.rem = p.rem[1:]
}
func (p *testParser) followWord(fval string) *syntax.Word {
if p.eof {
p.err("%s must be followed by a word", fval)
}
w := &syntax.Word{Parts: []syntax.WordPart{
&syntax.Lit{Value: p.val},
}}
p.next()
return w
}
func (p *testParser) classicTest(fval string, pastAndOr bool) syntax.TestExpr {
var left syntax.TestExpr
if pastAndOr {
left = p.testExprBase(fval)
} else {
left = p.classicTest(fval, true)
}
if left == nil || p.eof {
return left
}
opStr := p.val
op := testBinaryOp(p.val)
if op == illegalTok {
p.err("not a valid test operator: %s", p.val)
}
b := &syntax.BinaryTest{
Op: op,
X: left,
}
p.next()
switch b.Op {
case syntax.AndTest, syntax.OrTest:
if b.Y = p.classicTest(opStr, false); b.Y == nil {
p.err("%s must be followed by an expression", opStr)
}
default:
b.Y = p.followWord(opStr)
}
return b
}
func (p *testParser) testExprBase(fval string) syntax.TestExpr {
if p.eof {
return nil
}
op := testUnaryOp(p.val)
switch op {
case syntax.TsNot:
u := &syntax.UnaryTest{Op: op}
p.next()
u.X = p.classicTest(op.String(), false)
return u
case illegalTok:
return p.followWord(fval)
default:
u := &syntax.UnaryTest{Op: op}
p.next()
u.X = p.followWord(op.String())
return u
}
}
// testUnaryOp is an exact copy of syntax's.
func testUnaryOp(val string) syntax.UnTestOperator {
switch val {
case "!":
return syntax.TsNot
case "-e", "-a":
return syntax.TsExists
case "-f":
return syntax.TsRegFile
case "-d":
return syntax.TsDirect
case "-c":
return syntax.TsCharSp
case "-b":
return syntax.TsBlckSp
case "-p":
return syntax.TsNmPipe
case "-S":
return syntax.TsSocket
case "-L", "-h":
return syntax.TsSmbLink
case "-k":
return syntax.TsSticky
case "-g":
return syntax.TsGIDSet
case "-u":
return syntax.TsUIDSet
case "-G":
return syntax.TsGrpOwn
case "-O":
return syntax.TsUsrOwn
case "-N":
return syntax.TsModif
case "-r":
return syntax.TsRead
case "-w":
return syntax.TsWrite
case "-x":
return syntax.TsExec
case "-s":
return syntax.TsNoEmpty
case "-t":
return syntax.TsFdTerm
case "-z":
return syntax.TsEmpStr
case "-n":
return syntax.TsNempStr
case "-o":
return syntax.TsOptSet
case "-v":
return syntax.TsVarSet
case "-R":
return syntax.TsRefVar
default:
return illegalTok
}
}
// testBinaryOp is like syntax's, but with -a and -o, and without =~.
func testBinaryOp(val string) syntax.BinTestOperator {
switch val {
case "-a":
return syntax.AndTest
case "-o":
return syntax.OrTest
case "==", "=":
return syntax.TsMatch
case "!=":
return syntax.TsNoMatch
case "-nt":
return syntax.TsNewer
case "-ot":
return syntax.TsOlder
case "-ef":
return syntax.TsDevIno
case "-eq":
return syntax.TsEql
case "-ne":
return syntax.TsNeq
case "-le":
return syntax.TsLeq
case "-ge":
return syntax.TsGeq
case "-lt":
return syntax.TsLss
case "-gt":
return syntax.TsGtr
default:
return illegalTok
}
}

View File

@@ -18,16 +18,16 @@ for foo in a b c; do
done
case $foo in
a) A ;;
b)
B
;;
a) A ;;
b)
B
;;
esac
foo | bar
foo \
&& $(bar) \
&& (more)
foo &&
$(bar) &&
(more)
foo 2>&1
foo <<EOF

View File

@@ -28,11 +28,20 @@ func paramOps(r rune) bool {
return false
}
// these start a parameter expansion name
func paramNameOp(r rune) bool {
switch r {
case '}', ':', '+', '=', '%', '[', ']', '/', '^', ',', '@':
return false
}
return true
}
// tokenize these inside arithmetic expansions
func arithmOps(r rune) bool {
switch r {
case '+', '-', '!', '*', '/', '%', '(', ')', '^', '<', '>', ':', '=',
',', '?', '|', '&', ']':
',', '?', '|', '&', '[', ']', '#':
return true
}
return false
@@ -46,13 +55,18 @@ func wordBreak(r rune) bool {
return false
}
func (p *parser) rune() rune {
func (p *Parser) rune() rune {
retry:
if p.npos < len(p.bs) {
if b := p.bs[p.npos]; b < utf8.RuneSelf {
if p.npos++; b == '\n' {
p.f.lines = append(p.f.lines, p.getPos())
if p.bsp < len(p.bs) {
if b := p.bs[p.bsp]; b < utf8.RuneSelf {
p.bsp++
if p.r == '\n' {
// p.r instead of b so that newline
// character positions don't have col 0.
p.npos.line++
p.npos.col = 0
}
p.npos.col++
if p.litBs != nil {
p.litBs = append(p.litBs, b)
}
@@ -60,24 +74,25 @@ retry:
p.r = r
return r
}
if p.npos+utf8.UTFMax >= len(p.bs) {
if p.bsp+utf8.UTFMax >= len(p.bs) {
// we might need up to 4 bytes to read a full
// non-ascii rune
p.fill()
}
var w int
p.r, w = utf8.DecodeRune(p.bs[p.npos:])
p.r, w = utf8.DecodeRune(p.bs[p.bsp:])
if p.litBs != nil {
p.litBs = append(p.litBs, p.bs[p.npos:p.npos+w]...)
p.litBs = append(p.litBs, p.bs[p.bsp:p.bsp+w]...)
}
p.npos += w
p.bsp += w
p.npos.col += uint16(w)
if p.r == utf8.RuneError && w == 1 {
p.posErr(p.getPos(), "invalid UTF-8 encoding")
p.posErr(p.npos, "invalid UTF-8 encoding")
}
} else {
if p.r == utf8.RuneSelf {
} else if p.fill(); p.bs == nil {
p.npos++
p.bsp++
p.r = utf8.RuneSelf
} else {
goto retry
@@ -86,20 +101,13 @@ retry:
return p.r
}
func (p *parser) unrune(r rune) {
if p.r != utf8.RuneSelf {
p.npos -= utf8.RuneLen(p.r)
p.r = r
}
}
// fill reads more bytes from the input src into readBuf. Any bytes that
// had not yet been used at the end of the buffer are slid into the
// beginning of the buffer.
func (p *parser) fill() {
left := len(p.bs) - p.npos
p.offs += p.npos
copy(p.readBuf[:left], p.readBuf[p.npos:])
func (p *Parser) fill() {
p.offs += p.bsp
left := len(p.bs) - p.bsp
copy(p.readBuf[:left], p.readBuf[p.bsp:])
var n int
var err error
if p.readErr == nil {
@@ -121,13 +129,13 @@ func (p *parser) fill() {
} else {
p.bs = p.readBuf[:left+n]
}
p.npos = 0
p.bsp = 0
}
func (p *parser) nextKeepSpaces() {
func (p *Parser) nextKeepSpaces() {
r := p.r
if p.pos = p.getPos(); r > utf8.RuneSelf {
p.pos -= Pos(utf8.RuneLen(r) - 1)
p.pos = posAddCol(p.pos, -1) // TODO
}
switch p.quote {
case paramExpRepl:
@@ -174,7 +182,7 @@ func (p *parser) nextKeepSpaces() {
}
}
func (p *parser) next() {
func (p *Parser) next() {
if p.r == utf8.RuneSelf {
p.tok = _EOF
return
@@ -218,7 +226,7 @@ skipSpace:
}
}
if p.pos = p.getPos(); r > utf8.RuneSelf {
p.pos -= Pos(utf8.RuneLen(r) - 1)
p.pos = posAddCol(p.pos, -1) // TODO
}
switch {
case p.quote&allRegTokens != 0:
@@ -231,8 +239,8 @@ skipSpace:
for r != utf8.RuneSelf && r != '\n' {
r = p.rune()
}
if p.mode&ParseComments > 0 {
p.f.Comments = append(p.f.Comments, &Comment{
if p.keepComments {
*p.curComs = append(*p.curComs, Comment{
Hash: p.pos,
Text: p.endLit(),
})
@@ -240,6 +248,13 @@ skipSpace:
p.litBs = nil
}
p.next()
case '[':
if p.quote == arrayElems {
p.tok = leftBrack
p.rune()
} else {
p.advanceLitNone(r)
}
case '?', '*', '+', '@', '!':
if p.peekByte('(') {
switch r {
@@ -282,14 +297,14 @@ skipSpace:
}
}
func (p *parser) peekByte(b byte) bool {
if p.npos == len(p.bs) && p.readErr == nil {
func (p *Parser) peekByte(b byte) bool {
if p.bsp == len(p.bs) && p.readErr == nil {
p.fill()
}
return p.npos < len(p.bs) && p.bs[p.npos] == b
return p.bsp < len(p.bs) && p.bs[p.bsp] == b
}
func (p *parser) regToken(r rune) token {
func (p *Parser) regToken(r rune) token {
switch r {
case '\'':
p.rune()
@@ -306,7 +321,7 @@ func (p *parser) regToken(r rune) token {
p.rune()
return andAnd
case '>':
if !p.bash() {
if p.lang == LangPOSIX {
break
}
if p.rune() == '>' {
@@ -322,23 +337,23 @@ func (p *parser) regToken(r rune) token {
p.rune()
return orOr
case '&':
if !p.bash() {
if p.lang == LangPOSIX {
break
}
p.rune()
return pipeAll
return orAnd
}
return or
case '$':
switch p.rune() {
case '\'':
if !p.bash() {
if p.lang == LangPOSIX {
break
}
p.rune()
return dollSglQuote
case '"':
if !p.bash() {
if p.lang == LangPOSIX {
break
}
p.rune()
@@ -347,7 +362,7 @@ func (p *parser) regToken(r rune) token {
p.rune()
return dollBrace
case '[':
if !p.bash() {
if p.lang != LangBash {
break
}
p.rune()
@@ -361,7 +376,7 @@ func (p *parser) regToken(r rune) token {
}
return dollar
case '(':
if p.rune() == '(' && p.bash() {
if p.rune() == '(' && p.lang != LangPOSIX {
p.rune()
return dblLeftParen
}
@@ -372,17 +387,23 @@ func (p *parser) regToken(r rune) token {
case ';':
switch p.rune() {
case ';':
if p.rune() == '&' && p.bash() {
if p.rune() == '&' && p.lang == LangBash {
p.rune()
return dblSemiFall
return dblSemiAnd
}
return dblSemicolon
case '&':
if !p.bash() {
if p.lang == LangPOSIX {
break
}
p.rune()
return semiFall
return semiAnd
case '|':
if p.lang != LangMirBSDKorn {
break
}
p.rune()
return semiOr
}
return semicolon
case '<':
@@ -391,7 +412,7 @@ func (p *parser) regToken(r rune) token {
if r = p.rune(); r == '-' {
p.rune()
return dashHdoc
} else if r == '<' && p.bash() {
} else if r == '<' && p.lang != LangPOSIX {
p.rune()
return wordHdoc
}
@@ -403,7 +424,7 @@ func (p *parser) regToken(r rune) token {
p.rune()
return dplIn
case '(':
if !p.bash() {
if p.lang != LangBash {
break
}
p.rune()
@@ -422,7 +443,7 @@ func (p *parser) regToken(r rune) token {
p.rune()
return clbOut
case '(':
if !p.bash() {
if p.lang != LangBash {
break
}
p.rune()
@@ -432,7 +453,7 @@ func (p *parser) regToken(r rune) token {
}
}
func (p *parser) dqToken(r rune) token {
func (p *Parser) dqToken(r rune) token {
switch r {
case '"':
p.rune()
@@ -446,7 +467,7 @@ func (p *parser) dqToken(r rune) token {
p.rune()
return dollBrace
case '[':
if !p.bash() {
if p.lang != LangBash {
break
}
p.rune()
@@ -462,7 +483,7 @@ func (p *parser) dqToken(r rune) token {
}
}
func (p *parser) paramToken(r rune) token {
func (p *Parser) paramToken(r rune) token {
switch r {
case '}':
p.rune()
@@ -514,7 +535,7 @@ func (p *parser) paramToken(r rune) token {
p.rune()
return leftBrack
case '/':
if p.rune() == '/' {
if p.rune() == '/' && p.quote != paramExpRepl {
p.rune()
return dblSlash
}
@@ -537,7 +558,7 @@ func (p *parser) paramToken(r rune) token {
}
}
func (p *parser) arithmToken(r rune) token {
func (p *Parser) arithmToken(r rune) token {
switch r {
case '!':
if p.rune() == '=' {
@@ -651,6 +672,9 @@ func (p *parser) arithmToken(r rune) token {
return xorAssgn
}
return caret
case '[':
p.rune()
return leftBrack
case ']':
p.rune()
return rightBrack
@@ -660,27 +684,30 @@ func (p *parser) arithmToken(r rune) token {
case '?':
p.rune()
return quest
default: // ':'
case ':':
p.rune()
return colon
default: // '#'
p.rune()
return hash
}
}
func (p *parser) newLit(r rune) {
func (p *Parser) newLit(r rune) {
// don't let r == utf8.RuneSelf go to the second case as RuneLen
// would return -1
if r <= utf8.RuneSelf {
p.litBs = p.litBuf[:1]
p.litBs[0] = byte(r)
} else if p.npos <= len(p.bs) {
} else if p.bsp <= len(p.bs) {
w := utf8.RuneLen(r)
p.litBs = append(p.litBuf[:0], p.bs[p.npos-w:p.npos]...)
p.litBs = append(p.litBuf[:0], p.bs[p.bsp-w:p.bsp]...)
}
}
func (p *parser) discardLit(n int) { p.litBs = p.litBs[:len(p.litBs)-n] }
func (p *Parser) discardLit(n int) { p.litBs = p.litBs[:len(p.litBs)-n] }
func (p *parser) endLit() (s string) {
func (p *Parser) endLit() (s string) {
if p.r == utf8.RuneSelf {
s = string(p.litBs)
} else if len(p.litBs) > 0 {
@@ -690,7 +717,7 @@ func (p *parser) endLit() (s string) {
return
}
func (p *parser) advanceLitOther(r rune) {
func (p *Parser) advanceLitOther(r rune) {
tok := _LitWord
loop:
for p.newLit(r); r != utf8.RuneSelf; r = p.rune() {
@@ -732,18 +759,42 @@ loop:
if p.quote&allArithmExpr != 0 {
break loop
}
case ':', '=', '%', '?', '^', ',':
if p.quote == paramName && p.peekByte('(') {
tok = _Lit
break loop
}
case '?':
if p.quote == paramName && p.peekByte('(') {
tok = _Lit
break loop
}
fallthrough
case ':', '=', '%', '^', ',':
if p.quote&allArithmExpr != 0 || p.quote&allParamReg != 0 {
break loop
}
case '#', '[', '@':
case '@':
if p.quote == paramName && p.peekByte('(') {
tok = _Lit
break loop
}
fallthrough
case '#', '[':
if p.quote&allParamReg != 0 {
break loop
}
case '+', '-':
if r == '[' && p.lang != LangPOSIX && p.quote&allArithmExpr != 0 {
break loop
}
case '+':
if p.quote == paramName && p.peekByte('(') {
tok = _Lit
break loop
}
fallthrough
case '-':
switch p.quote {
case paramExpInd, paramExpLen, paramExpOff,
paramExpExp, paramExpRepl, sglQuotes:
case paramExpExp, paramExpRepl, sglQuotes:
default:
break loop
}
@@ -758,7 +809,7 @@ loop:
p.tok, p.val = tok, p.endLit()
}
func (p *parser) advanceLitNone(r rune) {
func (p *Parser) advanceLitNone(r rune) {
p.asPos = 0
tok := _LitWord
loop:
@@ -778,7 +829,7 @@ loop:
p.discardLit(1)
if r = p.rune(); r == '\\' {
p.discardLit(1)
r = p.rune()
p.rune()
}
}
}
@@ -804,12 +855,17 @@ loop:
}
case '=':
p.asPos = len(p.litBs) - 1
case '[':
if p.lang != LangPOSIX && len(p.litBs) > 1 && p.litBs[0] != '[' {
tok = _Lit
break loop
}
}
}
p.tok, p.val = tok, p.endLit()
}
func (p *parser) advanceLitDquote(r rune) {
func (p *Parser) advanceLitDquote(r rune) {
tok := _LitWord
loop:
for p.newLit(r); r != utf8.RuneSelf; r = p.rune() {
@@ -826,7 +882,7 @@ loop:
p.tok, p.val = tok, p.endLit()
}
func (p *parser) advanceLitHdoc(r rune) {
func (p *Parser) advanceLitHdoc(r rune) {
p.tok = _Lit
p.newLit(r)
if p.quote == hdocBodyTabs {
@@ -835,19 +891,25 @@ func (p *parser) advanceLitHdoc(r rune) {
}
}
lStart := len(p.litBs) - 1
loop:
for ; r != utf8.RuneSelf; r = p.rune() {
for ; ; r = p.rune() {
switch r {
case '`', '$':
break loop
p.val = p.endLit()
return
case '\\': // escaped byte follows
p.rune()
case '\n':
if bytes.Equal(p.litBs[lStart:len(p.litBs)-1], p.hdocStop) {
case '\n', utf8.RuneSelf:
if bytes.HasPrefix(p.litBs[lStart:], p.hdocStop) {
p.val = p.endLit()[:lStart]
if p.val == "" {
p.tok = illegalTok
}
p.hdocStop = nil
return
}
if r == utf8.RuneSelf {
return
}
if p.quote == hdocBodyTabs {
for p.peekByte('\t') {
p.rune()
@@ -856,22 +918,15 @@ loop:
lStart = len(p.litBs)
}
}
if bytes.Equal(p.litBs[lStart:], p.hdocStop) {
p.val = p.endLit()[:lStart]
p.hdocStop = nil
} else {
p.val = p.endLit()
}
}
func (p *parser) hdocLitWord() *Word {
func (p *Parser) hdocLitWord() *Word {
r := p.r
p.newLit(r)
pos, val := p.getPos(), ""
for {
pos := p.getPos()
for ; ; r = p.rune() {
if r == utf8.RuneSelf {
val = p.endLit()
break
return nil
}
if p.quote == hdocBodyTabs {
for r == '\t' {
@@ -882,21 +937,18 @@ func (p *parser) hdocLitWord() *Word {
for r != utf8.RuneSelf && r != '\n' {
r = p.rune()
}
lEnd := len(p.litBs)
if r != utf8.RuneSelf {
lEnd--
if bytes.HasPrefix(p.litBs[lStart:], p.hdocStop) {
p.hdocStop = nil
val := p.endLit()[:lStart]
if val == "" {
return nil
}
return p.word(p.wps(p.lit(pos, val)))
}
if bytes.Equal(p.litBs[lStart:lEnd], p.hdocStop) {
val = p.endLit()[:lStart]
break
}
r = p.rune()
}
l := p.lit(pos, val)
return p.word(p.singleWps(l))
}
func (p *parser) advanceLitRe(r rune) {
func (p *Parser) advanceLitRe(r rune) {
lparens := 0
loop:
for p.newLit(r); r != utf8.RuneSelf; r = p.rune() {
@@ -916,90 +968,90 @@ loop:
p.tok, p.val = _LitWord, p.endLit()
}
func testUnaryOp(val string) token {
func testUnaryOp(val string) UnTestOperator {
switch val {
case "!":
return exclMark
return TsNot
case "-e", "-a":
return tsExists
return TsExists
case "-f":
return tsRegFile
return TsRegFile
case "-d":
return tsDirect
return TsDirect
case "-c":
return tsCharSp
return TsCharSp
case "-b":
return tsBlckSp
return TsBlckSp
case "-p":
return tsNmPipe
return TsNmPipe
case "-S":
return tsSocket
return TsSocket
case "-L", "-h":
return tsSmbLink
return TsSmbLink
case "-k":
return tsSticky
return TsSticky
case "-g":
return tsGIDSet
return TsGIDSet
case "-u":
return tsUIDSet
return TsUIDSet
case "-G":
return tsGrpOwn
return TsGrpOwn
case "-O":
return tsUsrOwn
return TsUsrOwn
case "-N":
return tsModif
return TsModif
case "-r":
return tsRead
return TsRead
case "-w":
return tsWrite
return TsWrite
case "-x":
return tsExec
return TsExec
case "-s":
return tsNoEmpty
return TsNoEmpty
case "-t":
return tsFdTerm
return TsFdTerm
case "-z":
return tsEmpStr
return TsEmpStr
case "-n":
return tsNempStr
return TsNempStr
case "-o":
return tsOptSet
return TsOptSet
case "-v":
return tsVarSet
return TsVarSet
case "-R":
return tsRefVar
return TsRefVar
default:
return illegalTok
return 0
}
}
func testBinaryOp(val string) token {
func testBinaryOp(val string) BinTestOperator {
switch val {
case "==", "=":
return equal
return TsMatch
case "!=":
return nequal
return TsNoMatch
case "=~":
return tsReMatch
return TsReMatch
case "-nt":
return tsNewer
return TsNewer
case "-ot":
return tsOlder
return TsOlder
case "-ef":
return tsDevIno
return TsDevIno
case "-eq":
return tsEql
return TsEql
case "-ne":
return tsNeq
return TsNeq
case "-le":
return tsLeq
return TsLeq
case "-ge":
return tsGeq
return TsGeq
case "-lt":
return tsLss
return TsLss
case "-gt":
return tsGtr
return TsGtr
default:
return illegalTok
return 0
}
}

View File

@@ -7,9 +7,12 @@ import "fmt"
// Node represents an AST node.
type Node interface {
// Pos returns the first character of the node
// Pos returns the position of the first character of the node.
// Comments are ignored.
Pos() Pos
// End returns the character immediately after the node
// End returns the position of the character immediately after
// the node. If the character is a newline, the line number
// won't cross into the next line. Comments are ignored.
End() Pos
}
@@ -17,83 +20,85 @@ type Node interface {
type File struct {
Name string
Stmts []*Stmt
Comments []*Comment
lines []Pos
StmtList
}
// Pos is the internal representation of a position within a source
// file.
type Pos uint32
// IsValid reports whether the position is valid. All positions in nodes
// returned by Parse are valid.
func (p Pos) IsValid() bool { return p > 0 }
const maxPos = Pos(^uint32(0))
// Position describes a position within a source file including the line
// and column location. A Position is valid if the line number is > 0.
type Position struct {
Filename string // if any
Offset int // byte offset, starting at 0
Line int // line number, starting at 1
Column int // column number, starting at 1 (in bytes)
// StmtList is a list of statements with any number of trailing
// comments. Both lists can be empty.
type StmtList struct {
Stmts []*Stmt
Last []Comment
}
// IsValid reports whether the position is valid. All positions in nodes
// returned by Parse are valid.
func (p Position) IsValid() bool { return p.Line > 0 }
// String returns the position in the "file:line:column" form, or
// "line:column" if there is no filename available.
func (p Position) String() string {
prefix := ""
if p.Filename != "" {
prefix = p.Filename + ":"
func (s StmtList) pos() Pos {
if len(s.Stmts) > 0 {
return s.Stmts[0].Pos()
}
return fmt.Sprintf("%s%d:%d", prefix, p.Line, p.Column)
if len(s.Last) > 0 {
return s.Last[0].Pos()
}
return Pos{}
}
func (s StmtList) empty() bool {
return len(s.Stmts) == 0 && len(s.Last) == 0
}
// Pos is a position within a source file.
type Pos struct {
offs uint32
line, col uint16
}
// Offset returns the byte offset of the position in the original
// source file. Byte offsets start at 0.
func (p Pos) Offset() uint {
return uint(p.offs)
}
// Line returns the line number of the position, starting at 1.
func (p Pos) Line() uint {
return uint(p.line)
}
// Col returns the column number of the position, starting at 0. It
// counts in bytes.
func (p Pos) Col() uint {
return uint(p.col)
}
func (p Pos) String() string {
return fmt.Sprintf("%d:%d", p.Line(), p.Col())
}
// IsValid reports whether the position is valid. All positions in nodes
// returned by Parse are valid.
func (p Pos) IsValid() bool { return p.line > 0 }
func (p Pos) After(p2 Pos) bool { return p.offs > p2.offs }
func (f *File) Pos() Pos {
if len(f.Stmts) == 0 {
return 0
return Pos{}
}
return f.Stmts[0].Pos()
}
func (f *File) End() Pos {
if len(f.Stmts) == 0 {
return 0
return Pos{}
}
return f.Stmts[len(f.Stmts)-1].End()
}
func (f *File) Position(p Pos) (pos Position) {
pos.Filename = f.Name
pos.Offset = int(p) - 1
if i := searchPos(f.lines, p); i >= 0 {
pos.Line, pos.Column = i+1, int(p-f.lines[i])
}
return
}
func searchPos(a []Pos, x Pos) int {
i, j := 0, len(a)
for i < j {
h := i + (j-i)/2
if a[h] <= x {
i = h + 1
} else {
j = h
}
}
return i - 1
func posAddCol(p Pos, n int) Pos {
p.col += uint16(n)
p.offs += uint32(n)
return p
}
func posMax(p1, p2 Pos) Pos {
if p2 > p1 {
if p2.After(p1) {
return p2
}
return p1
@@ -106,29 +111,32 @@ type Comment struct {
}
func (c *Comment) Pos() Pos { return c.Hash }
func (c *Comment) End() Pos { return c.Hash + Pos(len(c.Text)) }
func (c *Comment) End() Pos { return posAddCol(c.Hash, len(c.Text)) }
// Stmt represents a statement, otherwise known as a compound command.
// It is compromised of a command and other components that may come
// before or after it.
type Stmt struct {
Comments []Comment
Cmd Command
Position Pos
Semicolon Pos
Negated bool
Background bool
Assigns []*Assign
Redirs []*Redirect
Negated bool // ! stmt
Background bool // stmt &
Coprocess bool // mksh's |&
Assigns []*Assign // a=x b=y stmt
Redirs []*Redirect // stmt >a <b
}
func (s *Stmt) Pos() Pos { return s.Position }
func (s *Stmt) End() Pos {
if s.Semicolon.IsValid() {
return s.Semicolon + 1
return posAddCol(s.Semicolon, 1)
}
end := s.Position
if s.Negated {
end++
end = posAddCol(end, 1)
}
if s.Cmd != nil {
end = s.Cmd.End()
@@ -144,6 +152,10 @@ func (s *Stmt) End() Pos {
// Command represents all nodes that are simple commands, which are
// directly placed in a Stmt.
//
// These are *CallExpr, *IfClause, *WhileClause, *ForClause,
// *CaseClause, *Block, *Subshell, *BinaryCmd, *FuncDecl, *ArithmCmd,
// *TestClause, *DeclClause, *LetClause, *TimeClause, and *CoprocClause.
type Command interface {
Node
commandNode()
@@ -152,7 +164,6 @@ type Command interface {
func (*CallExpr) commandNode() {}
func (*IfClause) commandNode() {}
func (*WhileClause) commandNode() {}
func (*UntilClause) commandNode() {}
func (*ForClause) commandNode() {}
func (*CaseClause) commandNode() {}
func (*Block) commandNode() {}
@@ -162,37 +173,60 @@ func (*FuncDecl) commandNode() {}
func (*ArithmCmd) commandNode() {}
func (*TestClause) commandNode() {}
func (*DeclClause) commandNode() {}
func (*EvalClause) commandNode() {}
func (*LetClause) commandNode() {}
func (*TimeClause) commandNode() {}
func (*CoprocClause) commandNode() {}
func (*SelectClause) commandNode() {}
// Assign represents an assignment to a variable.
//
// Here and elsewhere, Index can either mean an index into an indexed or
// an associative array. In the former, it's just an arithmetic
// expression. In the latter, it will be a word with a single DblQuoted
// part.
//
// If Naked is true, it's part of a DeclClause and doesn't contain a
// value. In that context, if the name wasn't a literal, it will be in
// Value instead of Name.
type Assign struct {
Append bool
Append bool // +=
Naked bool // without '='
Name *Lit
Value *Word
Index ArithmExpr // [i], ["k"]
Value *Word // =val
Array *ArrayExpr // =(arr)
}
func (a *Assign) Pos() Pos {
if a.Name != nil {
return a.Name.Pos()
if a.Name == nil {
return a.Value.Pos()
}
return a.Value.Pos()
return a.Name.Pos()
}
func (a *Assign) End() Pos {
if a.Value != nil {
return a.Value.End()
}
return a.Name.End() + 1
if a.Array != nil {
return a.Array.End()
}
if a.Index != nil {
return posAddCol(a.Index.End(), 2)
}
if a.Naked {
return a.Name.End()
}
return posAddCol(a.Name.End(), 1)
}
// Redirect represents an input/output redirection.
type Redirect struct {
OpPos Pos
Op RedirOperator
N *Lit
Word, Hdoc *Word
OpPos Pos
Op RedirOperator
N *Lit // N>
Word *Word // >word
Hdoc *Word // here-document body
}
func (r *Redirect) Pos() Pos {
@@ -215,72 +249,75 @@ func (c *CallExpr) End() Pos { return c.Args[len(c.Args)-1].End() }
// nested shell environment.
type Subshell struct {
Lparen, Rparen Pos
Stmts []*Stmt
StmtList
}
func (s *Subshell) Pos() Pos { return s.Lparen }
func (s *Subshell) End() Pos { return s.Rparen + 1 }
func (s *Subshell) End() Pos { return posAddCol(s.Rparen, 1) }
// Block represents a series of commands that should be executed in a
// nested scope.
type Block struct {
Lbrace, Rbrace Pos
Stmts []*Stmt
StmtList
}
func (b *Block) Pos() Pos { return b.Rbrace }
func (b *Block) End() Pos { return b.Rbrace + 1 }
func (b *Block) End() Pos { return posAddCol(b.Rbrace, 1) }
// IfClause represents an if statement.
type IfClause struct {
If, Then, Else, Fi Pos
CondStmts []*Stmt
ThenStmts []*Stmt
Elifs []*Elif
ElseStmts []*Stmt
Elif bool
IfPos Pos // pos of "elif" if Elif == true
ThenPos Pos
ElsePos Pos // pos of "elif" if FollowedByElif() == true
FiPos Pos // empty if Elif == true
Cond StmtList
Then StmtList
Else StmtList
}
func (c *IfClause) Pos() Pos { return c.If }
func (c *IfClause) End() Pos { return c.Fi + 2 }
// Elif represents an "else if" case in an if clause.
type Elif struct {
Elif, Then Pos
CondStmts []*Stmt
ThenStmts []*Stmt
func (c *IfClause) Pos() Pos { return c.IfPos }
func (c *IfClause) End() Pos {
if !c.FiPos.IsValid() {
return posAddCol(c.ElsePos, 4)
}
return posAddCol(c.FiPos, 2)
}
// WhileClause represents a while clause.
// FollowedByElif reports whether this IfClause is followed by an "elif"
// IfClause in its Else branch. This is true if Else.Stmts has exactly
// one statement with an IfClause whose Elif field is true.
func (c *IfClause) FollowedByElif() bool {
if len(c.Else.Stmts) != 1 {
return false
}
ic, _ := c.Else.Stmts[0].Cmd.(*IfClause)
return ic != nil && ic.Elif
}
// WhileClause represents a while or an until clause.
type WhileClause struct {
While, Do, Done Pos
CondStmts []*Stmt
DoStmts []*Stmt
WhilePos, DoPos, DonePos Pos
Until bool
Cond StmtList
Do StmtList
}
func (w *WhileClause) Pos() Pos { return w.While }
func (w *WhileClause) End() Pos { return w.Done + 4 }
// UntilClause represents an until clause.
type UntilClause struct {
Until, Do, Done Pos
CondStmts []*Stmt
DoStmts []*Stmt
}
func (u *UntilClause) Pos() Pos { return u.Until }
func (u *UntilClause) End() Pos { return u.Done + 4 }
func (w *WhileClause) Pos() Pos { return w.WhilePos }
func (w *WhileClause) End() Pos { return posAddCol(w.DonePos, 4) }
// ForClause represents a for clause.
type ForClause struct {
For, Do, Done Pos
Loop Loop
DoStmts []*Stmt
ForPos, DoPos, DonePos Pos
Loop Loop
Do StmtList
}
func (f *ForClause) Pos() Pos { return f.For }
func (f *ForClause) End() Pos { return f.Done + 4 }
func (f *ForClause) Pos() Pos { return f.ForPos }
func (f *ForClause) End() Pos { return posAddCol(f.DonePos, 4) }
// Loop represents all nodes that can be loops in a for clause.
// Loop holds either *WordIter or *CStyleLoop.
type Loop interface {
Node
loopNode()
@@ -292,24 +329,24 @@ func (*CStyleLoop) loopNode() {}
// WordIter represents the iteration of a variable over a series of
// words in a for clause.
type WordIter struct {
Name *Lit
List []*Word
Name *Lit
Items []*Word
}
func (w *WordIter) Pos() Pos { return w.Name.Pos() }
func (w *WordIter) End() Pos { return posMax(w.Name.End(), wordLastEnd(w.List)) }
func (w *WordIter) End() Pos { return posMax(w.Name.End(), wordLastEnd(w.Items)) }
// CStyleLoop represents the behaviour of a for clause similar to the C
// language.
//
// This node will never appear when in PosixConformant mode.
// This node will only appear with LangBash.
type CStyleLoop struct {
Lparen, Rparen Pos
Init, Cond, Post ArithmExpr
}
func (c *CStyleLoop) Pos() Pos { return c.Lparen }
func (c *CStyleLoop) End() Pos { return c.Rparen + 2 }
func (c *CStyleLoop) End() Pos { return posAddCol(c.Rparen, 2) }
// BinaryCmd represents a binary expression between two statements.
type BinaryCmd struct {
@@ -323,10 +360,10 @@ func (b *BinaryCmd) End() Pos { return b.Y.End() }
// FuncDecl represents the declaration of a function.
type FuncDecl struct {
Position Pos
BashStyle bool
Name *Lit
Body *Stmt
Position Pos
RsrvWord bool // non-posix "function f()" style
Name *Lit
Body *Stmt
}
func (f *FuncDecl) Pos() Pos { return f.Position }
@@ -342,6 +379,9 @@ func (w *Word) Pos() Pos { return w.Parts[0].Pos() }
func (w *Word) End() Pos { return w.Parts[len(w.Parts)-1].End() }
// WordPart represents all nodes that can form a word.
//
// These are *Lit, *SglQuoted, *DblQuoted, *ParamExp, *CmdSubst,
// *ArithmExp, *ProcSubst, and *ExtGlob.
type WordPart interface {
Node
wordPartNode()
@@ -354,7 +394,6 @@ func (*ParamExp) wordPartNode() {}
func (*CmdSubst) wordPartNode() {}
func (*ArithmExp) wordPartNode() {}
func (*ProcSubst) wordPartNode() {}
func (*ArrayExpr) wordPartNode() {}
func (*ExtGlob) wordPartNode() {}
// Lit represents an unquoted string consisting of characters that were
@@ -369,24 +408,18 @@ func (l *Lit) End() Pos { return l.ValueEnd }
// SglQuoted represents a string within single quotes.
type SglQuoted struct {
Position Pos
Dollar bool
Value string
Left, Right Pos
Dollar bool // $''
Value string
}
func (q *SglQuoted) Pos() Pos { return q.Position }
func (q *SglQuoted) End() Pos {
end := q.Position + 2 + Pos(len(q.Value))
if q.Dollar {
end++
}
return end
}
func (q *SglQuoted) Pos() Pos { return q.Left }
func (q *SglQuoted) End() Pos { return q.Right }
// DblQuoted represents a list of nodes within double quotes.
type DblQuoted struct {
Position Pos
Dollar bool
Dollar bool // $""
Parts []WordPart
}
@@ -394,52 +427,57 @@ func (q *DblQuoted) Pos() Pos { return q.Position }
func (q *DblQuoted) End() Pos {
if len(q.Parts) == 0 {
if q.Dollar {
return q.Position + 3
return posAddCol(q.Position, 3)
}
return q.Position + 2
return posAddCol(q.Position, 2)
}
return q.Parts[len(q.Parts)-1].End() + 1
return posAddCol(q.Parts[len(q.Parts)-1].End(), 1)
}
// CmdSubst represents a command substitution.
type CmdSubst struct {
Left, Right Pos
Stmts []*Stmt
StmtList
TempFile bool // mksh's ${ foo;}
ReplyVar bool // mksh's ${|foo;}
}
func (c *CmdSubst) Pos() Pos { return c.Left }
func (c *CmdSubst) End() Pos { return c.Right + 1 }
func (c *CmdSubst) End() Pos { return posAddCol(c.Right, 1) }
// ParamExp represents a parameter expansion.
type ParamExp struct {
Dollar, Rbrace Pos
Short bool
Length, Excl bool // TODO(mvdan): rename Excl in 2.0 (Indirect, etc)
Short bool // $a instead of ${a}
Indirect bool // ${!a}
Length bool // ${#a}
Width bool // ${%a}
Param *Lit
Ind *Index
Slice *Slice
Repl *Replace
Exp *Expansion
Index ArithmExpr // ${a[i]}, ${a["k"]}
Slice *Slice // ${a:x:y}
Repl *Replace // ${a/x/y}
Exp *Expansion // ${a:-b}, ${a#b}, etc
}
func (p *ParamExp) Pos() Pos { return p.Dollar }
func (p *ParamExp) End() Pos {
if !p.Short {
return p.Rbrace + 1
return posAddCol(p.Rbrace, 1)
}
if p.Index != nil {
return posAddCol(p.Index.End(), 1)
}
return p.Param.End()
}
// Index represents access to an array via an index inside a ParamExp.
//
// This node will never appear when in PosixConformant mode.
type Index struct {
Expr ArithmExpr
func (p *ParamExp) nakedIndex() bool {
return p.Short && p.Index != nil
}
// Slice represents character slicing inside a ParamExp.
//
// This node will never appear when in PosixConformant mode.
// This node will only appear in LangBash and LangMirBSDKorn.
type Slice struct {
Offset, Length ArithmExpr
}
@@ -460,30 +498,34 @@ type Expansion struct {
// ArithmExp represents an arithmetic expansion.
type ArithmExp struct {
Left, Right Pos
Bracket bool
Bracket bool // deprecated $[expr] form
Unsigned bool // mksh's $((# expr))
X ArithmExpr
}
func (a *ArithmExp) Pos() Pos { return a.Left }
func (a *ArithmExp) End() Pos {
if a.Bracket {
return a.Right + 1
return posAddCol(a.Right, 1)
}
return a.Right + 2
return posAddCol(a.Right, 2)
}
// ArithmCmd represents an arithmetic command.
//
// This node will never appear when in PosixConformant mode.
// This node will only appear in LangBash and LangMirBSDKorn.
type ArithmCmd struct {
Left, Right Pos
Unsigned bool // mksh's ((# expr))
X ArithmExpr
}
func (a *ArithmCmd) Pos() Pos { return a.Left }
func (a *ArithmCmd) End() Pos { return a.Right + 2 }
func (a *ArithmCmd) End() Pos { return posAddCol(a.Right, 2) }
// ArithmExpr represents all nodes that form arithmetic expressions.
//
// These are *BinaryArithm, *UnaryArithm, *ParenArithm, and *Word.
type ArithmExpr interface {
Node
arithmExprNode()
@@ -497,15 +539,12 @@ func (*Word) arithmExprNode() {}
// BinaryArithm represents a binary expression between two arithmetic
// expression.
//
// If Op is any assign operator, X will be a *Word with a single *Lit
// If Op is any assign operator, X will be a word with a single *Lit
// whose value is a valid name.
//
// Ternary operators like "a ? b : c" are fit into this structure. Thus,
// if Op == Quest, Y will be a *BinaryArithm with Op == Colon. Op can
// only be Colon in that scenario.
//
// TODO(mvdan): we probably want to split up assigns in 2.0 (X would be
// a *Lit) to simplify the rules here. Perhaps reuse the Assign type?
type BinaryArithm struct {
OpPos Pos
Op BinAritOperator
@@ -518,11 +557,8 @@ func (b *BinaryArithm) End() Pos { return b.Y.End() }
// UnaryArithm represents an unary expression over a node, either before
// or after it.
//
// If Op is Inc or Dec, X will be a *Word with a single *Lit whose value
// If Op is Inc or Dec, X will be a word with a single *Lit whose value
// is a valid name.
//
// TODO(mvdan): consider splitting up Inc/Dec like the assigns above in
// 2.0.
type UnaryArithm struct {
OpPos Pos
Op UnAritOperator
@@ -539,7 +575,7 @@ func (u *UnaryArithm) Pos() Pos {
func (u *UnaryArithm) End() Pos {
if u.Post {
return u.OpPos + 2
return posAddCol(u.OpPos, 2)
}
return u.X.End()
}
@@ -552,38 +588,45 @@ type ParenArithm struct {
}
func (p *ParenArithm) Pos() Pos { return p.Lparen }
func (p *ParenArithm) End() Pos { return p.Rparen + 1 }
func (p *ParenArithm) End() Pos { return posAddCol(p.Rparen, 1) }
// CaseClause represents a case (switch) clause.
type CaseClause struct {
Case, Esac Pos
Word *Word
List []*PatternList
Items []*CaseItem
Last []Comment
}
func (c *CaseClause) Pos() Pos { return c.Case }
func (c *CaseClause) End() Pos { return c.Esac + 4 }
func (c *CaseClause) End() Pos { return posAddCol(c.Esac, 4) }
// PatternList represents a pattern list (case) within a CaseClause.
type PatternList struct {
// CaseItem represents a pattern list (case) within a CaseClause.
type CaseItem struct {
Op CaseOperator
OpPos Pos
Comments []Comment
Patterns []*Word
Stmts []*Stmt
StmtList
}
func (c *CaseItem) Pos() Pos { return c.Patterns[0].Pos() }
func (c *CaseItem) End() Pos { return posAddCol(c.OpPos, len(c.Op.String())) }
// TestClause represents a Bash extended test clause.
//
// This node will never appear when in PosixConformant mode.
// This node will only appear in LangBash and LangMirBSDKorn.
type TestClause struct {
Left, Right Pos
X TestExpr
}
func (t *TestClause) Pos() Pos { return t.Left }
func (t *TestClause) End() Pos { return t.Right + 2 }
func (t *TestClause) End() Pos { return posAddCol(t.Right, 2) }
// TestExpr represents all nodes that form arithmetic expressions.
//
// These are *BinaryTest, *UnaryTest, *ParenTest, and *Word.
type TestExpr interface {
Node
testExprNode()
@@ -624,42 +667,60 @@ type ParenTest struct {
}
func (p *ParenTest) Pos() Pos { return p.Lparen }
func (p *ParenTest) End() Pos { return p.Rparen + 1 }
func (p *ParenTest) End() Pos { return posAddCol(p.Rparen, 1) }
// DeclClause represents a Bash declare clause.
//
// This node will never appear when in PosixConformant mode.
// This node will only appear with LangBash.
type DeclClause struct {
Position Pos
Variant string
Opts []*Word
Assigns []*Assign
Variant *Lit // "declare", "local", etc
Opts []*Word
Assigns []*Assign
}
func (d *DeclClause) Pos() Pos { return d.Position }
func (d *DeclClause) Pos() Pos { return d.Variant.Pos() }
func (d *DeclClause) End() Pos {
if len(d.Assigns) > 0 {
return d.Assigns[len(d.Assigns)-1].End()
}
return wordLastEnd(d.Opts)
if len(d.Opts) > 0 {
return wordLastEnd(d.Opts)
}
return d.Variant.End()
}
// ArrayExpr represents a Bash array expression.
//
// This node will never appear when in PosixConformant mode.
// This node will only appear with LangBash.
type ArrayExpr struct {
Lparen, Rparen Pos
List []*Word
Elems []*ArrayElem
Last []Comment
}
func (a *ArrayExpr) Pos() Pos { return a.Lparen }
func (a *ArrayExpr) End() Pos { return a.Rparen + 1 }
func (a *ArrayExpr) End() Pos { return posAddCol(a.Rparen, 1) }
// ArrayElem represents a Bash array element.
type ArrayElem struct {
Index ArithmExpr // [i]=, ["k"]=
Value *Word
Comments []Comment
}
func (a *ArrayElem) Pos() Pos {
if a.Index != nil {
return a.Index.Pos()
}
return a.Value.Pos()
}
func (a *ArrayElem) End() Pos { return a.Value.End() }
// ExtGlob represents a Bash extended globbing expression. Note that
// these are parsed independently of whether shopt has been called or
// not.
//
// This node will never appear when in PosixConformant mode.
// This node will only appear in LangBash and LangMirBSDKorn.
type ExtGlob struct {
OpPos Pos
Op GlobOperator
@@ -667,42 +728,39 @@ type ExtGlob struct {
}
func (e *ExtGlob) Pos() Pos { return e.OpPos }
func (e *ExtGlob) End() Pos { return e.Pattern.End() + 1 }
func (e *ExtGlob) End() Pos { return posAddCol(e.Pattern.End(), 1) }
// ProcSubst represents a Bash process substitution.
//
// This node will never appear when in PosixConformant mode.
// This node will only appear with LangBash.
type ProcSubst struct {
OpPos, Rparen Pos
Op ProcOperator
Stmts []*Stmt
StmtList
}
func (s *ProcSubst) Pos() Pos { return s.OpPos }
func (s *ProcSubst) End() Pos { return s.Rparen + 1 }
func (s *ProcSubst) End() Pos { return posAddCol(s.Rparen, 1) }
// EvalClause represents a Bash eval clause.
// TimeClause represents a Bash time clause.
//
// This node will never appear when in PosixConformant mode.
//
// TODO(mvdan): EvalClause is actually pointless, as any non-trivial use
// of eval will involve parsing the program at run-time. Remove in 2.0.
type EvalClause struct {
Eval Pos
// This node will only appear in LangBash and LangMirBSDKorn.
type TimeClause struct {
Time Pos
Stmt *Stmt
}
func (e *EvalClause) Pos() Pos { return e.Eval }
func (e *EvalClause) End() Pos {
if e.Stmt != nil {
return e.Stmt.End()
func (c *TimeClause) Pos() Pos { return c.Time }
func (c *TimeClause) End() Pos {
if c.Stmt == nil {
return posAddCol(c.Time, 4)
}
return e.Eval + 4
return c.Stmt.End()
}
// CoprocClause represents a Bash coproc clause.
//
// This node will never appear when in PosixConformant mode.
// This node will only appear with LangBash.
type CoprocClause struct {
Coproc Pos
Name *Lit
@@ -714,7 +772,7 @@ func (c *CoprocClause) End() Pos { return c.Stmt.End() }
// LetClause represents a Bash let clause.
//
// This node will never appear when in PosixConformant mode.
// This node will only appear in LangBash and LangMirBSDKorn.
type LetClause struct {
Let Pos
Exprs []ArithmExpr
@@ -723,9 +781,19 @@ type LetClause struct {
func (l *LetClause) Pos() Pos { return l.Let }
func (l *LetClause) End() Pos { return l.Exprs[len(l.Exprs)-1].End() }
// SelectClause represents a Bash select clause.
type SelectClause struct {
SelectPos, DoPos, DonePos Pos
Loop WordIter
Do StmtList
}
func (f *SelectClause) Pos() Pos { return f.SelectPos }
func (f *SelectClause) End() Pos { return posAddCol(f.DonePos, 4) }
func wordLastEnd(ws []*Word) Pos {
if len(ws) == 0 {
return 0
return Pos{}
}
return ws[len(ws)-1].End()
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

244
vendor/github.com/mvdan/sh/syntax/simplify.go generated vendored Normal file
View File

@@ -0,0 +1,244 @@
// Copyright (c) 2017, Daniel Martí <mvdan@mvdan.cc>
// See LICENSE for licensing information
package syntax
import "bytes"
// Simplify simplifies a given program and returns whether any changes
// were made.
//
// This function is EXPERIMENTAL; it may change or disappear at any
// point until this notice is removed.
func Simplify(f *File) bool {
s := simplifier{}
Walk(f, s.visit)
return s.modified
}
type simplifier struct {
modified bool
}
func (s *simplifier) visit(node Node) bool {
switch x := node.(type) {
case *Assign:
if x.Index != nil {
x.Index = s.removeParensArithm(x.Index)
x.Index = s.inlineSimpleParams(x.Index)
}
case *ParamExp:
if x.Index != nil {
x.Index = s.removeParensArithm(x.Index)
x.Index = s.inlineSimpleParams(x.Index)
}
if x.Slice == nil {
break
}
if x.Slice.Offset != nil {
x.Slice.Offset = s.removeParensArithm(x.Slice.Offset)
x.Slice.Offset = s.inlineSimpleParams(x.Slice.Offset)
}
if x.Slice.Length != nil {
x.Slice.Length = s.removeParensArithm(x.Slice.Length)
x.Slice.Length = s.inlineSimpleParams(x.Slice.Length)
}
case *ArithmExp:
x.X = s.removeParensArithm(x.X)
x.X = s.inlineSimpleParams(x.X)
case *ArithmCmd:
x.X = s.removeParensArithm(x.X)
x.X = s.inlineSimpleParams(x.X)
case *ParenArithm:
x.X = s.removeParensArithm(x.X)
x.X = s.inlineSimpleParams(x.X)
case *BinaryArithm:
x.X = s.inlineSimpleParams(x.X)
x.Y = s.inlineSimpleParams(x.Y)
case *CmdSubst:
x.Stmts = s.inlineSubshell(x.Stmts)
case *Subshell:
x.Stmts = s.inlineSubshell(x.Stmts)
case *Word:
x.Parts = s.simplifyWord(x.Parts)
case *TestClause:
x.X = s.removeParensTest(x.X)
x.X = s.removeNegateTest(x.X)
case *ParenTest:
x.X = s.removeParensTest(x.X)
x.X = s.removeNegateTest(x.X)
case *BinaryTest:
x.X = s.unquoteParams(x.X)
x.X = s.removeNegateTest(x.X)
switch x.Op {
case TsMatch, TsNoMatch:
// unquoting enables globbing
default:
x.Y = s.unquoteParams(x.Y)
}
x.Y = s.removeNegateTest(x.Y)
case *UnaryTest:
x.X = s.unquoteParams(x.X)
}
return true
}
func (s *simplifier) simplifyWord(wps []WordPart) []WordPart {
parts:
for i, wp := range wps {
dq, _ := wp.(*DblQuoted)
if dq == nil || len(dq.Parts) != 1 {
break
}
lit, _ := dq.Parts[0].(*Lit)
if lit == nil {
break
}
var buf bytes.Buffer
escaped := false
for _, r := range lit.Value {
switch r {
case '\\':
escaped = !escaped
if escaped {
continue
}
case '\'':
continue parts
case '$', '"', '`':
escaped = false
default:
if escaped {
continue parts
}
escaped = false
}
buf.WriteRune(r)
}
newVal := buf.String()
if newVal == lit.Value {
break
}
s.modified = true
wps[i] = &SglQuoted{
Left: dq.Pos(),
Right: dq.End(),
Dollar: dq.Dollar,
Value: newVal,
}
}
return wps
}
func (s *simplifier) removeParensArithm(x ArithmExpr) ArithmExpr {
for {
par, _ := x.(*ParenArithm)
if par == nil {
return x
}
s.modified = true
x = par.X
}
}
func (s *simplifier) inlineSimpleParams(x ArithmExpr) ArithmExpr {
w, _ := x.(*Word)
if w == nil || len(w.Parts) != 1 {
return x
}
pe, _ := w.Parts[0].(*ParamExp)
if pe == nil || !ValidName(pe.Param.Value) {
return x
}
if pe.Indirect || pe.Length || pe.Width || pe.Slice != nil ||
pe.Repl != nil || pe.Exp != nil {
return x
}
if pe.Index != nil {
s.modified = true
pe.Short = true
return w
}
s.modified = true
return &Word{Parts: []WordPart{pe.Param}}
}
func (s *simplifier) inlineSubshell(stmts []*Stmt) []*Stmt {
for len(stmts) == 1 {
st := stmts[0]
if st.Negated || st.Background || st.Coprocess ||
len(st.Assigns) > 0 || len(st.Redirs) > 0 {
break
}
sub, _ := st.Cmd.(*Subshell)
if sub == nil {
break
}
s.modified = true
stmts = sub.Stmts
}
return stmts
}
func (s *simplifier) unquoteParams(x TestExpr) TestExpr {
w, _ := x.(*Word)
if w == nil || len(w.Parts) != 1 {
return x
}
dq, _ := w.Parts[0].(*DblQuoted)
if dq == nil || len(dq.Parts) != 1 {
return x
}
if _, ok := dq.Parts[0].(*ParamExp); !ok {
return x
}
s.modified = true
w.Parts = dq.Parts
return w
}
func (s *simplifier) removeParensTest(x TestExpr) TestExpr {
for {
par, _ := x.(*ParenTest)
if par == nil {
return x
}
s.modified = true
x = par.X
}
}
func (s *simplifier) removeNegateTest(x TestExpr) TestExpr {
u, _ := x.(*UnaryTest)
if u == nil || u.Op != TsNot {
return x
}
switch y := u.X.(type) {
case *UnaryTest:
switch y.Op {
case TsEmpStr:
y.Op = TsNempStr
s.modified = true
return y
case TsNempStr:
y.Op = TsEmpStr
s.modified = true
return y
case TsNot:
s.modified = true
return y.X
}
case *BinaryTest:
switch y.Op {
case TsMatch:
y.Op = TsNoMatch
s.modified = true
return y
case TsNoMatch:
y.Op = TsMatch
s.modified = true
return y
}
}
return x
}

View File

@@ -1,12 +1,12 @@
// Code generated by "stringer -type token"; DO NOT EDIT
// Code generated by "stringer -type token -linecoms"; DO NOT EDIT.
package syntax
import "fmt"
const _token_name = "illegalTokEOFLitLitWordLitRedir'\"`&&&||||&$$'$\"${$[$($(([(((}])));;;;&;;&!++--***==!=<=>=+=-=*=/=%=&=|=^=<<=>>=>>><<><&>&>|<<<<-<<<&>&>><(>(+:+-:-?:?=:=%%%###^^^,,,@///:-e-f-d-c-b-p-S-L-k-g-u-G-O-N-r-w-x-s-t-z-n-o-v-R=~-nt-ot-ef-eq-ne-le-ge-lt-gt?(*(+(@(!("
const _token_name = "illegalTokEOFLitLitWordLitRedir'\"`&&&||||&$$'$\"${$[$($(([(((}])));;;;&;;&;|!++--***==!=<=>=+=-=*=/=%=&=|=^=<<=>>=>>><<><&>&>|<<<<-<<<&>&>><(>(+:+-:-?:?=:=%%%###^^^,,,@///:-e-f-d-c-b-p-S-L-k-g-u-G-O-N-r-w-x-s-t-z-n-o-v-R=~-nt-ot-ef-eq-ne-le-ge-lt-gt?(*(+(@(!("
var _token_index = [...]uint16{0, 10, 13, 16, 23, 31, 32, 33, 34, 35, 37, 39, 40, 42, 43, 45, 47, 49, 51, 53, 56, 57, 58, 60, 61, 62, 63, 65, 66, 68, 70, 73, 74, 76, 78, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99, 101, 103, 105, 108, 111, 112, 114, 115, 117, 119, 121, 123, 125, 128, 131, 133, 136, 138, 140, 141, 143, 144, 146, 147, 149, 150, 152, 153, 155, 156, 158, 159, 161, 162, 164, 165, 166, 168, 169, 171, 173, 175, 177, 179, 181, 183, 185, 187, 189, 191, 193, 195, 197, 199, 201, 203, 205, 207, 209, 211, 213, 215, 217, 219, 222, 225, 228, 231, 234, 237, 240, 243, 246, 248, 250, 252, 254, 256}
var _token_index = [...]uint16{0, 10, 13, 16, 23, 31, 32, 33, 34, 35, 37, 39, 40, 42, 43, 45, 47, 49, 51, 53, 56, 57, 58, 60, 61, 62, 63, 65, 66, 68, 70, 73, 75, 76, 78, 80, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99, 101, 103, 105, 107, 110, 113, 114, 116, 117, 119, 121, 123, 125, 127, 130, 133, 135, 138, 140, 142, 143, 145, 146, 148, 149, 151, 152, 154, 155, 157, 158, 160, 161, 163, 164, 166, 167, 168, 170, 171, 173, 175, 177, 179, 181, 183, 185, 187, 189, 191, 193, 195, 197, 199, 201, 203, 205, 207, 209, 211, 213, 215, 217, 219, 221, 224, 227, 230, 233, 236, 239, 242, 245, 248, 250, 252, 254, 256, 258}
func (i token) String() string {
if i >= token(len(_token_index)-1) {

View File

@@ -8,25 +8,26 @@ type token uint32
// Modified version of golang.org/x/tools/cmd/stringer that gets the
// string value from the inline comment of each constant, if there is
// one. Also removes leading '_'.
//go:generate stringer -type token
//go:generate stringer -type token -linecoms
// The list of all possible tokens.
const (
illegalTok token = iota
_EOF
_Lit
_LitWord
_LitRedir
_EOF // EOF
_Lit // Lit
_LitWord // LitWord
_LitRedir // LitRedir
sglQuote // '
dblQuote // "
bckQuote // `
and // &
andAnd // &&
orOr // ||
or // |
pipeAll // |&
and // &
andAnd // &&
orOr // ||
or // |
orAnd // |&
dollar // $
dollSglQuote // $'
@@ -46,8 +47,9 @@ const (
semicolon // ;
dblSemicolon // ;;
semiFall // ;&
dblSemiFall // ;;&
semiAnd // ;&
dblSemiAnd // ;;&
semiOr // ;|
exclMark // !
addAdd // ++
@@ -196,9 +198,10 @@ const (
type CaseOperator token
const (
DblSemicolon = CaseOperator(dblSemicolon) + iota
SemiFall
DblSemiFall
Break = CaseOperator(dblSemicolon) + iota
Fallthrough
Resume
ResumeKorn
)
type ParExpOperator token
@@ -316,16 +319,12 @@ const (
TsGeq
TsLss
TsGtr
AndTest = BinTestOperator(andAnd)
OrTest = BinTestOperator(orOr)
// TODO(mvdan): == and != are pattern matches; use more
// appropriate names like TsMatch and TsNoMatch in 2.0
TsEqual = BinTestOperator(equal)
TsNequal = BinTestOperator(nequal)
TsBefore = BinTestOperator(rdrIn)
TsAfter = BinTestOperator(rdrOut)
// Deprecated: now parses as TsEqual
TsAssgn = BinTestOperator(assgn) // TODO(mvdan): remove in 2.0
AndTest = BinTestOperator(andAnd)
OrTest = BinTestOperator(orOr)
TsMatch = BinTestOperator(equal)
TsNoMatch = BinTestOperator(nequal)
TsBefore = BinTestOperator(rdrIn)
TsAfter = BinTestOperator(rdrOut)
)
func (o RedirOperator) String() string { return token(o).String() }

View File

@@ -5,8 +5,8 @@ package syntax
import "fmt"
func walkStmts(stmts []*Stmt, f func(Node) bool) {
for _, s := range stmts {
func walkStmts(sl StmtList, f func(Node) bool) {
for _, s := range sl.Stmts {
Walk(s, f)
}
}
@@ -28,7 +28,7 @@ func Walk(node Node, f func(Node) bool) {
switch x := node.(type) {
case *File:
walkStmts(x.Stmts, f)
walkStmts(x.StmtList, f)
case *Stmt:
if x.Cmd != nil {
Walk(x.Cmd, f)
@@ -46,6 +46,12 @@ func Walk(node Node, f func(Node) bool) {
if x.Value != nil {
Walk(x.Value, f)
}
if x.Index != nil {
Walk(x.Index, f)
}
if x.Array != nil {
Walk(x.Array, f)
}
case *Redirect:
if x.N != nil {
Walk(x.N, f)
@@ -57,29 +63,25 @@ func Walk(node Node, f func(Node) bool) {
case *CallExpr:
walkWords(x.Args, f)
case *Subshell:
walkStmts(x.Stmts, f)
walkStmts(x.StmtList, f)
case *Block:
walkStmts(x.Stmts, f)
walkStmts(x.StmtList, f)
case *IfClause:
walkStmts(x.CondStmts, f)
walkStmts(x.ThenStmts, f)
for _, elif := range x.Elifs {
walkStmts(elif.CondStmts, f)
walkStmts(elif.ThenStmts, f)
}
walkStmts(x.ElseStmts, f)
walkStmts(x.Cond, f)
walkStmts(x.Then, f)
walkStmts(x.Else, f)
case *WhileClause:
walkStmts(x.CondStmts, f)
walkStmts(x.DoStmts, f)
case *UntilClause:
walkStmts(x.CondStmts, f)
walkStmts(x.DoStmts, f)
walkStmts(x.Cond, f)
walkStmts(x.Do, f)
case *ForClause:
Walk(x.Loop, f)
walkStmts(x.DoStmts, f)
walkStmts(x.Do, f)
case *SelectClause:
Walk(&x.Loop, f)
walkStmts(x.Do, f)
case *WordIter:
Walk(x.Name, f)
walkWords(x.List, f)
walkWords(x.Items, f)
case *CStyleLoop:
if x.Init != nil {
Walk(x.Init, f)
@@ -107,29 +109,27 @@ func Walk(node Node, f func(Node) bool) {
Walk(wp, f)
}
case *CmdSubst:
walkStmts(x.Stmts, f)
walkStmts(x.StmtList, f)
case *ParamExp:
if x.Param != nil {
Walk(x.Param, f)
}
if x.Ind != nil {
Walk(x.Ind.Expr, f)
Walk(x.Param, f)
if x.Index != nil {
Walk(x.Index, f)
}
if x.Repl != nil {
Walk(x.Repl.Orig, f)
Walk(x.Repl.With, f)
if x.Repl.Orig != nil {
Walk(x.Repl.Orig, f)
}
if x.Repl.With != nil {
Walk(x.Repl.With, f)
}
}
if x.Exp != nil {
if x.Exp != nil && x.Exp.Word != nil {
Walk(x.Exp.Word, f)
}
case *ArithmExp:
if x.X != nil {
Walk(x.X, f)
}
Walk(x.X, f)
case *ArithmCmd:
if x.X != nil {
Walk(x.X, f)
}
Walk(x.X, f)
case *BinaryArithm:
Walk(x.X, f)
Walk(x.Y, f)
@@ -146,10 +146,12 @@ func Walk(node Node, f func(Node) bool) {
Walk(x.X, f)
case *CaseClause:
Walk(x.Word, f)
for _, pl := range x.List {
walkWords(pl.Patterns, f)
walkStmts(pl.Stmts, f)
for _, ci := range x.Items {
Walk(ci, f)
}
case *CaseItem:
walkWords(x.Patterns, f)
walkStmts(x.StmtList, f)
case *TestClause:
Walk(x.X, f)
case *DeclClause:
@@ -158,12 +160,19 @@ func Walk(node Node, f func(Node) bool) {
Walk(a, f)
}
case *ArrayExpr:
walkWords(x.List, f)
for _, el := range x.Elems {
Walk(el, f)
}
case *ArrayElem:
if x.Index != nil {
Walk(x.Index, f)
}
Walk(x.Value, f)
case *ExtGlob:
Walk(x.Pattern, f)
case *ProcSubst:
walkStmts(x.Stmts, f)
case *EvalClause:
walkStmts(x.StmtList, f)
case *TimeClause:
if x.Stmt != nil {
Walk(x.Stmt, f)
}

View File

@@ -251,12 +251,18 @@ func (u *UUID) UnmarshalText(text []byte) (err error) {
b := u[:]
for i, byteGroup := range byteGroups {
if i > 0 {
if t[0] != '-' {
err = fmt.Errorf("uuid: invalid string format")
if i > 0 && t[0] == '-' {
t = t[1:]
} else if i > 0 && t[0] != '-' {
err = fmt.Errorf("uuid: invalid string format")
return
}
if i == 2 {
if !bytes.Contains([]byte("012345"), []byte{t[0]}) {
err = fmt.Errorf("uuid: invalid version number: %s", t[0])
return
}
t = t[1:]
}
if len(t) < byteGroup {
@@ -266,11 +272,12 @@ func (u *UUID) UnmarshalText(text []byte) (err error) {
if i == 4 && len(t) > byteGroup &&
((braced && t[byteGroup] != '}') || len(t[byteGroup:]) > 1 || !braced) {
err = fmt.Errorf("uuid: UUID string too long: %s", text)
err = fmt.Errorf("uuid: UUID string too long: %s", t)
return
}
_, err = hex.Decode(b[:byteGroup/2], t[:byteGroup])
if err != nil {
return
}

View File

@@ -246,6 +246,25 @@ It is possible to mark a flag as hidden, meaning it will still function as norma
flags.MarkHidden("secretFlag")
```
## Disable sorting of flags
`pflag` allows you to disable sorting of flags for help and usage message.
**Example**:
```go
flags.BoolP("verbose", "v", false, "verbose output")
flags.String("coolflag", "yeaah", "it's really cool flag")
flags.Int("usefulflag", 777, "sometimes it's very useful")
flags.SortFlags = false
flags.PrintDefaults()
```
**Output**:
```
-v, --verbose verbose output
--coolflag string it's really cool flag (default "yeaah")
--usefulflag int sometimes it's very useful (default 777)
```
## Supporting Go flags when using pflag
In order to support flags defined using Go's `flag` package, they must be added to the `pflag` flagset. This is usually necessary
to support flags defined by third-party dependencies (e.g. `golang/glog`).
@@ -270,8 +289,8 @@ func main() {
You can see the full reference documentation of the pflag package
[at godoc.org][3], or through go's standard documentation system by
running `godoc -http=:6060` and browsing to
[http://localhost:6060/pkg/github.com/ogier/pflag][2] after
[http://localhost:6060/pkg/github.com/spf13/pflag][2] after
installation.
[2]: http://localhost:6060/pkg/github.com/ogier/pflag
[3]: http://godoc.org/github.com/ogier/pflag
[2]: http://localhost:6060/pkg/github.com/spf13/pflag
[3]: http://godoc.org/github.com/spf13/pflag

View File

@@ -83,7 +83,9 @@ func (f *FlagSet) CountP(name, shorthand string, usage string) *int {
return p
}
// Count like Count only the flag is placed on the CommandLine isntead of a given flag set
// Count defines a count flag with specified name, default value, and usage string.
// The return value is the address of an int variable that stores the value of the flag.
// A count flag will add 1 to its value evey time it is found on the command line
func Count(name string, usage string) *int {
return CommandLine.CountP(name, "", usage)
}

Some files were not shown because too many files have changed in this diff Show More