Skip to content

Commit

Permalink
Merge pull request #31 from orbatschow/master
Browse files Browse the repository at this point in the history
  • Loading branch information
orbatschow authored May 4, 2023
2 parents f017fac + 6adec4c commit 13bd047
Show file tree
Hide file tree
Showing 20 changed files with 125 additions and 95 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ test: generate tparse
######################################################
.PHONY: generate
generate: clean
go generate github.com/orbatschow/kontext/pkg/version
go generate github.com/orbatschow/kontext/pkg/cmd/version
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,45 @@ included sources files first and then removes all duplicates. After the include
been computed the same happens for all files, that shall be excluded. Take a look at the
[example](./example/kontext.yaml) to understand sources in depth.

## Usage

```shell
[I] ➜ kx -h
manage kubernetes config files, contexts, groups and sources

Usage:
kontext [flags]
kontext [command]

Available Commands:
completion Generate the autocompletion script for the specified shell
get get [context|group] [name], defaults to context
help Help about any command
reload reload the active group
set set [context|group] [name]
version version for kontext

Flags:
-h, --help help for kontext
-v, --verbosity int verbose output (default 3)

Use "kontext [command] --help" for more information about a command.
```

### Verbosity

You can tweak the verbosity by using the following levels:

```shell
1 -> Trace
2 -> Debug
3 -> Info (default)
4 -> Warn
5 -> Error
6 -> Fatal
7 -> Print
```

## Demo

![Demo](./assets/demo.svg)
Expand Down
84 changes: 38 additions & 46 deletions example/kontext.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,20 @@ global:
# will be replaced each time a group/context is set
# see .backup for backup/restore options
kubeconfig: "$HOME/.config/kontext/kubeconfig.yaml"
# verbosity, possible values are:
# - 1 -> Trace
# - 2 -> Debug
# - 3 -> Info
# - 4 -> Warn
# - 5 -> Error
# - 6 -> Fatal
# - 7 -> Print
verbosity: 3

# state configuration options
state:
# override the default state file path
file: $HOME/.local/state/kontext/state.json
file: "$HOME/.local/state/kontext/state.json"

# backup configuration settings
# backup targets:
# - .global.kubeconfig
backup:
# enable the backup, defaults to true
enabled: true
enabled: "true"
# override the default backup directory
directory: $HOME/.local/share/kontext/backup
directory: "$HOME/.local/share/kontext/backup"
# override the maximum number of kubeconfig files, that shall be kept by kontext
revisions: 10

Expand All @@ -35,95 +26,96 @@ group:
# define groups
items:
# a group named "default"
- name: default
- name: "default"
# all sources referred by this group
sources:
- primary
- "primary"
# context configuration options within this group
context:
# set a default context, that will be activated as soon as you switch to this group
default: kind-kind
default: "kind-kind"
selection:
# make this context the default selected option within the interactive dialogue
# note: you can use "-", as a value, to use the active context as the new selection default
default: kind-kind
default: "kind-kind"
# sort the interactive selection
# possible values, defaults to asc:
# - asc
# - desc
sort: asc
sort: "asc"

# another group called dev, that refers to the sources of multiple customers
- name: dev
- name: "dev"
# set a default context, that will be activated as soon as you switch to this group
# note: the context has to be available within this group
context: dev-01
context:
default: "dev-01"
sources:
- customer-a.dev
- customer-b.dev
- "customer-a.dev"
- "customer-b.dev"

# another group called prod, that refers to a source called prod
- name: prod
- name: "prod"
sources:
- prod
- "prod"

# another group called testing, that refers to multiple sources
- name: testing
- name: "testing"
sources:
- local
- private
- "local"
- "private"

# interactive selection options for all groups
selection:
# make this group the default selected group
# note: you can use "-", as a value, to use the active group as the new selection default
default: dev
default: "dev"
# sort the interactive selection
# possible values, defaults to asc:
# - asc
# - desc
sort: asc
sort: "asc"


