Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: add config file and openshift4 plugin #107

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,62 @@ package cmd
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"log"
"os"
"os/exec"
"unicode"

"github.com/ghodss/yaml"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var outputFormat *string
var inputFile *string
var configFile *string

func init() {
outputFormat = rootCmd.PersistentFlags().StringP("output", "o", "yaml", "output format: yaml or json")
inputFile = rootCmd.Flags().StringP("file", "f", "-", "file path to neat, or - to read from stdin")
configFile = rootCmd.Flags().StringP("config", "c", "", "file path of config")
viper.BindPFlag("config", rootCmd.Flags().Lookup("config"))

rootCmd.SetOut(os.Stdout)
rootCmd.SetErr(os.Stderr)
rootCmd.MarkFlagFilename("file")
rootCmd.AddCommand(getCmd)
rootCmd.AddCommand(versionCmd)

}

func initConfig() {
viper.SetConfigName("kubectl-neat")
viper.SetConfigType("yaml")

// Search current directory for config file
viper.AddConfigPath(".")

// Search $HOME/.config/ for config file
home, err := os.UserHomeDir()
if err == nil {
viper.AddConfigPath(home + "/.config/")
}

// If a config file was explicitly specified, use only this file
confFile := viper.GetString("config")
if confFile != "" {
viper.SetConfigFile(confFile)
}

if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
fmt.Println("No config file found") //TODO
} else {
// Config file was found but another error was produced
log.Fatalf("Error reading in config: %s", err)
}
}
}

// Execute is the entry point for the command package
Expand All @@ -51,17 +87,23 @@ var rootCmd = &cobra.Command{
Use: "kubectl-neat",
Example: `kubectl get pod mypod -o yaml | kubectl neat
kubectl get pod mypod -oyaml | kubectl neat -o json
kubectl get pod mypod -c ~/.config/kubectl-neat.conf | kubectl neat -o json
kubectl neat -f - <./my-pod.json
kubectl neat -f ./my-pod.json
kubectl neat -f ./my-pod.json --output yaml`,
kubectl neat -f ./my-pod.json --output yaml
kubectl neat -f ./my-pod.json --output yaml --config ~/.config/kubectl-neat.conf`,
RunE: func(cmd *cobra.Command, args []string) error {
initConfig()
var in, out []byte
var err error
if *inputFile == "-" {
stdin := cmd.InOrStdin()
in, err = ioutil.ReadAll(stdin)
in, err = io.ReadAll(stdin)
if err != nil {
return err
}
} else {
in, err = ioutil.ReadFile(*inputFile)
in, err = os.ReadFile(*inputFile)
if err != nil {
return err
}
Expand Down
37 changes: 37 additions & 0 deletions cmd/neat.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,21 @@ package cmd

import (
"fmt"
"log"
"strings"

"github.com/itaysk/kubectl-neat/pkg/defaults"
"github.com/itaysk/kubectl-neat/pkg/openshift4"
"github.com/spf13/viper"

"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)

type plugin struct {
Name string
}

// Neat gets a Kubernetes resource json as string and de-clutters it to make it more readable.
func Neat(in string) (string, error) {
var err error
Expand Down Expand Up @@ -87,6 +94,36 @@ func Neat(in string) (string, error) {
if err != nil {
return draft, fmt.Errorf("error in neatStatus : %v", err)
}

// parse plugins
var plugins []plugin
if viper.IsSet("plugins") {
err = viper.UnmarshalKey("plugins", &plugins)
if err != nil {
log.Fatalf("unable to decode into struct, %v", err)
}
if len(plugins) == 0 {
fmt.Println("empty plugins") //TODO
}
}

// plugin neating
for _, plugin := range plugins {
switch plugin.Name {
case "openshift4":
draft, err = openshift4.NeatOpenShift4(draft)
if err != nil {
return draft, fmt.Errorf("error in neatOpenShift4: %v", err)
}
default:
if plugin.Name == "" {
log.Fatalln("plugin must declare a name")
}
log.Fatalf("plugin %s not known\n", plugin.Name)
}
}

// general neating after plugins
draft, err = neatEmpty(draft)
if err != nil {
return draft, fmt.Errorf("error in neatEmpty : %v", err)
Expand Down
34 changes: 29 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,73 @@ toolchain go1.22.5
require (
github.com/ghodss/yaml v1.0.0
github.com/jeremywohl/flatten v0.0.0-20180923035001-588fe0d4c603
github.com/openshift/openshift-apiserver v0.0.0-alpha.0.0.20240514073636-6b5184128103
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.19.0
github.com/tidwall/gjson v1.9.3
github.com/tidwall/sjson v1.0.4
k8s.io/apimachinery v0.30.2
k8s.io/client-go v0.30.2
k8s.io/kubernetes v1.30.2
)

require github.com/openshift/library-go v0.0.0-20240513090140-e22d25af5587 // indirect

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/openshift/api v0.0.0-20240830023148-b7d0481c9094 // indirect
github.com/openshift/apiserver-library-go v0.0.0-20240313131158-facc40cc7688 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.10.1 // 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/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.30.2 // indirect
k8s.io/apiextensions-apiserver v0.0.0 // indirect
k8s.io/apiextensions-apiserver v0.30.1 // indirect
k8s.io/apiserver v0.30.2 // indirect
k8s.io/cloud-provider v0.29.2 // indirect
k8s.io/component-base v0.30.2 // indirect
k8s.io/controller-manager v0.30.2 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
k8s.io/kube-aggregator v0.29.2 // indirect
k8s.io/utils v0.0.0-20240310230437-4693a0247e57 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
Expand Down
Loading