Skip to content

Commit

Permalink
Merge pull request #107 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 6.0.0
  • Loading branch information
andyone authored Aug 9, 2022
2 parents 810c40b + 64c0d37 commit 0ef97de
Show file tree
Hide file tree
Showing 29 changed files with 914 additions and 335 deletions.
5 changes: 1 addition & 4 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ _Before opening an issue, search for similar bug reports or feature requests on

**System info:**

* **Version used (`--version`):**
* **OS (e.g. from `/etc/*-release`):**
* **Kernel (`uname -a`):**
* **Go version (`go version`):**
* **Verbose app info (`bibop -vv`):**
* **Install tools:**

**Steps to reproduce:**
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ jobs:
uses: actions/setup-go@v3
with:
go-version: '1.17.x'
id: go

- name: Checkout
uses: actions/checkout@v3
Expand Down
13 changes: 10 additions & 3 deletions COOKBOOK.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ delay 1.5

#### `command`

Executes command. If you want to do some actions and checks without executing any command or binary, you can use "-" (_minus_) as a command name.
Executes command. If you want to do some actions and checks without executing any binary (_"hollow" command_), you can use "-" (_minus_) as a command name.

You can execute the command as another user. For using this feature, you should define user name at the start of the command, e.g. `nobody:echo 'ABCD'`. This feature requires that `bibop` utility was executed with super user privileges (e.g. `root`).

Expand All @@ -320,6 +320,13 @@ command "echo 'ABCD'" "Simple echo command"
```

```yang
command "USER=john ID=123 echo 'ABCD'" "Simple echo command with enviroment variables"
expect "ABCD"
exit 0
```

```yang
command "postgres:echo 'ABCD'" "Simple echo command as postgres user"
expect "ABCD"
Expand All @@ -328,15 +335,15 @@ command "postgres:echo 'ABCD'" "Simple echo command as postgres user"
```

```yang
command "-" "Check configuration files"
command "-" "Check configuration files (hollow command)"
exist "/etc/myapp.conf"
owner "/etc/myapp.conf" "root"
mode "/etc/myapp.conf" 644
```

```yang
command:init "my app initdb" "Init database"
command:init "myapp initdb" "Init database"
exist "/var/db/myapp.db"
```
Expand Down
19 changes: 13 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
################################################################################

# This Makefile generated by GoMakeGen 2.0.0 using next command:
# This Makefile generated by GoMakeGen 2.1.0 using next command:
# gomakegen --mod .
#
# More info: https://kaos.sh/gomakegen
Expand All @@ -13,6 +13,9 @@ ifdef VERBOSE ## Print verbose information (Flag)
VERBOSE_FLAG = -v
endif

MAKEDIR = $(dir $(realpath $(firstword $(MAKEFILE_LIST))))
GITREV ?= $(shell test -s $(MAKEDIR)/.git && git rev-parse --short HEAD)

################################################################################

.DEFAULT_GOAL := help
Expand All @@ -23,7 +26,7 @@ endif
all: bibop ## Build all binaries

bibop:
go build $(VERBOSE_FLAG) bibop.go
go build $(VERBOSE_FLAG) -ldflags="-X main.gitrev=$(GITREV)" bibop.go

install: ## Install all binaries
cp bibop /usr/bin/bibop
Expand All @@ -40,7 +43,11 @@ update: mod-update ## Update dependencies to the latest versions
vendor: mod-vendor ## Make vendored copy of dependencies

test: ## Run tests
go test $(VERBOSE_FLAG) -covermode=count ./parser ./recipe
ifdef COVERAGE_FILE ## Save coverage data into file (String)
go test $(VERBOSE_FLAG) -covermode=count -coverprofile=$(COVERAGE_FILE) ./parser ./recipe
else
go test $(VERBOSE_FLAG) -covermode=count ./parser ./recipe
endif

gen-fuzz: ## Generate archives for fuzz testing
which go-fuzz-build &>/dev/null || go get -u -v github.com/dvyukov/go-fuzz/go-fuzz-build
Expand Down Expand Up @@ -72,13 +79,13 @@ else
go mod tidy $(VERBOSE_FLAG)
endif

test -d vendor && go mod vendor $(VERBOSE_FLAG) || :
test -d vendor && rm -rf vendor && go mod vendor $(VERBOSE_FLAG) || :

mod-download:
go mod download

mod-vendor:
go mod vendor $(VERBOSE_FLAG)
rm -rf vendor && go mod vendor $(VERBOSE_FLAG)

fmt: ## Format source code with gofmt
find . -name "*.go" -exec gofmt -s -w {} \;
Expand All @@ -98,6 +105,6 @@ help: ## Show this info
| sed 's/ifdef //' \
| awk 'BEGIN {FS = " .*?## "}; {printf " \033[32m%-14s\033[0m %s\n", $$1, $$2}'
@echo -e ''
@echo -e '\033[90mGenerated by GoMakeGen 2.0.0\033[0m\n'
@echo -e '\033[90mGenerated by GoMakeGen 2.1.0\033[0m\n'

################################################################################
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Information about bibop recipe syntax you can find in our [cookbook](COOKBOOK.md

### Usage demo

[![demo](https://gh.kaos.st/bibop-510.gif)](#usage-demo)
[![demo](https://gh.kaos.st/bibop-600.gif)](#usage-demo)

### Installation

Expand Down Expand Up @@ -99,7 +99,7 @@ Options
--dry-run, -D Parse and validate recipe
--list-packages, -L List required packages
--format, -f format Output format (tap|json|xml)
--format, -f format Output format (tap13|tap14|json|xml)
--dir, -d dir Path to working directory
--path, -p path Path to directory with binaries
--error-dir, -e dir Path to directory for errors data
Expand All @@ -124,7 +124,6 @@ Examples
bibop app.recipe --format json 1> ~/results/app.json
Run tests from app.recipe and save result in JSON format
```