# define all sources, that are used by groups
source:
items:
# a source called primary, that defines one include and one exclude glob
- name: primary
- name: "primary"
include:
- $HOME/.config/kontext/**/*.yaml
- "$HOME/.config/kontext/**/*.yaml"
exclude:
- $HOME/.config/kontext/**/*prod*.yaml
- "$HOME/.config/kontext/**/*prod*.yaml"

# a source called customer-a.dev, that defines one include and one exclude glob
- name: customer-a.dev
- name: "customer-a.dev"
include:
- $HOME/.config/kontext/dev/customer-a/**/*.yaml
- "$HOME/.config/kontext/dev/customer-a/**/*.yaml"
exclude:
- $HOME/.config/kontext/dev/**/*skip*.yaml
- "$HOME/.config/kontext/dev/**/*skip*.yaml"

# a source called customer-b.dev, that defines one include and one exclude glob
- name: customer-b.dev
- name: "customer-b.dev"
include:
- $HOME/.config/kontext/dev/customer-b/**/*.yaml
- "$HOME/.config/kontext/dev/customer-b/**/*.yaml"
exclude:
- $HOME/.config/kontext/dev/**/*skip*.yaml
- "$HOME/.config/kontext/dev/**/*skip*.yaml"

# a source called prod, that defines one include and one exclude glob
- name: prod
- name: "prod"
include:
- $HOME/.config/kontext/prod/**/*.yaml
- "$HOME/.config/kontext/prod/**/*.yaml"
exclude:
- $HOME/.config/kontext/prod/**/*skip*.yaml
- "$HOME/.config/kontext/prod/**/*skip*.yaml"

# a source called local, that defines three include and no exclude globs
- name: local
- name: "local"
include:
- $HOME/.config/kontext/orbatschow/**/*.yaml
- $HOME/.config/kontext/local/**/*.yaml
- $HOME/.config/kontext/kind/**/*.yaml
- "$HOME/.config/kontext/orbatschow/**/*.yaml"
- "$HOME/.config/kontext/local/**/*.yaml"
- "$HOME/.config/kontext/kind/**/*.yaml"

