Skip to content

Commit

Permalink
helm-ez first commit 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
vyshakhp committed Jul 17, 2018
1 parent fb42f32 commit 88b983d
Show file tree
Hide file tree
Showing 102 changed files with 28,851 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

bin/
33 changes: 33 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"


[[constraint]]
branch = "master"
name = "github.com/icza/dyno"

[[constraint]]
name = "github.com/imdario/mergo"
version = "0.3.5"

[[constraint]]
name = "github.com/urfave/cli"
version = "1.20.0"

[[constraint]]
name = "gopkg.in/yaml.v2"
version = "2.2.1"
75 changes: 75 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
SHELL := /bin/bash
GO := GO15VENDOREXPERIMENT=1 go
NAME := helmez-bin

OS := $(shell uname)
MAIN_GO := helmez.go
ROOT_PACKAGE := github.com/ysaakpr/$(NAME)
GO_VERSION := $(shell $(GO) version | sed -e 's/^[^0-9.]*\([0-9.]*\).*/\1/')
PACKAGE_DIRS := $(shell $(GO) list ./... | grep -v /vendor/)
PKGS := $(shell go list ./... | grep -v /vendor | grep -v generated)
BUILDFLAGS := ''
CGO_ENABLED = 0
VENDOR_DIR=vendor

all: build

check: fmt build test

build:
CGO_ENABLED=$(CGO_ENABLED) $(GO) build -ldflags $(BUILDFLAGS) -o bin/$(NAME) $(MAIN_GO)

test:
CGO_ENABLED=$(CGO_ENABLED) $(GO) test $(PACKAGE_DIRS) -test.v

full: $(PKGS)

install:
GOBIN=${GOPATH}/bin $(GO) install -ldflags $(BUILDFLAGS) $(MAIN_GO)

fmt:
@FORMATTED=`$(GO) fmt $(PACKAGE_DIRS)`
@([[ ! -z "$(FORMATTED)" ]] && printf "Fixed unformatted files:\n$(FORMATTED)") || true

clean:
rm -rf build release

linux-amd64:
CGO_ENABLED=$(CGO_ENABLED) GOOS=linux GOARCH=amd64 $(GO) build -ldflags $(BUILDFLAGS) -o bin/$(NAME).linux.amd64 $(MAIN_GO)
linux-386:
CGO_ENABLED=$(CGO_ENABLED) GOOS=linux GOARCH=386 $(GO) build -ldflags $(BUILDFLAGS) -o bin/$(NAME).linux.386 $(MAIN_GO)
darwin-amd64:
CGO_ENABLED=$(CGO_ENABLED) GOOS=darwin GOARCH=amd64 $(GO) build -ldflags $(BUILDFLAGS) -o bin/$(NAME).darwin.amd64 $(MAIN_GO)
darwin-386:
CGO_ENABLED=$(CGO_ENABLED) GOOS=darwin GOARCH=386 $(GO) build -ldflags $(BUILDFLAGS) -o bin/$(NAME).darwin.386 $(MAIN_GO)
windows-amd64:
CGO_ENABLED=$(CGO_ENABLED) GOOS=windows GOARCH=amd64 $(GO) build -ldflags $(BUILDFLAGS) -o bin/$(NAME).windows.amd64 $(MAIN_GO)
windows-386:
CGO_ENABLED=$(CGO_ENABLED) GOOS=windows GOARCH=386 $(GO) build -ldflags $(BUILDFLAGS) -o bin/$(NAME).windows.386 $(MAIN_GO)

.PHONY: release clean

release-all: linux-amd64 linux-386 darwin-amd64 darwin-386 windows-amd64 windows-386

FGT := $(GOPATH)/bin/fgt
$(FGT):
go get github.com/GeertJohan/fgt

GOLINT := $(GOPATH)/bin/golint
$(GOLINT):
go get github.com/golang/lint/golint