### Build Status
Expand Down
85 changes: 85 additions & 0 deletions action/auxi.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,94 @@ package action
// ////////////////////////////////////////////////////////////////////////////////// //

import (
"bytes"
"path/filepath"
"regexp"
"strings"

"github.com/essentialkaos/bibop/recipe"
)

// ////////////////////////////////////////////////////////////////////////////////// //

// Handler is action handler function
type Handler func(action *recipe.Action) error

// Store it is storage for stdout and stderr data
type OutputContainer struct {
buf *bytes.Buffer
size int
}

// ////////////////////////////////////////////////////////////////////////////////// //

// escapeCharRegex is regexp for searching escape characters
var escapeCharRegex = regexp.MustCompile(`\x1b\[[0-9\;]+m`)

// ////////////////////////////////////////////////////////////////////////////////// //

func NewOutputContainer(size int) *OutputContainer {
return &OutputContainer{size: size}
}

// ////////////////////////////////////////////////////////////////////////////////// //

// Write writes data into buffer
func (c *OutputContainer) Write(data []byte) {
if c == nil || len(data) == 0 {
return
}

if c.buf == nil {
c.buf = bytes.NewBuffer(nil)
}

dataLen := len(data)

if dataLen >= c.size {
c.buf.Reset()
data = data[len(data)-c.size:]
}

if c.buf.Len()+dataLen > c.size {
c.buf.Next(c.buf.Len() + dataLen - c.size)
}

c.buf.Write(sanitizeData(data))
}

// Bytes returns data as a byte slice
func (c *OutputContainer) Bytes() []byte {
if c == nil || c.buf == nil {
return []byte{}
}

return c.buf.Bytes()
}

// String return data as a string
func (c *OutputContainer) String() string {
if c == nil || c.buf == nil {
return ""
}

return c.buf.String()
}

// IsEmpty returns true if container is empty
func (c *OutputContainer) IsEmpty() bool {
return c == nil || c.buf == nil || c.buf.Len() == 0
}

// Purge clears data
func (c *OutputContainer) Purge() {
if c == nil || c.buf != nil {
c.buf.Reset()
}
}

// ////////////////////////////////////////////////////////////////////////////////// //

