Skip to content

Commit

Permalink
Add test for existing xml elements (#17)
Browse files Browse the repository at this point in the history
* Add test for existing xml elements

* order yaml before compare

* Use markdown license

* Remove licenses from headers

* Document refsecrets

* more lint

* lint

* n

* simplification (gofumpt extra)
  • Loading branch information
mniak authored Jun 4, 2021
1 parent 5237532 commit a50ea52
Show file tree
Hide file tree
Showing 21 changed files with 181 additions and 178 deletions.
21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

18 changes: 18 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
The MIT License (MIT)
=======================

Copyright © 2020 Andre Soares

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.
> ```
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,23 @@ bpdt env-to-yaml <.env-file-path> <yaml-file-path>
- **`--directory`, `-d`:** Directory where the files will be looked for
- **`--ypath`:** A period separated string indicating where in the YAML the variables should be appended


### `bpdt ref-secrets`
Adds a secret key reference variable to a yaml file.
The file is tipically a kubernetes Deployment or Pod resource file.

```bash
bpdt ref-secrets <file-path> <secret-name>
```

#### Arguments
1. **`<file-path>`:** Path of the _.env_ file
2. **`<secret-name>`:** Path of the YAML file

#### Flags
- **`--ypath`:** A period separated string indicating where in the YAML the variables are placed. _Default: `spec.template.spec.containers.0.env`_
- **`--directory`, `-d`:**: Directory where the files will be looked for. _Default: working directory_

- **`--prefix`, `-p`:** The prefix for the secret variables. _Default: `#<Secret>{`_
- **`--suffix`, `-s`:** The suffix for the secret variables. _Default: `}#`_

2 changes: 2 additions & 0 deletions cmd/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package cmd provide the entry point for the bpdt tool
package cmd
21 changes: 0 additions & 21 deletions cmd/env-to-yaml.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,3 @@
/*
Copyright © 2020 BPDT Authors
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.
*/
package cmd

import (
Expand Down
21 changes: 0 additions & 21 deletions cmd/export-settings.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,3 @@
/*
Copyright © 2020 BPDT Authors
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.
*/
package cmd

import (
Expand Down
21 changes: 0 additions & 21 deletions cmd/patch-nuget.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,3 @@
/*
Copyright © 2020 BPDT Authors
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.
*/
package cmd

import (
Expand Down
21 changes: 4 additions & 17 deletions cmd/ref-secrets.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
/*
Copyright © 2020 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
Expand All @@ -22,9 +7,11 @@ import (
"github.com/spf13/cobra"
)

// linkSecretsCmd represents the linkSecrets command
// refSecretsCmd represents the linkSecrets command
var refSecretsCmd = &cobra.Command{
Use: "ref-secrets <file-path> <secret-name>",
Use: "ref-secrets <file-path> <secret-name>",
Short: "Add secret key reference variable to a yaml file",
Long: "Add secret key reference variable to a yaml file. Tipically a kubernetes Deployment or Pod resource file.",
Aliases: []string{
"refsecrets",
"refsec",
Expand Down
28 changes: 2 additions & 26 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,11 @@
/*
Copyright © 2020 BPDT Authors
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.
*/
package cmd

import (
"fmt"
"github.com/spf13/cobra"
"os"
)


"github.com/spf13/cobra"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -64,5 +42,3 @@ func init() {
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}


1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.14

require (
github.com/beevik/etree v1.1.0
github.com/bxcodec/faker/v3 v3.6.0
github.com/dimchansky/utfbom v1.1.1
github.com/jeremywohl/flatten v1.0.1
github.com/joho/godotenv v1.3.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84=
github.com/bxcodec/faker/v3 v3.6.0 h1:Meuh+M6pQJsQJwxVALq6H5wpDzkZ4pStV9pmH7gbKKs=
github.com/bxcodec/faker/v3 v3.6.0/go.mod h1:gF31YgnMSMKgkvl+fyEo1xuSMbEuieyqfeslGYFjneM=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
Expand Down
4 changes: 3 additions & 1 deletion lib/config/config.go → lib/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import (
"muzzammil.xyz/jsonc"
)

// Configuration is a map of strings
type Configuration map[string]string

func (config *Configuration) AddJsonReader(reader io.Reader) error {
// AddJSONReader loads configuration from a JSON reader
func (config *Configuration) AddJSONReader(reader io.Reader) error {
reader = utfbom.SkipOnly(reader)

bytes, err := ioutil.ReadAll(reader)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ func TestExport(t *testing.T) {
require.NoError(t, err, "Could not open file2.json")
defer file1.Close()

err = config.AddJsonReader(file1)
err = config.AddJSONReader(file1)
require.NoError(t, err, "Failed to add file1")

err = config.AddJsonReader(file2)
err = config.AddJSONReader(file2)
require.NoError(t, err, "Failed to add file2")

expected := map[string]string{
Expand Down
File renamed without changes.
File renamed without changes.
48 changes: 38 additions & 10 deletions lib/nuget.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,51 @@ import (
"github.com/palantir/stacktrace"
)

func findOrCreateElement(xml *etree.Element, name string) *etree.Element {
if el := xml.SelectElement(name); el != nil {
return el
}
return xml.CreateElement(name)
}

func findOrCreateElementWithAttribute(xml *etree.Element, name, attrName, attrValue string) *etree.Element {
if els := xml.SelectElements(name); len(els) > 0 {
for _, el := range els {
if attr := el.SelectAttr(attrName); attr != nil {
if attr.Value == attrValue {
return el
}
}
}
}
el := xml.CreateElement(name)
el.CreateAttr(attrName, attrValue)
return el
}

func setAttribute(xml *etree.Element, name, value string) *etree.Attr {
if attr := xml.SelectAttr(name); attr != nil {
attr.Value = value
return attr
}
return xml.CreateAttr(name, value)
}

// PatchNuget adds clear text password to a nuget config
func PatchNuget(reader io.Reader, writer io.Writer, source, username, password string) error {
doc := etree.NewDocument()
_, err := doc.ReadFrom(reader)
if err != nil {
return stacktrace.Propagate(err, "Could not read XML")
}
creds := doc.Root().
CreateElement("packageSourceCredentials").
CreateElement(source)
add := creds.CreateElement("add")
add.CreateAttr("key", "Username")
add.CreateAttr("value", username)

add = creds.CreateElement("add")
add.CreateAttr("key", "ClearTextPassword")
add.CreateAttr("value", password)
pkgSrcCreds := findOrCreateElement(doc.Root(), "packageSourceCredentials")
creds := findOrCreateElement(pkgSrcCreds, source)

add := findOrCreateElementWithAttribute(creds, "add", "key", "Username")
setAttribute(add, "value", username)

add = findOrCreateElementWithAttribute(creds, "add", "key", "ClearTextPassword")
setAttribute(add, "value", password)

doc.IndentTabs()
_, err = doc.WriteTo(writer)
Expand Down
Loading

0 comments on commit a50ea52

Please sign in to comment.