Skip to content

Commit

Permalink
initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
gilesw committed Mar 21, 2024
0 parents commit 42d6137
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This GitHub action can publish assets for release when a tag is created.
# Currently its setup to run on any tag that matches the pattern "v*" (ie. v0.1.0).

name: release
on:
push:
tags:
- 'v*'
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@v2
with:
go-version: 1.14
-
name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GPG_FINGERPRINT: ${{ steps.import_gpg.outputs.fingerprint }}
38 changes: 38 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Visit https://goreleaser.com for documentation on how to customize this
# behavior.
before:
hooks:
# this is just an example and not a requirement for provider building/publishing
- go mod tidy
builds:
- env:
# goreleaser does not work with CGO, it could also complicate
# usage by users in CI/CD systems like Terraform Cloud where
# they are unable to install libraries.
- CGO_ENABLED=0
mod_timestamp: '{{ .CommitTimestamp }}'
flags:
- -trimpath
ldflags:
- '-s -w -X main.version={{.Version}} -X main.commit={{.Commit}}'
goos:
- darwin
- windows
- linux
goarch:
- amd64
- '386'
- arm
- arm64
binary: '{{ .ProjectName }}_v{{ .Version }}'
archives:
- format: zip
name_template: '{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}'
checksum:
name_template: '{{ .ProjectName }}_{{ .Version }}_SHA256SUMS'
algorithm: sha256
release:
# Visit your project's GitHub Releases page to publish this release .
draft: true
changelog:
skip: true
15 changes: 15 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ORCID MIT-Style License (MIT)

Copyright © 2012-2018 ORCID, Inc.

http://orcid.org

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 license does not apply to the branding or the "look and feel" of the websites located at the orcid.org URL even if elements thereof are contained in 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.

Except to provide the copyright notice required above or as allowed under ORCID Inc.'s Trademark Use Policy (available at http://orcid.org, under "Policies"), you may not use the name of ORCID, Inc., ORCID ®, its marks and logo, to advertise, promote or suggest any affiliation with or endorsement by ORCID, Inc. in connection with your use of the Software.

The above copyright notice and the full text of this license or a link thereto shall be included in all copies or substantial portions of the Software.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# jinjafier

## Introduction

Jinjafier is a small script to convert and existing java properties file to a jinja2 template with an included yml file with the current values of the property file.
4 changes: 4 additions & 0 deletions example.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# test comment
org.wibble.testCamelCaps=20
org.wibble.test.long.property=this is a long prop that is space separated to test whether wrapping occurs in the go yaml lib

4 changes: 4 additions & 0 deletions example.properties.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# test comment
org.wibble.testCamelCaps={{ ORG_WIBBLE_TEST_CAMEL_CAPS }}
org.wibble.test.long.property={{ ORG_WIBBLE_TEST_LONG_PROPERTY }}

2 changes: 2 additions & 0 deletions example.properties.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ORG_WIBBLE_TEST_CAMEL_CAPS: "20"
ORG_WIBBLE_TEST_LONG_PROPERTY: this is a long prop that is space separated to test whether wrapping occurs in the go yaml lib
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module jinjafier

go 1.20

require gopkg.in/yaml.v3 v3.0.1

require gopkg.in/yaml.v2 v2.4.0 // indirect
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
78 changes: 78 additions & 0 deletions jinjafier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package main

import (
"bufio"
"fmt"
"io/ioutil"
"os"
"regexp"
"strings"

"gopkg.in/yaml.v3"
)

func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: jinjafier <java_properties_file>")
os.Exit(1)
}

filename := os.Args[1]
file, err := os.Open(filename)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer file.Close()

scanner := bufio.NewScanner(file)
jinjaTemplate := ""
yamlMap := make(map[string]string)

for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "#") {
jinjaTemplate += line + "\n"
} else if strings.Contains(line, "=") {
split := strings.Split(line, "=")
key := split[0]
value := split[1]

// Convert key to lowercase and replace dots and camel case with underscores
re := regexp.MustCompile("([a-z0-9])([A-Z])")
key = re.ReplaceAllString(key, "${1}_${2}")
key = strings.ReplaceAll(key, ".", "_")
key = strings.ToUpper(key)

// Add to jinja template
jinjaTemplate += fmt.Sprintf("%s={{ %s }}\n", split[0], key)

// Add to yaml map
yamlMap[key] = value
} else {
jinjaTemplate += line + "\n"
}
}

// Write jinja template to file
err = ioutil.WriteFile(strings.ReplaceAll(filename, ".properties", ".properties.j2"), []byte(jinjaTemplate), 0644)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// Convert yaml map to yaml
yamlData, err := yaml.Marshal(&yamlMap)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// Write yaml to file
err = ioutil.WriteFile(strings.ReplaceAll(filename, ".properties", ".properties.yml"), yamlData, 0644)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}

0 comments on commit 42d6137

Please sign in to comment.