forked from mirror/pkger
remove post-processing library hepa; pending revision
This commit is contained in:
parent
fdcec3a4ea
commit
143afe6a94
|
@ -9,8 +9,6 @@ import (
|
|||
"path/filepath"
|
||||
|
||||
"github.com/markbates/pkger"
|
||||
"github.com/markbates/pkger/internal/takeon/github.com/markbates/hepa"
|
||||
"github.com/markbates/pkger/internal/takeon/github.com/markbates/hepa/filters"
|
||||
"github.com/markbates/pkger/parser"
|
||||
)
|
||||
|
||||
|
@ -71,15 +69,7 @@ func (e *listCmd) Exec(args []string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
hep := hepa.New()
|
||||
hep = hepa.With(hep, filters.Home())
|
||||
hep = hepa.With(hep, filters.Golang())
|
||||
|
||||
b, err := hep.Filter(bb.Bytes())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = os.Stdout.Write(b)
|
||||
_, err = os.Stdout.Write(bb.Bytes())
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
@ -1,21 +0,0 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2019 Mark Bates
|
||||
|
||||
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.
|
|
@ -1,35 +0,0 @@
|
|||
package hepa
|
||||
|
||||
import "bytes"
|
||||
|
||||
type Filter interface {
|
||||
Filter([]byte) ([]byte, error)
|
||||
}
|
||||
|
||||
type FilterFunc func([]byte) ([]byte, error)
|
||||
|
||||
func (f FilterFunc) Filter(b []byte) ([]byte, error) {
|
||||
return f(b)
|
||||
}
|
||||
|
||||
func Rinse(p Purifier, s, r []byte) Purifier {
|
||||
return WithFunc(p, func(b []byte) ([]byte, error) {
|
||||
b = bytes.ReplaceAll(b, s, r)
|
||||
return b, nil
|
||||
})
|
||||
}
|
||||
|
||||
func Clean(p Purifier, s []byte) Purifier {
|
||||
return WithFunc(p, func(b []byte) ([]byte, error) {
|
||||
if bytes.Contains(b, s) {
|
||||
return []byte{}, nil
|
||||
}
|
||||
return b, nil
|
||||
})
|
||||
}
|
||||
|
||||
func Noop() FilterFunc {
|
||||
return func(b []byte) ([]byte, error) {
|
||||
return b, nil
|
||||
}
|
||||
}
|
|
@ -1,42 +0,0 @@
|
|||
package filters
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var env = func() map[string]string {
|
||||
m := map[string]string{}
|
||||
|
||||
for _, line := range os.Environ() {
|
||||
kv := strings.Split(line, "=")
|
||||
|
||||
k, v := kv[0], kv[1]
|
||||
kt, vt := strings.TrimSpace(k), strings.TrimSpace(v)
|
||||
|
||||
if len(kt) == 0 || len(vt) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
switch k {
|
||||
case "GO111MODULE":
|
||||
continue
|
||||
}
|
||||
|
||||
switch v {
|
||||
case "true", "TRUE", "false", "FALSE", "null", "nil", "NULL":
|
||||
continue
|
||||
}
|
||||
|
||||
if _, err := strconv.Atoi(k); err == nil {
|
||||
continue
|
||||
}
|
||||
if _, err := strconv.Atoi(v); err == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
m[k] = v
|
||||
}
|
||||
return m
|
||||
}()
|
|
@ -1,12 +0,0 @@
|
|||
package filters
|
||||
|
||||
type FilterFunc func([]byte) ([]byte, error)
|
||||
|
||||
func (f FilterFunc) Filter(b []byte) ([]byte, error) {
|
||||
return f(b)
|
||||
}
|
||||
|
||||
type dir struct {
|
||||
Dir string
|
||||
Err error
|
||||
}
|
|
@ -1,51 +0,0 @@
|
|||
package filters
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func Golang() FilterFunc {
|
||||
return func(b []byte) ([]byte, error) {
|
||||
gp, err := gopath(home)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
b = bytes.ReplaceAll(b, []byte(gp.Dir), []byte("$GOPATH"))
|
||||
|
||||
gru, err := goroot(gp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b = bytes.ReplaceAll(b, []byte(gru.Dir), []byte("$GOROOT"))
|
||||
return b, nil
|
||||
}
|
||||
}
|
||||
|
||||
func goroot(gp dir) (dir, error) {
|
||||
gru, ok := os.LookupEnv("GOROOT")
|
||||
if !ok {
|
||||
if gp.Err != nil {
|
||||
return gp, gp.Err
|
||||
}
|
||||
gru = filepath.Join(string(gp.Dir), "go")
|
||||
}
|
||||
return dir{
|
||||
Dir: gru,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func gopath(home dir) (dir, error) {
|
||||
gp, ok := os.LookupEnv("GOPATH")
|
||||
if !ok {
|
||||
if home.Err != nil {
|
||||
return home, home.Err
|
||||
}
|
||||
gp = filepath.Join(string(home.Dir), "go")
|
||||
}
|
||||
return dir{
|
||||
Dir: gp,
|
||||
}, nil
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
package filters
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
)
|
||||
|
||||
var home = func() dir {
|
||||
var d dir
|
||||
home, ok := os.LookupEnv("HOME")
|
||||
if !ok {
|
||||
pwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
d.Err = err
|
||||
return d
|
||||
}
|
||||
home = pwd
|
||||
}
|
||||
d.Dir = home
|
||||
|
||||
return d
|
||||
}()
|
||||
|
||||
func Home() FilterFunc {
|
||||
return func(b []byte) ([]byte, error) {
|
||||
if home.Err != nil {
|
||||
return b, home.Err
|
||||
}
|
||||
return bytes.ReplaceAll(b, []byte(home.Dir), []byte("$HOME")), nil
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -1,29 +0,0 @@
|
|||
package filters
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Secrets() FilterFunc {
|
||||
return func(b []byte) ([]byte, error) {
|
||||
for k, v := range env {
|
||||
for _, s := range secretSuffixes {
|
||||
if !strings.HasSuffix(k, s) {
|
||||
continue
|
||||
}
|
||||
b = bytes.ReplaceAll(b, []byte(v), []byte(mask()))
|
||||
break
|
||||
}
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
}
|
||||
|
||||
var secretSuffixes = []string{
|
||||
"_KEY",
|
||||
"_SECRET",
|
||||
"_TOKEN",
|
||||
"_PASSWORD",
|
||||
"_PASS",
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package hepa
|
||||
|
||||
func WithFunc(p Purifier, fn FilterFunc) Purifier {
|
||||
c := New()
|
||||
c.parent = &p
|
||||
c.filter = fn
|
||||
return c
|
||||
}
|
||||
|
||||
func With(p Purifier, f Filter) Purifier {
|
||||
c := New()
|
||||
c.parent = &p
|
||||
c.filter = f
|
||||
return c
|
||||
}
|
|
@ -1,67 +0,0 @@
|
|||
package hepa
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"io"
|
||||
|
||||
"github.com/markbates/pkger/internal/takeon/github.com/markbates/hepa/filters"
|
||||
)
|
||||
|
||||
type Purifier struct {
|
||||
parent *Purifier
|
||||
filter Filter
|
||||
}
|
||||
|
||||
func (p Purifier) Filter(b []byte) ([]byte, error) {
|
||||
if p.filter == nil {
|
||||
p.filter = filters.Home()
|
||||
}
|
||||
b, err := p.filter.Filter(b)
|
||||
if err != nil {
|
||||
return b, err
|
||||
}
|
||||
if p.parent != nil {
|
||||
return p.parent.Filter(b)
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func (p Purifier) Clean(r io.Reader) ([]byte, error) {
|
||||
bb := &bytes.Buffer{}
|
||||
|
||||
if p.filter == nil {
|
||||
if p.parent != nil {
|
||||
return p.parent.Clean(r)
|
||||
}
|
||||
_, err := io.Copy(bb, r)
|
||||
return bb.Bytes(), err
|
||||
}
|
||||
|
||||
home := filters.Home()
|
||||
reader := bufio.NewReader(r)
|
||||
for {
|
||||
input, _, err := reader.ReadLine()
|
||||
if err != nil && err == io.EOF {
|
||||
break
|
||||
}
|
||||
input, err = p.Filter(input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
input, err = home(input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bb.Write(input)
|
||||
// if len(input) > 0 {
|
||||
bb.Write([]byte("\n"))
|
||||
// }
|
||||
}
|
||||
|
||||
return bb.Bytes(), nil
|
||||
}
|
||||
|
||||
func New() Purifier {
|
||||
return Purifier{}
|
||||
}
|
|
@ -1,4 +0,0 @@
|
|||
package hepa
|
||||
|
||||
// Version of hepa
|
||||
const Version = "v0.0.1"
|
|
@ -7,8 +7,6 @@ import (
|
|||
"io"
|
||||
|
||||
"github.com/markbates/pkger/here"
|
||||
"github.com/markbates/pkger/internal/takeon/github.com/markbates/hepa"
|
||||
"github.com/markbates/pkger/internal/takeon/github.com/markbates/hepa/filters"
|
||||
)
|
||||
|
||||
func Decode(src []byte) ([]byte, error) {
|
||||
|
@ -31,15 +29,6 @@ func Decode(src []byte) ([]byte, error) {
|
|||
}
|
||||
|
||||
func Encode(b []byte) ([]byte, error) {
|
||||
hep := hepa.New()
|
||||
hep = hepa.With(hep, filters.Home())
|
||||
hep = hepa.With(hep, filters.Golang())
|
||||
|
||||
b, err := hep.Filter(b)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bb := &bytes.Buffer{}
|
||||
gz := gzip.NewWriter(bb)
|
||||
|
||||
|
|
Loading…
Reference in New Issue