// checkPathSafety return true if path is save
func checkPathSafety(r *recipe.Recipe, path string) (bool, error) {
if r.UnsafeActions {
Expand Down Expand Up @@ -45,3 +125,8 @@ func fmtValue(v string) string {

return v
}

// sanitizeData removes escape characters
func sanitizeData(data []byte) []byte {
return escapeCharRegex.ReplaceAll(data, nil)
}
16 changes: 8 additions & 8 deletions action/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ func Backup(action *recipe.Action, tmpDir string) error {
return fmt.Errorf("Can't backup file: %v", err)
}

err = fsutil.CopyAttr(path, tmpDir+"/"+pathCRC32)

if err != nil {
return fmt.Errorf("Can't copy attributes: %v", err)
}

return nil
}

Expand Down Expand Up @@ -77,12 +83,6 @@ func BackupRestore(action *recipe.Action, tmpDir string) error {
return fmt.Errorf("Backup file for %s does not exist", path)
}

ownerUID, ownerGID, err := fsutil.GetOwner(path)

if err != nil {
return fmt.Errorf("Can't get file owner info: %v", err)
}

err = os.Remove(path)

if err != nil {
Expand All @@ -95,10 +95,10 @@ func BackupRestore(action *recipe.Action, tmpDir string) error {
return fmt.Errorf("Can't copy backup file: %v", err)
}

err = os.Chown(path, ownerUID, ownerGID)
err = fsutil.CopyAttr(backupFile, path)

if err != nil {
return fmt.Errorf("Can't restore owner info: %v", err)
return fmt.Errorf("Can't copy attributes: %v", err)
}

return nil
Expand Down
5 changes: 0 additions & 5 deletions action/common.go → action/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ import (

// ////////////////////////////////////////////////////////////////////////////////// //

// Handler is action handler function
type Handler func(action *recipe.Action) error

// ////////////////////////////////////////////////////////////////////////////////// //

// Wait is action processor for "exit"
func Wait(action *recipe.Action) error {
durSec, err := action.GetF(0)
Expand Down
22 changes: 11 additions & 11 deletions action/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ func FileContains(action *recipe.Action) error {
}

if !isSafePath {
return fmt.Errorf("Path \"%s\" is unsafe", file)
return fmt.Errorf("Path %q is unsafe", file)
}

substr, err := action.GetS(1)
Expand All @@ -395,9 +395,9 @@ func FileContains(action *recipe.Action) error {

switch {
case !action.Negative && !bytes.Contains(data, []byte(substr)):
return fmt.Errorf("File %s doesn't contain substring \"%s\"", file, substr)
return fmt.Errorf("File %s doesn't contain substring %q", file, substr)
case action.Negative && bytes.Contains(data, []byte(substr)):
return fmt.Errorf("File %s contains substring \"%s\"", file, substr)
return fmt.Errorf("File %s contains substring %q", file, substr)
}

return nil
Expand Down Expand Up @@ -504,7 +504,7 @@ func Touch(action *recipe.Action) error {
}

if !isSafePath {
return fmt.Errorf("Path \"%s\" is unsafe", file)
return fmt.Errorf("Path %q is unsafe", file)
}

err = ioutil.WriteFile(file, []byte(""), 0644)
Expand All @@ -531,7 +531,7 @@ func Mkdir(action *recipe.Action) error {
}

if !isSafePath {
return fmt.Errorf("Path \"%s\" is unsafe", dir)
return fmt.Errorf("Path %q is unsafe", dir)
}

err = os.MkdirAll(dir, 0755)
Expand All @@ -558,7 +558,7 @@ func Remove(action *recipe.Action) error {
}

if !isSafePath {
return fmt.Errorf("Path \"%s\" is unsafe", target)
return fmt.Errorf("Path %q is unsafe", target)
}

err = os.RemoveAll(target)
Expand All @@ -585,11 +585,11 @@ func Cleanup(action *recipe.Action) error {
}

if !isSafePath {
return fmt.Errorf("Path \"%s\" is unsafe", target)
return fmt.Errorf("Path %q is unsafe", target)
}

if !fsutil.IsDir(target) {
return fmt.Errorf("Target object \"%s\" is not a directory", target)
return fmt.Errorf("Target object %q is not a directory", target)
}

if fsutil.IsEmptyDir(target) {
Expand All @@ -603,7 +603,7 @@ func Cleanup(action *recipe.Action) error {
err = os.RemoveAll(obj)

if err != nil {
return fmt.Errorf("Can't remove object \"%s\": %v", err)
return fmt.Errorf("Can't remove object %q: %v", err)
}
}

Expand Down Expand Up @@ -637,7 +637,7 @@ func Chmod(action *recipe.Action) error {
}

if !isSafePath {
return fmt.Errorf("Path \"%s\" is unsafe", target)
return fmt.Errorf("Path %q is unsafe", target)
}

err = os.Chmod(target, os.FileMode(mode))
Expand All @@ -664,7 +664,7 @@ func Truncate(action *recipe.Action) error {
}

if !isSafePath {
return fmt.Errorf("Path \"%s\" is unsafe", target)
return fmt.Errorf("Path %q is unsafe", target)
}

return os.Truncate(target, 0)
Expand Down
Loading

0 comments on commit 0ef97de

Please sign in to comment.