Skip to content

Commit

Permalink
Add support to include triple dashes in output files. (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickdappollonio authored Mar 16, 2024
1 parent db8ad03 commit 0b46df9
Show file tree
Hide file tree
Showing 14 changed files with 170 additions and 73 deletions.
39 changes: 0 additions & 39 deletions .github/workflows/codeql-analysis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/releasing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.20"
go-version-file: "go.mod"
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v3
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.20"
go-version-file: "go.mod"
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v3
with:
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ Examples:
kubectl-slice --config config.yaml
Flags:
--allow-empty-kinds if enabled, resources with empty kinds don't produce an error when filtering
--allow-empty-names if enabled, resources with empty names don't produce an error when filtering
-c, --config string path to the config file
--dry-run if true, no files are created, but the potentially generated files will be printed as the command output
--exclude strings resource name to exclude in the output (format <kind>/<name>, case insensitive, glob supported)
Expand All @@ -98,6 +100,7 @@ Flags:
--include strings resource name to include in the output (format <kind>/<name>, case insensitive, glob supported)
--include-kind strings resource kind to include in the output (singular, case insensitive, glob supported)
--include-name strings resource name to include in the output (singular, case insensitive, glob supported)
--include-triple-dash if enabled, the typical "---" YAML separator is included at the beginning of resources sliced
-f, --input-file string the input file used to read the initial macro YAML file; if empty or "-", stdin is used
-o, --output-dir string the output directory used to output the splitted files
-q, --quiet if true, no output is written to stdout/err
Expand Down
1 change: 1 addition & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func root() *cobra.Command {
rootCommand.Flags().StringVarP(&configFile, "config", "c", "", "path to the config file")
rootCommand.Flags().BoolVar(&opts.AllowEmptyKinds, "allow-empty-kinds", false, "if enabled, resources with empty kinds don't produce an error when filtering")
rootCommand.Flags().BoolVar(&opts.AllowEmptyNames, "allow-empty-names", false, "if enabled, resources with empty names don't produce an error when filtering")
rootCommand.Flags().BoolVar(&opts.IncludeTripleDash, "include-triple-dash", false, "if enabled, the typical \"---\" YAML separator is included at the beginning of resources sliced")

_ = rootCommand.Flags().MarkHidden("debug")
return rootCommand
Expand Down
13 changes: 6 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module github.com/patrickdappollonio/kubectl-slice

go 1.20
go 1.22

require (
github.com/mb0/glob v0.0.0-20160210091149-1eb79d2de6c4
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.8.4
github.com/stretchr/testify v1.9.0
golang.org/x/text v0.14.0
gopkg.in/yaml.v3 v3.0.1
)
Expand All @@ -19,17 +19,16 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sys v0.15.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20240314144324-c7f7c6466f7f // indirect
golang.org/x/sys v0.18.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
)
28 changes: 16 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,32 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mb0/glob v0.0.0-20160210091149-1eb79d2de6c4 h1:NK3O7S5FRD/wj7ORQ5C3Mx1STpyEMuFe+/F0Lakd1Nk=
github.com/mb0/glob v0.0.0-20160210091149-1eb79d2de6c4/go.mod h1:FqD3ES5hx6zpzDainDaHgkTIqrPaI9uX4CVWqYZoQjY=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI=
github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
Expand All @@ -45,25 +50,24 @@ github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMV
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
golang.org/x/exp v0.0.0-20240314144324-c7f7c6466f7f h1:3CW0unweImhOzd5FmYuRsD4Y4oQFKZIjAnKbjV4WIrw=
golang.org/x/exp v0.0.0-20240314144324-c7f7c6466f7f/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
14 changes: 12 additions & 2 deletions slice/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,22 @@ func (s *Split) store() error {
continue

default:
local := make([]byte, 0, len(v.data)+4)

// If the user wants to include the triple dash, add it
// at the beginning of the file
if s.opts.IncludeTripleDash && !bytes.Equal(v.data, []byte("---")) {
local = append([]byte("---\n"), v.data...)
} else {
local = append(local, v.data...)
}

// do nothing, handling below
if err := s.writeToFile(fullpath, v.data); err != nil {
if err := s.writeToFile(fullpath, local); err != nil {
return err
}

s.WriteStderr("Wrote %s -- %d bytes.", fullpath, fileLength)
s.WriteStderr("Wrote %s -- %d bytes.", fullpath, len(local))
continue
}
}
Expand Down
108 changes: 108 additions & 0 deletions slice/execute_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package slice

import (
"io"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -179,6 +180,8 @@ kind: "Namespace

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

s := &Split{
opts: tt.fields,
log: nolog,
Expand Down Expand Up @@ -210,6 +213,7 @@ func TestExecute_writeToFileCases(t *testing.T) {
s := &Split{log: nolog}

t.Run("write new file", func(tt *testing.T) {
t.Parallel()
require.NoError(tt, s.writeToFile(filepath.Join(tempdir, "test.txt"), []byte("test")))
content, err := os.ReadFile(filepath.Join(tempdir, "test.txt"))
require.NoError(tt, err)
Expand Down Expand Up @@ -237,3 +241,107 @@ func TestExecute_writeToFileCases(t *testing.T) {
require.Error(tt, s.writeToFile(filepath.Join(tempdir, "readonly_sub", "readonly", "test.txt"), []byte("test")))
})
}

func TestAddingTripleDashes(t *testing.T) {
cases := []struct {
name string
input string
includeDashes bool
output map[string]string
}{
{
name: "empty file",
input: `---`,
output: map[string]string{"-.yaml": "---\n"},
},
{
name: "simple no dashes",
input: `apiVersion: v1
kind: Pod
metadata:
name: nginx-ingress
---
apiVersion: v1
kind: Namespace
metadata:
name: production`,
output: map[string]string{
"pod-nginx-ingress.yaml": "apiVersion: v1\nkind: Pod\nmetadata:\n name: nginx-ingress\n",
"namespace-production.yaml": "apiVersion: v1\nkind: Namespace\nmetadata:\n name: production\n",
},
},
{
name: "simple with dashes",
includeDashes: true,
input: `apiVersion: v1
kind: Pod
metadata:
name: nginx-ingress
---
apiVersion: v1
kind: Namespace
metadata:
name: production`,
output: map[string]string{
"pod-nginx-ingress.yaml": "---\napiVersion: v1\nkind: Pod\nmetadata:\n name: nginx-ingress\n",
"namespace-production.yaml": "---\napiVersion: v1\nkind: Namespace\nmetadata:\n name: production\n",
},
},
{
name: "simple with dashes - adding empty intermediate files",
includeDashes: true,
input: `apiVersion: v1
kind: Pod
metadata:
name: nginx-ingress
---
---
---
---
apiVersion: v1
kind: Namespace
metadata:
name: production`,
output: map[string]string{
"pod-nginx-ingress.yaml": "---\napiVersion: v1\nkind: Pod\nmetadata:\n name: nginx-ingress\n",
"namespace-production.yaml": "---\napiVersion: v1\nkind: Namespace\nmetadata:\n name: production\n",
},
},
}

for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

tdinput := t.TempDir()
tdoutput := t.TempDir()
require.NotEqual(t, tdinput, tdoutput, "input and output directories should be different")

err := os.WriteFile(filepath.Join(tdinput, "input.yaml"), []byte(tt.input), 0644)
require.NoError(t, err, "error found while writing input file")

s, err := New(Options{
GoTemplate: DefaultTemplateName,
IncludeTripleDash: tt.includeDashes,
InputFile: filepath.Join(tdinput, "input.yaml"),
OutputDirectory: tdoutput,
Stderr: os.Stderr,
Stdout: io.Discard,
})
require.NoError(t, err, "error found while creating new Split instance")
require.NoError(t, s.Execute(), "error found while executing slice")

files, err := os.ReadDir(tdoutput)
require.NoError(t, err, "error found while reading output directory")

for _, file := range files {
content, err := os.ReadFile(filepath.Join(tdoutput, file.Name()))
require.NoError(t, err, "error found while reading file %q", file.Name())

expected, found := tt.output[file.Name()]
require.True(t, found, "expected file %q to be found in the output map", file.Name())
require.Equal(t, expected, string(content), "expected content to be equal for file %q", file.Name())
}
})
}
}
6 changes: 4 additions & 2 deletions slice/kube.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package slice

import "sort"
import (
"sort"
)

type yamlFile struct {
filename string
Expand Down Expand Up @@ -68,7 +70,7 @@ func sortYAMLsByKind(manifests []yamlFile) []yamlFile {
}

// from: https://github.com/helm/helm/blob/v3.11.1/pkg/releaseutil/kind_sorter.go#L133-L158
func lessByKind(a interface{}, b interface{}, kindA string, kindB string, o []string) bool {
func lessByKind(_ interface{}, _ interface{}, kindA string, kindB string, o []string) bool {
ordering := make(map[string]int, len(o))
for v, k := range o {
ordering[k] = v
Expand Down
2 changes: 1 addition & 1 deletion slice/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (s *Split) parseYAMLManifest(contents []byte) (yamlFile, error) {
name = strings.NewReplacer("<no value>", "", "\n", "").Replace(name)

if str := strings.TrimSuffix(name, filepath.Ext(name)); str == "" {
return yamlFile{}, fmt.Errorf("file name rendered will yield no file name for YAML file number %d", s.fileCount)
return yamlFile{}, fmt.Errorf("file name rendered will yield no file name for YAML file number %d (original name: %q, metadata: %v)", s.fileCount, name, k8smeta)
}

return yamlFile{filename: name, meta: k8smeta}, nil
Expand Down
Loading

0 comments on commit 0b46df9

Please sign in to comment.