$(PKGS): $(GOLINT) $(FGT)
@echo "LINTING"
@$(FGT) $(GOLINT) $(GOPATH)/src/$@/*.go
@echo "VETTING"
@go vet -v $@
@echo "TESTING"
@go test -v $@

.PHONY: lint
lint: vendor | $(PKGS) $(GOLINT) #
@cd $(BASE) && ret=0 && for pkg in $(PKGS); do \
test -z "$$($(GOLINT) $$pkg | tee /dev/stderr)" || ret=1 ; \
done ; exit $$ret

Binary file added helmez-bin
Binary file not shown.
172 changes: 172 additions & 0 deletions helmez.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package main

import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"

"github.com/icza/dyno"
"github.com/imdario/mergo"
"github.com/urfave/cli"
"gopkg.in/yaml.v2"
)

//DefaultPath is the default path in case no paths are provided
var DefaultPath = "./"

//ConfigFile the config file name to configure the helmez
var ConfigFile = "helmez.yaml"

var defaultHelmezConfig HelmezConfig

func main() {
app := cli.NewApp()

defaultHelmezConfig = HelmezConfig{
ValuesFileName: "values.yaml",
IgnoreDotFile: true,
ExtraFilesToKV: true,
ExtraFileRoot: []string{"config", "files"},
IgnoredFiles: []string{},
}

app.Flags = []cli.Flag{
cli.StringFlag{
Name: "base, b",
Usage: "Execute in base directory `BASE`",
Value: "./",
},

cli.StringFlag{
Name: "dir, d",
Usage: "default folder name `DIR`, default `values`",
Value: "values",
},

cli.StringFlag{
Name: "helmez",
Usage: "default helmez config file `HELMEZ`",
Value: "helmez.yaml",
},
}

app.Action = Run

err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}

// Run runs the application
func Run(c *cli.Context) error {
base := c.String("base")
dir := c.String("dir")
v := prepareValuesFile(base, dir, defaultHelmezConfig)
d, err := yaml.Marshal(&v)
if err != nil {
log.Fatalf("error: %v", err)
}
fmt.Printf("--- t dump:\n%s\n\n", string(d))
return nil
}

// HelmezConfig config for helmez
type HelmezConfig struct {
ValuesFileName string `yaml:"values_file_name"`
IgnoreDotFile bool `yaml:"ignore_dot_file"`
ExtraFilesToKV bool `yaml:"extra_files_to_kv"`
ExtraFileRoot []string `yaml:"extra_files_root"`
IgnoredFiles []string `yaml:"ignored_files"`
}

func isValidFile(config HelmezConfig, fi os.FileInfo) bool {
if fi.Name() != config.ValuesFileName && fi.Name() != ConfigFile {
if config.IgnoreDotFile && strings.HasPrefix(fi.Name(), ".") {
return false
}

for _, val := range config.IgnoredFiles {
if fi.Name() == val {
return false
}
}
return true
}
return false

}

func prepareValuesFile(base string, dir string, parent HelmezConfig) interface{} {
folder := base + dir + "/"

hConfig := new(HelmezConfig)
if err := mergo.Merge(hConfig, parent); err != nil {
log.Printf("here")
log.Fatal(err)
}

helmezOvr := new(HelmezConfig)
helmez, err := ioutil.ReadFile(folder + ConfigFile)
if err == nil {
yaml.Unmarshal(helmez, helmezOvr)
if err := mergo.Merge(hConfig, helmezOvr, mergo.WithOverride); err != nil {
log.Printf("here 1")
log.Fatal(err)
}
}

fmt.Printf("%#v\n\n", hConfig)

valuesFile := hConfig.ValuesFileName
data, err := ioutil.ReadFile(folder + valuesFile)
if err != nil {
log.Printf("here 2")
log.Fatal(err)
}

subs, er := ioutil.ReadDir(folder)
if er != nil {
log.Fatal(er)
}

files := make(map[string]string)
deps := make(map[string]interface{})
for _, fi := range subs {
if isValidFile(*hConfig, fi) {
if fi.IsDir() {
deps[fi.Name()] = prepareValuesFile(folder, fi.Name(), *hConfig)
} else if hConfig.ExtraFilesToKV {
data, err := ioutil.ReadFile(folder + fi.Name())
if err != nil {
log.Fatal(err)
}

files[fi.Name()] = string(data[:])
}
}
}

var v interface{}
if err := yaml.Unmarshal([]byte(data), &v); err != nil {
panic(err)
}

if len(files) > 0 {
if err = dyno.Set(v, files, "config", "files"); err != nil {
fmt.Printf("unable : %v\n", err)
}
}

if len(deps) > 0 {
for key, val := range deps {
if err = dyno.Set(v, val, key); err != nil {
fmt.Printf("unable : %v\n", err)
}
}
}

return v
}
11 changes: 11 additions & 0 deletions vendor/github.com/icza/dyno/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 88b983d

Please sign in to comment.