Compare commits
7 Commits
circleci/m
...
master
Author | SHA1 | Date |
---|---|---|
Adrien Marquès | 5998bd7b29 | |
Adrien Marquès | 27ace57523 | |
Adrien Marquès | a3b513a23b | |
Adrien Marquès | b752906d15 | |
Adrien Marquès | 3331970eb5 | |
Adrien Marquès | bca2b145bc | |
Adrien Marquès | 0059dd90ea |
|
@ -0,0 +1,11 @@
|
|||
---
|
||||
kind: pipeline
|
||||
type: docker
|
||||
name: default
|
||||
|
||||
steps:
|
||||
- name: test
|
||||
image: golang:1.13
|
||||
commands:
|
||||
- go get ./...
|
||||
- go test -v -race -cover -coverprofile ./coverage.out ./...
|
63
README.md
63
README.md
|
@ -2,10 +2,10 @@
|
|||
|
||||
[![Go version](https://img.shields.io/badge/go_version-1.11-blue.svg)](https://golang.org/doc/go1.11)
|
||||
[![License: MIT](https://img.shields.io/github/license/xdrm-brackets/nix-amer.svg)](https://opensource.org/licenses/MIT)
|
||||
[![Go Report Card](https://goreportcard.com/badge/github.com/xdrm-brackets/nix-amer)](https://goreportcard.com/report/github.com/xdrm-brackets/nix-amer)
|
||||
[![Go Report Card](https://goreportcard.com/badge/git.xdrm.io/go/nix-amer)](https://goreportcard.com/report/git.xdrm.io/go/nix-amer)
|
||||
[![Coverage Status](https://img.shields.io/coveralls/github/xdrm-brackets/nix-amer/master.svg)](https://coveralls.io/github/xdrm-brackets/nix-amer?branch=master)
|
||||
[![CircleCI Build Status](https://img.shields.io/circleci/project/github/xdrm-brackets/nix-amer/master.svg)](https://circleci.com/gh/xdrm-brackets/nix-amer)
|
||||
[![Go doc](https://godoc.org/github.com/xdrm-brackets/nix-amer?status.svg)](https://godoc.org/github.com/xdrm-brackets/nix-amer)
|
||||
[![Build Status](https://drone.xdrm.io/api/badges/go/nix-amer/status.svg)](https://drone.xdrm.io/go/nix-amer)
|
||||
[![Go doc](https://godoc.org/git.xdrm.io/go/nix-amer?status.svg)](https://godoc.org/git.xdrm.io/go/nix-amer)
|
||||
|
||||
```yaml
|
||||
name: nix-amer
|
||||
|
@ -25,13 +25,14 @@ author: xdrm-brackets
|
|||
* [1. Create build file](#1-create-build-file)
|
||||
* [2. Run on the target](#2-run-on-the-target)
|
||||
* [II. Commands](#ii-commands)
|
||||
+ [1) Comments](#1-comments)
|
||||
+ [2) Install/remove Packages](#2-installremove-packages)
|
||||
+ [3) Setup configuration](#3-setup-configuration)
|
||||
+ [4) Service management](#4-service-management)
|
||||
+ [5) Custom scripts](#5-custom-scripts)
|
||||
+ [6) Copy files](#6-copy-files)
|
||||
+ [7) Aliases](#7-aliases)
|
||||
+ [1) Sections](#1-sections)
|
||||
+ [2) Comments](#2-comments)
|
||||
+ [3) Install/remove Packages](#3-installremove-packages)
|
||||
+ [4) Setup configuration](#4-setup-configuration)
|
||||
+ [5) Service management](#5-service-management)
|
||||
+ [6) Custom scripts](#6-custom-scripts)
|
||||
+ [7) Copy files](#7-copy-files)
|
||||
+ [8) Aliases](#8-aliases)
|
||||
* [III. Path Expressions](#iii-path-expressions)
|
||||
+ [1) Syntax](#1-syntax)
|
||||
+ [2) File Formats](#2-file-formats)
|
||||
|
@ -59,10 +60,10 @@ In order to install the `nix-amer` executable, you must have :
|
|||
Simply launch the following command in any terminal
|
||||
|
||||
```bash
|
||||
$ go get -u github.com/xdrm-brackets/nix-amer
|
||||
$ go get -u git.xdrm.io/go/nix-amer
|
||||
```
|
||||
|
||||
> For those who don't know, it will load the project sources into `$GOPATH/src/github.com/xdrm-brackets/nix-amer` and compile into the executable at `$GOPATH/bin/nix-amer`.
|
||||
> For those who don't know, it will load the project sources into `$GOPATH/src/git.xdrm.io/go/nix-amer` and compile into the executable at `$GOPATH/bin/nix-amer`.
|
||||
|
||||
|
||||
|
||||
|
@ -70,7 +71,7 @@ $ go get -u github.com/xdrm-brackets/nix-amer
|
|||
|
||||
###### 1. Create build file
|
||||
|
||||
The first step is to write your build file according to the installation you want. While writing your build file you can check the syntax by using the `-dry-run` command-line argument as follows :
|
||||
The first step is to write your build file according to the installation you want. While writing it you can check the syntax and validate instructions by using the `-dry-run` command-line argument as follows :
|
||||
|
||||
```bash
|
||||
$ nix-amer -p apt-get -dry-run <path/to/build/file>
|
||||
|
@ -82,7 +83,7 @@ $ nix-amer -p apt-get -dry-run <path/to/build/file>
|
|||
|
||||
###### 2. Run on the target
|
||||
|
||||
Once your build file is correct and fulfills your needs, you can log in to the target machine, install the nix-amer and run it with your build file. Nix-amer's rich and colorful command-line output will give you a good feedback to rapidly fix problems.
|
||||
Once your build file is correct and fulfills your needs, you can log in to the target machine, install nix-amer and run it with your build file. The rich and colorful command-line output will give you a good feedback to rapidly fix problems.
|
||||
|
||||
|
||||
|
||||
|
@ -94,17 +95,23 @@ Once your build file is correct and fulfills your needs, you can log in to the t
|
|||
|
||||
### II. Commands
|
||||
|
||||
Your whole setup remains in 1 only build file. Each line contains one instruction, the list of instructions is listed below.
|
||||
Your whole setup remains in only one file. Each line contains one instruction, the list of instructions is listed below.
|
||||
|
||||
|
||||
|
||||
#### 1) Comments
|
||||
#### 1) Sections
|
||||
|
||||
Each line beginning with one of the following characters : `[`, `#` or `;` is considered a comment and is not interpreted.
|
||||
Each instruction is enclosed in a section (_cf. ini file format_), a section definition stands on a line where the name of the section is surrounded by `[` and `]`. Each section is executed in parallel ; the special section named `pre` is executed before every other.
|
||||
|
||||
|
||||
|
||||
#### 2) Install/remove Packages
|
||||
#### 2) Comments
|
||||
|
||||
Each line beginning with one of the following characters : `#` or `;` is considered a comment and is not interpreted.
|
||||
|
||||
|
||||
|
||||
#### 3) Install/remove Packages
|
||||
|
||||
These instructions allow you to interact with the package system available on your system.
|
||||
|
||||
|
@ -122,7 +129,7 @@ Remove the listed packages. If more than one, use spaces to separate package nam
|
|||
|
||||
|
||||
|
||||
#### 3) Setup configuration
|
||||
#### 4) Setup configuration
|
||||
|
||||
This instruction allow you to set fields of configuration files without the need of an editor and in a developer-readable manner.
|
||||
|
||||
|
@ -134,7 +141,7 @@ Update a configuration file where \<expr\> is a dot-separated human-readable [pa
|
|||
|
||||
|
||||
|
||||
#### 4) Service management
|
||||
#### 5) Service management
|
||||
|
||||
These instructions allow you to interact with the service system (_cf. [systemd](https://github.com/systemd/systemd)_).
|
||||
|
||||
|
@ -145,7 +152,7 @@ Perform the action on services. If more than one, use spaces to separate service
|
|||
|
||||
|
||||
|
||||
#### 5) Custom scripts
|
||||
#### 6) Custom scripts
|
||||
|
||||
This instruction allows you to use custom scripts for complex operations.
|
||||
|
||||
|
@ -153,9 +160,9 @@ This instruction allows you to use custom scripts for complex operations.
|
|||
run <script>
|
||||
```
|
||||
|
||||
Execute the executable located at the path \<script\>. If script is an [alias](#7-aliases) it will resolve to its path
|
||||
Execute the executable located at the path \<script\>. If script is an [alias](#8-aliases) it will resolve to its path
|
||||
|
||||
#### 6) Copy files
|
||||
#### 7) Copy files
|
||||
|
||||
This instruction allows you to copy files.
|
||||
|
||||
|
@ -167,7 +174,7 @@ Try to copy the file \<src\> to the path \<dst\>.
|
|||
|
||||
|
||||
|
||||
#### 7) Aliases
|
||||
#### 8) Aliases
|
||||
|
||||
The file format allows you to create aliases to file paths for more readability in the [path expression](#ii-path-expressions) or with the [`run` command](#5-custom-scripts).
|
||||
|
||||
|
@ -195,7 +202,7 @@ The syntax is pretty fast-forward, it uses 2 levels (file, fields) to find your
|
|||
|
||||
| Field | Description | Example |
|
||||
| --------- | :----------------------------------- | -------------------------- |
|
||||
| `location_or_alias` | Path to the configuration file to edit. The file will be created if not found. If the path is an [alias](#7-aliases) created before in the file, it will resolve to the alias value as a filename. | `/etc/nginx/nginx.conf`, `some-alias` |
|
||||
| `location_or_alias` | Path to the configuration file to edit. The file will be created if not found. If the path is an [alias](#8-aliases) created before in the file, it will resolve to the alias value as a filename. | `/etc/nginx/nginx.conf`, `some-alias` |
|
||||
| `fields` | Dot-separated chain of strings that match a configuration field. If the field does not point to a raw field but an existing field container, the \<value\> will replace the group with a text value. | `AllowGroups`, `http.gzip` |
|
||||
|
||||
> The `fields` is processed only for known file formats listed in this [section](#2-file-formats).
|
||||
|
@ -212,9 +219,9 @@ Configuration files can be written according to some standards or application-sp
|
|||
|
||||
- [yaml](https://en.wikipedia.org/wiki/YAML) with [go-yaml/yaml](https://github.com/go-yaml/yaml).
|
||||
- [ini](https://en.wikipedia.org/wiki/INI_file) with [go-ini/ini](https://github.com/go-ini/ini).
|
||||
|
||||
- [nginx configurations](https://docs.nginx.com/nginx/admin-guide/basic-functionality/managing-configuration-files/) with [my own library](https://godoc.org/github.com/xdrm-brackets/nix-amer/internal/cnf/parser/nginx).
|
||||
- _and more to come..._
|
||||
- [nginx configurations](https://docs.nginx.com/nginx/admin-guide/basic-functionality/managing-configuration-files/) with [my own library](https://godoc.org/git.xdrm.io/go/nix-amer/internal/cnf/parser/nginx).
|
||||
- [bash sourced configurations]() with [my own library](https://godoc.org/git.xdrm.io/go/nix-amer/internal/cnf/parser/bash) (_e.g. ~/.bashrc_).
|
||||
- _and more to come..._
|
||||
|
||||
|
||||
|
||||
|
|
2
args.go
2
args.go
|
@ -3,7 +3,7 @@ package main
|
|||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/xdrm-brackets/nix-amer/internal/instruction"
|
||||
"git.xdrm.io/go/nix-amer/internal/instruction"
|
||||
)
|
||||
|
||||
// GetArgs manages cli arguments to build executionContext,
|
||||
|
|
44
circle.yml
44
circle.yml
|
@ -1,44 +0,0 @@
|
|||
version: 2
|
||||
jobs:
|
||||
build: # runs not using Workflows must have a `build` job as entry point
|
||||
docker:
|
||||
- image: circleci/golang
|
||||
|
||||
environment: # environment variables for the build itself
|
||||
GOPATH: /go
|
||||
TEST_RESULTS: /tmp/test-results
|
||||
COVER_PROFILE: /tmp/coverage.out
|
||||
|
||||
steps: # steps that comprise the `build` job
|
||||
- checkout # check out source code to working directory
|
||||
- run: mkdir -p $TEST_RESULTS # create the test results directory
|
||||
|
||||
- restore_cache: # restores saved cache if no changes are detected since last run
|
||||
keys:
|
||||
- v1-pkg-cache
|
||||
|
||||
- run:
|
||||
name: Load dependencies
|
||||
command: go get github.com/mattn/goveralls && go get github.com/go-ini/ini && go get gopkg.in/yaml.v2
|
||||
- run:
|
||||
name: Load nix-amer
|
||||
command: go get github.com/xdrm-brackets/nix-amer
|
||||
- run:
|
||||
name: Unit tests
|
||||
command: go test -v -cover -race -coverprofile=$COVER_PROFILE github.com/xdrm-brackets/nix-amer/... | tee $TEST_RESULTS/out
|
||||
- run:
|
||||
name: go fmt
|
||||
command: go fmt github.com/xdrm-brackets/nix-amer/...
|
||||
- run:
|
||||
name: go vet
|
||||
command: go vet github.com/xdrm-brackets/nix-amer/...
|
||||
- run:
|
||||
name: Update coveralls.io
|
||||
command: /go/bin/goveralls -coverprofile=$COVER_PROFILE -service=circle-ci -repotoken=$COVERALLS_TOKEN
|
||||
|
||||
- store_artifacts: # Upload test summary for display in Artifacts
|
||||
path: /tmp/test-results
|
||||
destination: raw-test-output
|
||||
|
||||
- store_test_results: # Upload test results for display in Test Summary
|
||||
path: /tmp/test-results
|
|
@ -0,0 +1,8 @@
|
|||
module git.xdrm.io/go/nix-amer
|
||||
|
||||
go 1.12
|
||||
|
||||
require (
|
||||
github.com/go-ini/ini v1.51.0
|
||||
gopkg.in/yaml.v2 v2.2.5
|
||||
)
|
|
@ -0,0 +1,5 @@
|
|||
github.com/go-ini/ini v1.51.0 h1:VPJKXGzbKlyExUE8f41aV57yxkYx5R49yR6n7flp0M0=
|
||||
github.com/go-ini/ini v1.51.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.5 h1:ymVxjfMaHvXD8RqPRmzHHsB3VvucivSkIAvJFDI5O3c=
|
||||
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
2
help.go
2
help.go
|
@ -2,7 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/xdrm-brackets/nix-amer/internal/clifmt"
|
||||
"git.xdrm.io/go/nix-amer/internal/clifmt"
|
||||
)
|
||||
|
||||
func help() {
|
||||
|
|
|
@ -2,7 +2,7 @@ package buildfile
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/xdrm-brackets/nix-amer/internal/clifmt"
|
||||
"git.xdrm.io/go/nix-amer/internal/clifmt"
|
||||
)
|
||||
|
||||
// LineError wraps errors with a line index
|
||||
|
|
|
@ -4,8 +4,8 @@ import (
|
|||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/xdrm-brackets/nix-amer/internal/clifmt"
|
||||
"github.com/xdrm-brackets/nix-amer/internal/instruction"
|
||||
"git.xdrm.io/go/nix-amer/internal/clifmt"
|
||||
"git.xdrm.io/go/nix-amer/internal/instruction"
|
||||
"io"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
|
|
@ -2,7 +2,7 @@ package buildfile
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/xdrm-brackets/nix-amer/internal/instruction"
|
||||
"git.xdrm.io/go/nix-amer/internal/instruction"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
|
|
@ -0,0 +1,99 @@
|
|||
package cnf
|
||||
|
||||
import (
|
||||
lib "git.xdrm.io/go/nix-amer/internal/cnf/parser/bash"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type bash struct {
|
||||
data *lib.File
|
||||
parsed bool
|
||||
}
|
||||
|
||||
// ReadFrom implements io.ReaderFrom
|
||||
func (d *bash) ReadFrom(_reader io.Reader) (int64, error) {
|
||||
|
||||
d.data = new(lib.File)
|
||||
|
||||
// 1. get bash decoder
|
||||
decoder := lib.NewDecoder(_reader)
|
||||
err := decoder.Decode(d.data)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
d.parsed = true
|
||||
return 0, nil
|
||||
|
||||
}
|
||||
|
||||
// WriteTo implements io.WriterTo
|
||||
func (d *bash) WriteTo(_writer io.Writer) (int64, error) {
|
||||
encoder := lib.NewEncoder(_writer)
|
||||
return 0, encoder.Encode(d.data)
|
||||
}
|
||||
|
||||
// browse returns the target of a dot-separated path (as an interface{} chain where the last is the target if found)
|
||||
// if <create> is true, create if does not exist
|
||||
func (d *bash) browse(_path string, create ...bool) (*lib.Line, bool) {
|
||||
|
||||
mustCreate := len(create) > 0 && create[0]
|
||||
|
||||
// 1. extract path
|
||||
path := strings.Split(_path, ".")
|
||||
field := path[len(path)-1]
|
||||
|
||||
// 2. nothing
|
||||
if len(path) < 1 {
|
||||
return &lib.Line{}, true
|
||||
}
|
||||
|
||||
// 3. iterate over path / nested fields
|
||||
for _, line := range d.data.Lines {
|
||||
if line.Type == lib.ASSIGNMENT && len(line.Components) > 1 && line.Components[1] == field {
|
||||
return line, true
|
||||
}
|
||||
}
|
||||
|
||||
// 4. create assignment
|
||||
if mustCreate {
|
||||
assignment := &lib.Line{
|
||||
Type: lib.ASSIGNMENT,
|
||||
Components: []string{"", field, "", ""},
|
||||
}
|
||||
d.data.Lines = append(d.data.Lines, assignment)
|
||||
return assignment, true
|
||||
}
|
||||
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// Get returns the value of a dot-separated path, and if it exists
|
||||
func (d *bash) Get(_path string) (string, bool) {
|
||||
|
||||
// 1. browse path
|
||||
last, found := d.browse(_path, true)
|
||||
if !found {
|
||||
return "", false
|
||||
}
|
||||
|
||||
// 2. Get last field
|
||||
return last.Components[2], true
|
||||
|
||||
}
|
||||
|
||||
// Set the value of a dot-separated path, and creates it if not found
|
||||
func (d *bash) Set(_path, _value string) bool {
|
||||
|
||||
// 1. browse path
|
||||
last, found := d.browse(_path, true)
|
||||
if !found {
|
||||
return false
|
||||
}
|
||||
|
||||
// 2. Set value
|
||||
last.Components[2] = _value
|
||||
return true
|
||||
|
||||
}
|
|
@ -0,0 +1,220 @@
|
|||
package cnf
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBashGet(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
raw string
|
||||
key string
|
||||
}{
|
||||
{"key=value;\n", "key"},
|
||||
{" \t key=value;\n", "key"},
|
||||
{"key=value; #comment\n", "key"},
|
||||
{"\t key=value; #comment\n", "key"},
|
||||
{"key=value; # comment\n", "key"},
|
||||
{"\t \tkey=value; # comment\n", "key"},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
|
||||
parser := new(bash)
|
||||
reader := bytes.NewBufferString(test.raw)
|
||||
|
||||
// try to extract value
|
||||
_, err := parser.ReadFrom(reader)
|
||||
if err != nil {
|
||||
t.Errorf("[%d] parse error: %s", i, err)
|
||||
continue
|
||||
}
|
||||
|
||||
// extract value
|
||||
value, found := parser.Get(test.key)
|
||||
if !found {
|
||||
t.Errorf("[%d] expected a result, got none", i)
|
||||
continue
|
||||
}
|
||||
|
||||
// check value
|
||||
if value != "value" {
|
||||
t.Errorf("[%d] expected 'value' got '%s'", i, value)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestBashSetPathExists(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
raw string
|
||||
key string
|
||||
value string
|
||||
}{
|
||||
|
||||
{"key=value;\n", "key", "newvalue"},
|
||||
{" \t key=value;\n", "key", "newvalue"},
|
||||
{"key=value; #comment\n", "key", "newvalue"},
|
||||
{"\t key=value; #comment\n", "key", "newvalue"},
|
||||
{"key=value; # comment\n", "key", "newvalue"},
|
||||
{"\t \tkey=value; # comment\n", "key", "newvalue"},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
|
||||
parser := new(bash)
|
||||
reader := bytes.NewBufferString(test.raw)
|
||||
|
||||
// try to extract value
|
||||
_, err := parser.ReadFrom(reader)
|
||||
if err != nil {
|
||||
t.Errorf("[%d] parse error: %s", i, err)
|
||||
continue
|
||||
}
|
||||
|
||||
// update value
|
||||
if !parser.Set(test.key, test.value) {
|
||||
t.Errorf("[%d] cannot set '%s' to '%s'", i, test.key, test.value)
|
||||
continue
|
||||
}
|
||||
|
||||
// check new value
|
||||
value, found := parser.Get(test.key)
|
||||
if !found {
|
||||
t.Errorf("[%d] expected a result, got none", i)
|
||||
continue
|
||||
}
|
||||
|
||||
// check value
|
||||
if value != test.value {
|
||||
t.Errorf("[%d] expected '%s' got '%s'", i, test.value, value)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestBashSetCreatePath(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
raw string
|
||||
key string
|
||||
ignore string // path to field that must be present after transformation
|
||||
value string
|
||||
}{
|
||||
{"ignore=xxx;\n", "key", "ignore", "newvalue"},
|
||||
{"unknown-line\nignore=xxx;\n", "key", "ignore", "newvalue"},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
|
||||
parser := new(bash)
|
||||
reader := bytes.NewBufferString(test.raw)
|
||||
|
||||
// try to extract value
|
||||
_, err := parser.ReadFrom(reader)
|
||||
if err != nil {
|
||||
t.Errorf("[%d] parse error: %s", i, err)
|
||||
continue
|
||||
}
|
||||
|
||||
// update value
|
||||
if !parser.Set(test.key, test.value) {
|
||||
t.Errorf("[%d] cannot set '%s' to '%s'", i, test.key, test.value)
|
||||
continue
|
||||
}
|
||||
|
||||
// check new value
|
||||
value, found := parser.Get(test.key)
|
||||
if !found {
|
||||
t.Errorf("[%d] expected a result, got none", i)
|
||||
continue
|
||||
}
|
||||
|
||||
// check value
|
||||
if value != test.value {
|
||||
t.Errorf("[%d] expected '%s' got '%s'", i, test.value, value)
|
||||
continue
|
||||
}
|
||||
|
||||
// check that ignore field is still there
|
||||
value, found = parser.Get(test.ignore)
|
||||
if !found {
|
||||
t.Errorf("[%d] expected ignore field, got none", i)
|
||||
continue
|
||||
}
|
||||
|
||||
// check value
|
||||
if value != "xxx" {
|
||||
t.Errorf("[%d] expected ignore value to be '%s' got '%s'", i, "xxx", value)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestBashSetCreateEncode(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
raw string
|
||||
key string
|
||||
value string
|
||||
encoded string
|
||||
}{
|
||||
{"ignore=xxx;\n", "key", `"newvalue"`, "ignore=xxx;\nkey=\"newvalue\";\n"},
|
||||
{"#!/bin/bash\n\nfunc(){\n\techo \"something\";\n}\nignore=xxx;\n", "key", `"newvalue"`, "#!/bin/bash\nfunc(){\n\techo \"something\";\n}\nignore=xxx;\nkey=\"newvalue\";\n"},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
|
||||
parser := new(bash)
|
||||
r, w := bytes.NewBufferString(test.raw), new(bytes.Buffer)
|
||||
|
||||
// try to extract value
|
||||
_, err := parser.ReadFrom(r)
|
||||
if err != nil {
|
||||
t.Errorf("[%d] parse error: %s", i, err)
|
||||
continue
|
||||
}
|
||||
|
||||
// update value
|
||||
if !parser.Set(test.key, test.value) {
|
||||
t.Errorf("[%d] cannot set '%s' to '%s'", i, test.key, test.value)
|
||||
continue
|
||||
}
|
||||
|
||||
// check new value
|
||||
value, found := parser.Get(test.key)
|
||||
if !found {
|
||||
t.Errorf("[%d] expected a result, got none", i)
|
||||
continue
|
||||
}
|
||||
|
||||
// check value
|
||||
if value != test.value {
|
||||
t.Errorf("[%d] expected '%s' got '%s'", i, test.value, value)
|
||||
continue
|
||||
}
|
||||
|
||||
// writeToBuffer
|
||||
_, err = parser.WriteTo(w)
|
||||
if err != nil {
|
||||
t.Errorf("[%d] unexpected write error <%s>", i, err)
|
||||
continue
|
||||
}
|
||||
|
||||
encoded := w.String()
|
||||
|
||||
// check value
|
||||
if encoded != test.encoded {
|
||||
t.Errorf("[%d] wrong encoded value \n-=-=-= HAVE =-=-=-\n%s\n-=-=-= WANT =-=-=-\n%s\n-=-=-=-=-=\n", i, escape(test.encoded), escape(encoded))
|
||||
continue
|
||||
}
|
||||
|
||||
// try to write
|
||||
}
|
||||
|
||||
}
|
|
@ -70,6 +70,8 @@ func loadFromExtension(ext string) ConfigurationFormat {
|
|||
return new(yaml)
|
||||
case ".nginx":
|
||||
return new(nginx)
|
||||
case ".sh":
|
||||
return new(bash)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
@ -86,7 +88,7 @@ func loadFromContent(path string) (ConfigurationFormat, error) {
|
|||
defer file.Close()
|
||||
|
||||
// extensions ordered by strictness of the language's syntax
|
||||
extensions := []string{".json", ".nginx", ".ini", ".yaml"}
|
||||
extensions := []string{".json", ".nginx", ".ini", ".yaml", ".sh"}
|
||||
|
||||
// try to load each available extension
|
||||
for _, ext := range extensions {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package cnf
|
||||
|
||||
import (
|
||||
lib "github.com/xdrm-brackets/nix-amer/internal/cnf/parser/nginx"
|
||||
lib "git.xdrm.io/go/nix-amer/internal/cnf/parser/nginx"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
package bash
|
||||
|
||||
import (
|
||||
"io"
|
||||
)
|
||||
|
||||
// File represents a bash file
|
||||
type File struct {
|
||||
Lines []*Line
|
||||
}
|
||||
|
||||
// NewDecoder implements parser.T
|
||||
func NewDecoder(r io.Reader) *Decoder {
|
||||
return &Decoder{reader: r}
|
||||
}
|
||||
|
||||
// NewEncoder implements parser.T
|
||||
func NewEncoder(w io.Writer) *Encoder {
|
||||
return &Encoder{writer: w}
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
package bash
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Decoder implements parser.Decoder
|
||||
type Decoder struct{ reader io.Reader }
|
||||
|
||||
// Decode is the main function of the parser.Decoder interface
|
||||
func (d *Decoder) Decode(v interface{}) error {
|
||||
|
||||
// check 'v'
|
||||
if v == nil {
|
||||
return ErrNullReceiver
|
||||
}
|
||||
vcast, ok := v.(*File)
|
||||
if !ok {
|
||||
return ErrInvalidReceiver
|
||||
}
|
||||
vcast.Lines = make([]*Line, 0)
|
||||
|
||||
r := bufio.NewReader(d.reader)
|
||||
n := -1 // line number
|
||||
|
||||
// regexes
|
||||
reAssign := regexp.MustCompile(`(?m)^(\s*)([A-Za-z0-9_]+)=([^;]+);?\s*(#.+)?$`)
|
||||
eof := false
|
||||
|
||||
for {
|
||||
n++
|
||||
if eof {
|
||||
break
|
||||
}
|
||||
|
||||
l := &Line{Type: ANY}
|
||||
|
||||
// 1. read line
|
||||
line, err := r.ReadString('\n')
|
||||
if err == io.EOF {
|
||||
if len(line) > 0 {
|
||||
eof = true
|
||||
} else {
|
||||
break
|
||||
}
|
||||
} else if err != nil {
|
||||
return &LineError{n, err}
|
||||
}
|
||||
line = strings.Trim(line, "\r\n")
|
||||
|
||||
// 2. ignore empty
|
||||
if len(strings.Trim(line, " \t\r\n")) < 1 {
|
||||
continue
|
||||
}
|
||||
|
||||
// 3. assignment
|
||||
match := reAssign.FindStringSubmatch(line)
|
||||
if match != nil {
|
||||
l.Type = ASSIGNMENT
|
||||
l.Components = match[1:]
|
||||
} else {
|
||||
l.Components = []string{line}
|
||||
}
|
||||
|
||||
// 4. add to file
|
||||
vcast.Lines = append(vcast.Lines, l)
|
||||
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
package bash
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Encoder implements parser.Encoder
|
||||
type Encoder struct {
|
||||
writer io.Writer
|
||||
prefix []byte
|
||||
indent []byte
|
||||
}
|
||||
|
||||
// Encode is the main function of the parser.Encoder interface
|
||||
func (e *Encoder) Encode(v interface{}) error {
|
||||
|
||||
// check 'v'
|
||||
vcast, ok := v.(*File)
|
||||
if !ok {
|
||||
return ErrInvalidReceiver
|
||||
}
|
||||
|
||||
// empty config
|
||||
if len(vcast.Lines) < 1 {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, line := range vcast.Lines {
|
||||
|
||||
// line representation
|
||||
repr := ""
|
||||
|
||||
// ASSIGNMENT
|
||||
if line.Type == ASSIGNMENT {
|
||||
repr = fmt.Sprintf("%s%s=%s;", line.Components[0], line.Components[1], line.Components[2])
|
||||
|
||||
// optional comment
|
||||
if len(line.Components[3]) > 0 {
|
||||
repr += fmt.Sprintf(" %s", line.Components[3])
|
||||
}
|
||||
|
||||
// ANY
|
||||
} else {
|
||||
repr = strings.Join(line.Components, "")
|
||||
}
|
||||
|
||||
repr += "\n"
|
||||
|
||||
_, err := e.writer.Write([]byte(repr))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package bash
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.xdrm.io/go/nix-amer/internal/clifmt"
|
||||
)
|
||||
|
||||
// ErrNullReceiver is raised when a null receiver is provided
|
||||
var ErrNullReceiver = fmt.Errorf("receiver must not be null")
|
||||
|
||||
// ErrInvalidReceiver is raised when an invalid receiver is provided
|
||||
var ErrInvalidReceiver = fmt.Errorf("receiver must be compatible with *File")
|
||||
|
||||
// LineError wraps errors with a line index
|
||||
type LineError struct {
|
||||
Line int
|
||||
Err error
|
||||
}
|
||||
|
||||
// Error implements Error
|
||||
func (le LineError) Error() string {
|
||||
return fmt.Sprintf(":%d %s", le.Line, clifmt.Color(31, le.Err.Error()))
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package bash
|
||||
|
||||
// LineType enumerates available line types
|
||||
type LineType byte
|
||||
|
||||
const (
|
||||
// ANY is the default line type (all except assignments)
|
||||
ANY LineType = iota
|
||||
// ASSIGNMENT line
|
||||
ASSIGNMENT
|
||||
)
|
||||
|
||||
// Line represents a meaningful line
|
||||
type Line struct {
|
||||
// Type of line
|
||||
Type LineType
|
||||
|
||||
// Components of the line
|
||||
//
|
||||
// When Type = ASSIGNMENT :
|
||||
// [0] = indentation (spaces and tabs)
|
||||
// [1] = variable name
|
||||
// [2] = variable value
|
||||
// [3] = comment (optional)
|
||||
Components []string
|
||||
}
|
|
@ -2,7 +2,7 @@ package nginx
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/xdrm-brackets/nix-amer/internal/clifmt"
|
||||
"git.xdrm.io/go/nix-amer/internal/clifmt"
|
||||
)
|
||||
|
||||
// ErrNullReceiver is raised when a null receiver is provided
|
||||
|
|
|
@ -2,9 +2,9 @@ package instruction
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/xdrm-brackets/nix-amer/internal/exec"
|
||||
"github.com/xdrm-brackets/nix-amer/internal/pkg"
|
||||
"github.com/xdrm-brackets/nix-amer/internal/ser"
|
||||
"git.xdrm.io/go/nix-amer/internal/exec"
|
||||
"git.xdrm.io/go/nix-amer/internal/pkg"
|
||||
"git.xdrm.io/go/nix-amer/internal/ser"
|
||||
)
|
||||
|
||||
// T is the instruction common interface
|
||||
|
|
|
@ -2,7 +2,7 @@ package instruction
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/xdrm-brackets/nix-amer/internal/cnf"
|
||||
"git.xdrm.io/go/nix-amer/internal/cnf"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package pkg
|
||||
|
||||
import "github.com/xdrm-brackets/nix-amer/internal/exec"
|
||||
import "git.xdrm.io/go/nix-amer/internal/exec"
|
||||
|
||||
type apk struct{ exec exec.Executor }
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package pkg
|
||||
|
||||
import (
|
||||
"github.com/xdrm-brackets/nix-amer/internal/exec"
|
||||
"git.xdrm.io/go/nix-amer/internal/exec"
|
||||
)
|
||||
|
||||
type aptGet struct{ exec exec.Executor }
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package pkg
|
||||
|
||||
import (
|
||||
"github.com/xdrm-brackets/nix-amer/internal/exec"
|
||||
"git.xdrm.io/go/nix-amer/internal/exec"
|
||||
)
|
||||
|
||||
// DefaultManager if not empty is the default package-manager to use when missing
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package pkg
|
||||
|
||||
import "github.com/xdrm-brackets/nix-amer/internal/exec"
|
||||
import "git.xdrm.io/go/nix-amer/internal/exec"
|
||||
|
||||
type dnf struct{ exec exec.Executor }
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package pkg
|
||||
|
||||
import "github.com/xdrm-brackets/nix-amer/internal/exec"
|
||||
import "git.xdrm.io/go/nix-amer/internal/exec"
|
||||
|
||||
type eopkg struct{ exec exec.Executor }
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ package pkg
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/xdrm-brackets/nix-amer/internal/exec"
|
||||
"git.xdrm.io/go/nix-amer/internal/exec"
|
||||
)
|
||||
|
||||
// ErrUnknownManager is raised when the asked manager does not exist
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package pkg
|
||||
|
||||
import (
|
||||
"github.com/xdrm-brackets/nix-amer/internal/exec"
|
||||
"git.xdrm.io/go/nix-amer/internal/exec"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package pkg
|
||||
|
||||
import "github.com/xdrm-brackets/nix-amer/internal/exec"
|
||||
import "git.xdrm.io/go/nix-amer/internal/exec"
|
||||
|
||||
type pacman struct{ exec exec.Executor }
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package pkg
|
||||
|
||||
import "github.com/xdrm-brackets/nix-amer/internal/exec"
|
||||
import "git.xdrm.io/go/nix-amer/internal/exec"
|
||||
|
||||
type yum struct{ exec exec.Executor }
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package ser
|
||||
|
||||
import (
|
||||
"github.com/xdrm-brackets/nix-amer/internal/exec"
|
||||
"git.xdrm.io/go/nix-amer/internal/exec"
|
||||
)
|
||||
|
||||
// DefaultManager if not empty is the default service-manager to use when missing
|
||||
|
|
|
@ -2,7 +2,7 @@ package ser
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/xdrm-brackets/nix-amer/internal/exec"
|
||||
"git.xdrm.io/go/nix-amer/internal/exec"
|
||||
)
|
||||
|
||||
// ErrUnknownManager is raised when the asked manager does not exist
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package ser
|
||||
|
||||
import (
|
||||
"github.com/xdrm-brackets/nix-amer/internal/exec"
|
||||
"git.xdrm.io/go/nix-amer/internal/exec"
|
||||
)
|
||||
|
||||
type systemd struct{ exec exec.Executor }
|
||||
|
|
Loading…
Reference in New Issue