# a source called local, that defines one include and no exclude glob
- name: private
- name: "private"
include:
- $HOME/.config/kontext/private/**/*.yaml
- "$HOME/.config/kontext/private/**/*.yaml"
3 changes: 3 additions & 0 deletions pkg/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func (r *Reconciler) Reconcile() error {

// create creates a new backup revision
func (r *Reconciler) create() (*os.File, error) {
log := logger.New()

file, err := os.Open(r.Config.Global.Kubeconfig)
if err != nil {
return nil, err
Expand Down Expand Up @@ -85,6 +87,7 @@ func (r *Reconciler) create() (*os.File, error) {
if err != nil {
return nil, err
}
log.Trace("created new backup", log.Args("file", backupFile.Name()))

return backupFile, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/backup/revision/revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (r *Reconciler) Reconcile() ([]state.Revision, error) {
func deleteRevisions(revision state.Revision) error {
log := logger.New()

log.Info("removing backup revision", log.Args("file", revision))
log.Trace("removing backup revision", log.Args("file", revision))
err := os.Remove(string(revision))
if err != nil {
return err
Expand Down
13 changes: 5 additions & 8 deletions pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"github.com/orbatschow/kontext/pkg/cmd/get"
"github.com/orbatschow/kontext/pkg/cmd/reload"
"github.com/orbatschow/kontext/pkg/cmd/set"
"github.com/orbatschow/kontext/pkg/version"
"github.com/orbatschow/kontext/pkg/cmd/version"
"github.com/orbatschow/kontext/pkg/logger"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
Expand All @@ -16,12 +17,6 @@ var rootCmd = &cobra.Command{
Short: "manage kubernetes config files, contexts, groups and sources",
PreRun: set.Init,
Run: func(cmd *cobra.Command, args []string) {
isVersionFlagSet := cmd.Flags().Lookup("version").Changed
if isVersionFlagSet {
pterm.Printfln(version.Compute())
os.Exit(0)
}

set.NewSetContextCommand(cmd, args)
},
}
Expand All @@ -31,7 +26,9 @@ func Execute() {
rootCmd.AddCommand(get.NewCommand())
rootCmd.AddCommand(set.NewCommand())
rootCmd.AddCommand(reload.NewCommand())
rootCmd.Flags().BoolP("version", "v", false, "version for kontext")
rootCmd.AddCommand(version.NewCommand())

rootCmd.PersistentFlags().IntVarP(&logger.Verbosity, "verbosity", "v", logger.DefaultVerbosity, "verbose output")

if err := rootCmd.Execute(); err != nil {
pterm.Printfln("%v", err)
Expand Down
3 changes: 0 additions & 3 deletions pkg/cmd/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ func Init(_ *cobra.Command, _ []string) {
log.Fatal(err.Error())
}

// initialize logger
logger.Init(currentConfig)

// initialize state
err = state.Init(currentConfig)
if err != nil {
Expand Down
3 changes: 0 additions & 3 deletions pkg/cmd/set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ func Init(_ *cobra.Command, _ []string) {
log.Fatal(err.Error())
}

// initialize logger
logger.Init(currentConfig)

// initialize currentState
err = state.Init(currentConfig)
if err != nil {
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions pkg/cmd/version/data/commit.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ada6fcc8444b4c15c073b863862468d3b56668f4
1 change: 1 addition & 0 deletions pkg/cmd/version/data/tag.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
undefined
19 changes: 17 additions & 2 deletions pkg/version/version.go → pkg/cmd/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ package version
import (
_ "embed"
"strings"

"github.com/pterm/pterm"
"github.com/spf13/cobra"
)

const undefined = "undefined"
const devel = "(devel)"

//go:generate sh -c "git name-rev --tags --name-only $(git rev-parse HEAD) > data/tag.txt"
//go:embed data/tag.txt
Expand All @@ -15,7 +19,7 @@ var Tag string
//go:embed data/commit.txt
var Commit string

func Compute() string {
func compute() string {
Tag = strings.ReplaceAll(Tag, "\n", "")
Commit = strings.ReplaceAll(Commit, "\n", "")

Expand All @@ -25,5 +29,16 @@ func Compute() string {
if Commit != undefined && len(Commit) > 0 {
return Commit
}
return "(devel)"
return devel
}

func NewCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "version for kontext",
Run: func(cmd *cobra.Command, args []string) {
pterm.Printfln(compute())
},
}
return cmd
}
5 changes: 1 addition & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/providers/structs"
"github.com/pterm/pterm"
)

var (
Expand All @@ -35,8 +34,7 @@ type Config struct {
}

type Global struct {
Kubeconfig string `json:"kubeconfig,omitempty"`
Verbosity pterm.LogLevel `json:"verbosity,omitempty"`
Kubeconfig string `json:"kubeconfig,omitempty"`
}

// State configuration options
Expand Down Expand Up @@ -108,7 +106,6 @@ func (r *Client) Read() (*Config, error) {
err := instance.Load(structs.Provider(Config{
Global: Global{
Kubeconfig: os.Getenv("KUBECONFIG"),
Verbosity: pterm.LogLevelInfo,
},
Backup: Backup{
Enabled: true,
Expand Down
3 changes: 0 additions & 3 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/adrg/xdg"
"github.com/google/go-cmp/cmp"
"github.com/pterm/pterm"
)

func Test_Read(t *testing.T) {
Expand Down Expand Up @@ -37,7 +36,6 @@ func Test_Read(t *testing.T) {
want: &Config{
Global: Global{
Kubeconfig: filepath.Join(xdg.ConfigHome, "kontext", "kubeconfig.yaml"),
Verbosity: pterm.LogLevelInfo,
},
Backup: Backup{
Enabled: true,
Expand Down Expand Up @@ -130,7 +128,6 @@ func Test_Read(t *testing.T) {

return path
}(),
Verbosity: pterm.LogLevelInfo,
},
Backup: Backup{
Enabled: true,
Expand Down
Loading

0 comments on commit 13bd047

Please sign in to comment.