Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo Marin committed Mar 7, 2020
0 parents commit b77e72a
Show file tree
Hide file tree
Showing 15 changed files with 739 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: goreleaser

on:
create:
tags:
push:
tags:
- '*'

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Unshallow
run: git fetch --prune --unshallow
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: 1.14.x
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v1
with:
version: latest
args: release --rm-dist
key: ${{ secrets.YOUR_PRIVATE_KEY }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Run GoReleaser
if: steps.semantic.outputs.new_release_published == 'true'
uses: ./.github/actions/goreleaser
18 changes: 18 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
on: [push, pull_request]
name: Test
jobs:
test:
strategy:
matrix:
go-version: [1.13.x, 1.14.x]
platform: [ubuntu-latest]
runs-on: ${{ matrix.platform }}
steps:
- name: Install Go
uses: actions/setup-go@v1
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- name: Test
run: go test ./...
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
.vscode
dist
testing*
47 changes: 47 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# This is an example goreleaser.yaml file with some sane defaults.
# Make sure to check the documentation at http://goreleaser.com
project_name: env-replacer
builds:
- id: env-replacer
binary: env-replacer
main: cmd/main.go
env:
- GO111MODULE=on
- CGO_ENABLED=0
goos:
- linux
- darwin
- windows
goarch:
- amd64

changelog:
sort: desc

archives:
- format_overrides:
- goos: windows
format: zip
replacements:
amd64: 64-bit
darwin: macOS

before:
hooks:
- go mod tidy

release:
github:
owner: guumaster
name: env-replacer

nfpms:
- id: env-replacer
builds:
- env-replacer
formats:
- deb
vendor: Guumaster
homepage: https://github.com/guumaster/env-replacer
maintainer: guumaster <guuweb@gmail.com>
description: remove forgotten folders quick and easy
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) [2020] [Gustavo Marin]

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.
75 changes: 75 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
[![Tests](https://img.shields.io/github/workflow/status/guumaster/env-replacer/Test)](https://github.com/guumaster/env-replacer/actions?query=workflow%3ATest)
[![GitHub Release](https://img.shields.io/github/release/guumaster/env-replacer.svg?logo=github&labelColor=262b30)](https://github.com/guumaster/env-replacer/releases)
[![Go Report Card](https://goreportcard.com/badge/github.com/guumaster/env-replacer)](https://goreportcard.com/report/github.com/guumaster/env-replacer)
[![License](https://img.shields.io/github/license/guumaster/env-replacer)](https://github.com/guumaster/env-replacer/LICENSE)
# Env Replacer

A powerful tool to replace environment variables on template files.

## Why?

I don't like to maintain `bash` scripts, I don't like `gettext` and `envsubst`, so this is a replacement.


## Installation

Go to [release page](https://github.com/guumaster/env-replacer/releases) and download the binary you need.


## Examples

```bash
$> env-replacer --path /some-path-to-tpl-files
```


## Usage

NAME:
env-replacer - replace env variables on files

USAGE:
env-replacer [global options] [arguments...]

VERSION:
dev

AUTHOR:
guumaster <guuweb@gmail.com>

GLOBAL OPTIONS:
--path value path to scan for templates

--files value file pattern to search (default: "*.tpl")

--quiet, -q hide report (default: false)

--no-recursive don't scan path recursively (default: false)

--no-truncate don't truncate dst files (default: false)

--no-error early quit on error (default: false)

--no-missing don't write files with missing variables (default: false)

--no-empty fail also with variables (default: false)

--strict same as --no-empty and --no-missing combined (default: false)

--version print the version (default: false)


### Dependencies & Refs
- [bmatcuk/doublestar](https://github.com/bmatcuk/doublestar)
- [urfave/cli](https://github.com/urfave/cli)


### LICENSE

[MIT license](LICENSE)


### Author(s)

* [guumaster](https://github.com/guumaster)

28 changes: 28 additions & 0 deletions cmd/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
NAME:
env-replacer - replace env variables on files
USAGE:
env-replacer [global options] [arguments...]
VERSION:
dev
AUTHOR:
guumaster <guuweb@gmail.com>
GLOBAL OPTIONS:
--path value path to scan for templates
--files value file pattern to search (default: "*.tpl")
--quiet, -q hide report (default: false)
--no-recursive don't scan path recursively (default: false)
--no-truncate don't truncate dst files (default: false)
--no-error early quit on error (default: false)
--no-missing don't write files with missing variables (default: false)
--no-empty fail also with variables (default: false)
--strict same as --no-empty and --no-missing combined (default: false)
--version print the version (default: false)
*/
package main
120 changes: 120 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Package main contains code tu run env-replacer as a CLI command.
package main

import (
"log"
"os"
"regexp"

"github.com/urfave/cli/v2"

"github.com/guumaster/env-replacer/pkg/replacer"
)

var version = "dev"

var reTrailingSlash = regexp.MustCompile(`/?$`)

func main() {
cmd := buildCLI(&replacer.App{})
if err := cmd.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func buildCLI(app *replacer.App) *cli.App {

cli.VersionFlag = &cli.BoolFlag{
Name: "version",
Usage: "print the version",
}

return &cli.App{
Name: "env-replacer",
HelpName: "env-replacer",
Usage: "replace env variables on files",
Version: version,
Description: "",
HideHelp: true,
Flags: []cli.Flag{
&cli.StringFlag{Name: "path", Value: "", Usage: "path to scan for templates"},
&cli.StringFlag{Name: "files", Value: "*.tpl", Usage: "file pattern to search"},
&cli.BoolFlag{Name: "quiet", Value: false, Usage: "hide report", Aliases: []string{"q"}},
&cli.BoolFlag{Name: "no-recursive", Value: false, Usage: "don't scan path recursively"},
&cli.BoolFlag{Name: "no-truncate", Value: false, Usage: "don't truncate dst files"},
&cli.BoolFlag{Name: "no-error", Value: false, Usage: "early quit on error"},
&cli.BoolFlag{Name: "no-missing", Value: false, Usage: "don't write files with missing variables"},
&cli.BoolFlag{Name: "no-empty", Value: false, Usage: "fail also with variables"},
&cli.BoolFlag{Name: "strict", Value: false, Usage: "same as --no-empty and --no-missing combined"},
},
Authors: []*cli.Author{
{
Name: "guumaster",
Email: "guuweb@gmail.com",
},
},
Before: func(c *cli.Context) error {
if c.Bool("version") {
cli.ShowVersion(c)
return cli.Exit("", 0)
}
return nil
},
Action: func(c *cli.Context) error {
if isPiped() {
rep := replacer.New(replacer.WithPipedOptions())
_, err := rep.Process(os.Stdin, os.Stdout)
if err != nil {
return cli.Exit(err.Error(), 1)
}

return nil
}

cwd, err := os.Getwd()
if err != nil {
return cli.Exit("can't read current directory", 1)
}

if c.NArg() > 0 {
return cli.Exit("wrong number of arguments. Put flags before path.", 1)
}

basePath := c.String("path")
if basePath == "" {
basePath = cwd
}
basePath = reTrailingSlash.ReplaceAllLiteralString(basePath, "")
if _, err := os.Stat(basePath); os.IsNotExist(err) {
return cli.Exit("path must be set to an existing directory", 1)
}

reporter, err := app.Run(&replacer.Options{
BasePath: basePath,
FilePattern: c.String("files"),
ShowReport: !c.Bool("quiet"),
ScanRecursive: !c.Bool("no-recursive"),
TruncateFiles: !c.Bool("no-truncate"),
StopOnErrors: c.Bool("no-error"),
StopOnMissing: c.Bool("no-missing"),
StopOnEmpty: c.Bool("no-empty"),
Strict: c.Bool("strict"),
})
if err != nil {
return err
}

reporter.Print()

return nil
},
}
}

func isPiped() bool {
info, err := os.Stdin.Stat()
if err != nil {
panic(err)
}
notPipe := info.Mode()&os.ModeNamedPipe == 0
return !notPipe
}
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/guumaster/env-replacer

go 1.14

require (
github.com/bmatcuk/doublestar v1.2.2
github.com/urfave/cli/v2 v2.1.1
)
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/bmatcuk/doublestar v1.2.2 h1:oC24CykoSAB8zd7XgruHo33E0cHJf/WhQA/7BeXj+x0=
github.com/bmatcuk/doublestar v1.2.2/go.mod h1:wiQtGV+rzVYxB7WIlirSN++5HPtPlXEo9MEoZQC/PmE=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/urfave/cli/v2 v2.1.1 h1:Qt8FeAtxE/vfdrLmR3rxR6JRE0RoVmbXu8+6kZtYU4k=
github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Loading

0 comments on commit b77e72a

Please sign in to comment.