Skip to content

Commit

Permalink
fix CLI interface, now with subcommands 'all' and 'crdsplit'
Browse files Browse the repository at this point in the history
  • Loading branch information
laverya committed May 5, 2021
1 parent c4891e6 commit 3af8740
Show file tree
Hide file tree
Showing 6 changed files with 286 additions and 55 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.state
bin
test
35 changes: 35 additions & 0 deletions ksplit/cmd/allsplit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cmd

import (
"github.com/go-ksplit/ksplit/pkg"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func AllSplitCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "all",
Short: "split kubernetes yaml within a directory",
Long: `ksplit reformats generated kubernetes yaml into a more easily readable file format`,
Example: "ksplit all myk8syamldir/",
SilenceErrors: true,
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlags(cmd.Flags())
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
cmd.Help()
return errors.New("Please supply a directory")
}

err := pkg.MaybeSplitMultidocYamlFs(args[0])
if err != nil {
return errors.Wrap(err, "allsplit cmd")
}
return nil
},
}

return cmd
}
35 changes: 35 additions & 0 deletions ksplit/cmd/crdsplit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cmd

import (
"github.com/go-ksplit/ksplit/pkg"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func CrdSplitCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "crdsplit",
Short: "split CRDs from non-CRD files",
Long: `...`,
Example: "ksplit crdsplit myk8syamldir/",
SilenceErrors: true,
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlags(cmd.Flags())
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
cmd.Help()
return errors.New("Please supply a directory")
}

err := pkg.MaybeSplitCRDsFs(args[0])
if err != nil {
return errors.Wrap(err, "crdsplit cmd")
}
return nil
},
}

return cmd
}
30 changes: 6 additions & 24 deletions ksplit/main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package ksplit
package main

import (
"fmt"
"os"
"strings"

"github.com/pkg/errors"
cmd2 "github.com/go-ksplit/ksplit/ksplit/cmd"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/go-ksplit/ksplit/pkg"
)

func main() {
Expand All @@ -20,29 +18,13 @@ func main() {
}

func RootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "ksplit directory",
Short: "split kubernetes yaml within a directory",
Long: `ksplit reformats generated kubernetes yaml into a more easily readable file format`,
Example: "ksplit myk8syamldir/",
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
cmd.Help()
return errors.New("Please supply a directory")
}

err := pkg.MaybeSplitMultidocYamlFs(args[0])
if err != nil {
return errors.Wrap(err, "root cmd")
}
return nil
},
}
cmd := &cobra.Command{}

cmd.PersistentFlags().String("log-level", "off", "Log level")

cmd.AddCommand(cmd2.CrdSplitCmd())
cmd.AddCommand(cmd2.AllSplitCmd())

_ = viper.BindPFlags(cmd.Flags())
_ = viper.BindPFlags(cmd.PersistentFlags())
viper.AutomaticEnv()
Expand Down
65 changes: 45 additions & 20 deletions pkg/ksplit.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (
)

type outputYaml struct {
name string
contents string
name string
contents string
overridePath string
}

type MinimalK8sYaml struct {
Expand All @@ -36,19 +37,30 @@ type ListK8sYaml struct {

func MaybeSplitMultidocYamlFs(localpath string) error {
fs := afero.NewOsFs()
return MaybeSplitMultidocYaml(afero.Afero{Fs: fs}, localpath)
return MaybeSplitMultidocYaml(afero.Afero{Fs: fs}, localpath, false)
}

func MaybeSplitCRDsFs(localpath string) error {
fs := afero.NewOsFs()
return MaybeSplitMultidocYaml(afero.Afero{Fs: fs}, localpath, true)
}

// this function is not perfect, and has known limitations. One of these is that it does not account for `\n---\n` in multiline strings.
func MaybeSplitMultidocYaml(fs afero.Afero, localPath string) error {
func MaybeSplitMultidocYaml(fs afero.Afero, localPath string, combineNonCRDs bool) error {
files, err := fs.ReadDir(localPath)
if err != nil {
return errors.Wrapf(err, "read files in %s", localPath)
}

allOutputFiles := []outputYaml{}
allCrds := []string{}

for _, file := range files {
outputFiles := []outputYaml{}
crds := []string{}

if file.IsDir() {
if err := MaybeSplitMultidocYaml(fs, filepath.Join(localPath, file.Name())); err != nil {
if err := MaybeSplitMultidocYaml(fs, filepath.Join(localPath, file.Name()), combineNonCRDs); err != nil {
return err
}
}
Expand All @@ -63,9 +75,7 @@ func MaybeSplitMultidocYaml(fs afero.Afero, localPath string) error {
return errors.Wrapf(err, "read %s", filepath.Join(localPath, file.Name()))
}

outputFiles := []outputYaml{}
filesStrings := strings.Split(string(inFileBytes), "\n---\n")
crds := []string{}

// generate replacement yaml files
for idx, fileString := range filesStrings {
Expand All @@ -79,14 +89,8 @@ func MaybeSplitMultidocYaml(fs afero.Afero, localPath string) error {
crds = append(crds, newCRDs...)
}

if len(crds) > 0 {
crdsFile := outputYaml{contents: strings.Join(crds, "\n---\n"), name: "CustomResourceDefinitions"}
outputFiles = append(outputFiles, crdsFile)
}

if len(outputFiles) < 2 {
// not a multidoc yaml, or at least not a multidoc kubernetes yaml
continue
if len(outputFiles)+len(crds) <= 1 { // don't rename files if we don't have to
outputFiles[0].overridePath = file.Name()
}

// delete multidoc yaml file
Expand All @@ -95,12 +99,33 @@ func MaybeSplitMultidocYaml(fs afero.Afero, localPath string) error {
return errors.Wrapf(err, "unable to remove %s", filepath.Join(localPath, file.Name()))
}

// write replacement yaml
for _, outputFile := range outputFiles {
allOutputFiles = append(allOutputFiles, outputFiles...)
allCrds = append(allCrds, crds...)
}

if combineNonCRDs {
allOutputStrings := []string{}
for _, outputFile := range allOutputFiles {
allOutputStrings = append(allOutputStrings, outputFile.contents)
}
nonCrdsFile := outputYaml{contents: strings.Join(allOutputStrings, "\n---\n"), name: "AllResorces"}
allOutputFiles = []outputYaml{nonCrdsFile}
}

if len(allCrds) > 0 {
crdsFile := outputYaml{contents: strings.Join(allCrds, "\n---\n"), name: "CustomResourceDefinitions"}
allOutputFiles = append(allOutputFiles, crdsFile)
}

// write replacement yaml
for _, outputFile := range allOutputFiles {
if outputFile.overridePath != "" {
err = fs.WriteFile(filepath.Join(localPath, outputFile.overridePath), []byte(outputFile.contents), os.FileMode(0644))
} else {
err = fs.WriteFile(filepath.Join(localPath, outputFile.name+".yaml"), []byte(outputFile.contents), os.FileMode(0644))
if err != nil {
return errors.Wrapf(err, "write %s", outputFile.name)
}
}
if err != nil {
return errors.Wrapf(err, "write %s", outputFile.name)
}
}

Expand Down
Loading

0 comments on commit 3af8740

Please sign in to comment.