Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into feature/printer-rework
Browse files Browse the repository at this point in the history
  • Loading branch information
orbatschow committed Apr 22, 2023
2 parents 30dd7cf + 3972228 commit d9fc8c2
Show file tree
Hide file tree
Showing 15 changed files with 977 additions and 225 deletions.
1 change: 1 addition & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
before:
hooks:
- go mod tidy
- make generate
builds:
- main: ./cmd/main.go
env:
Expand Down
149 changes: 86 additions & 63 deletions example/kontext.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,79 +27,102 @@ backup:
# enable the backup, defaults to true
enabled: true
# override the default backup directory
path: $HOME/.local/share/kontext
directory: $HOME/.local/share/kontext/backup
# override the maximum number of kubeconfig files, that shall be kept by kontext
revisions: 10

# groups define a set of sources to include
groups:
# a group named "default"
- name: default
# 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: kind-kind
# all sources referred by this group
sources:
- primary
# group configuration options
group:
# define groups
items:
# a group named "default"
- name: default
# all sources referred by this group
sources:
- 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
selection:
# make this context the default selected option within the interactive dialogue
default: kind-kind
# sort the interactive selection
# possible values, defaults to asc:
# - asc
# - desc
sort: asc

# another group called dev, that refers to the sources of multiple customers
- 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
sources:
- customer-a.dev
- customer-b.dev
# another group called dev, that refers to the sources of multiple customers
- 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
sources:
- customer-a.dev
- customer-b.dev

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

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

# interactive selection options for all groups
selection:
# make this group the default selected group
default: dev
# sort the interactive selection
# possible values, defaults to asc:
# - asc
# - desc
sort: asc

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

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

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

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

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

# a source called local, that defines three include and no exclude globs
- name: local
include:
- $HOME/.config/kontext/orbatschow/**/*.yaml
- $HOME/.config/kontext/local/**/*.yaml
- $HOME/.config/kontext/kind/**/*.yaml
# a source called local, that defines three include and no exclude globs
- name: local
include:
- $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
include:
- $HOME/.config/kontext/private/**/*.yaml
# a source called local, that defines one include and no exclude glob
- name: private
include:
- $HOME/.config/kontext/private/**/*.yaml
4 changes: 2 additions & 2 deletions pkg/cmd/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func newGetGroupCommand() *cobra.Command {
os.Exit(1)
}

var groups []config.Group
var groups []config.GroupItem
if len(groupName) != 0 {
match, err := groupClient.Get(groupName)
if err != nil {
Expand All @@ -60,7 +60,7 @@ func newGetGroupCommand() *cobra.Command {
}
groups = append(groups, *match)
} else {
groups = groupClient.Config.Groups
groups = groupClient.Config.Group.Items
}

err = groupClient.Print(groups...)
Expand Down
80 changes: 54 additions & 26 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,44 +27,72 @@ type Client struct {
File string
}

type Source struct {
Name string `json:"name"`
Include []string `json:"include,omitempty"`
Exclude []string `json:"exclude"`
type Config struct {
Global Global `json:"global,omitempty"`
State State `json:"state,omitempty"`
Backup Backup `json:"backup,omitempty"`
Group Group `json:"group,omitempty"`
Source Source `json:"source,omitempty"`
}

type Group struct {
Name string `json:"name"`
Context string `json:"context,omitempty"`
Sources []string `json:"sources"`
type Global struct {
Kubeconfig string `json:"kubeconfig,omitempty"`
Verbosity pterm.LogLevel `json:"verbosity,omitempty"`
}

type Backup struct {
Enabled bool `json:"enabled"`
Directory string `json:"directory,omitempty"`
Revisions int `json:"revisions,omitempty"`
// State configuration options
type State struct {
// path of the state file
File string `json:"file,omitempty"`
History History `json:"history,omitempty"`
}

type History struct {
// set the maximum history size
Size int `json:"size"`
}

type State struct {
File string `json:"file,omitempty"`
History History `json:"history,omitempty"`
// Backup configuration options
type Backup struct {
// enable/disable the backup, defaults to true
Enabled bool `json:"enabled"`
// set the backup directory
Directory string `json:"directory,omitempty"`
// set the maximum backup revision count
Revisions int `json:"revisions,omitempty"`
}

type Global struct {
Kubeconfig string `json:"kubeconfig,omitempty"`
Verbosity pterm.LogLevel `json:"verbosity,omitempty"`
// Group configuration Options
type Group struct {
Items []GroupItem `json:"items"`
Selection Selection `json:"selection"`
}

type Config struct {
Global Global `json:"global,omitempty"`
Backup Backup `json:"backup,omitempty"`
State State `json:"state,omitempty"`
Groups []Group `json:"groups,omitempty"`
Sources []Source `json:"sources,omitempty"`
type GroupItem struct {
Name string `json:"name"`
Context Context `json:"context,omitempty"`
Sources []string `json:"sources"`
}

type Context struct {
Default string `json:"default"`
Selection Selection `json:"selection"`
}

type Selection struct {
Default string `json:"default"`
Sort string `json:"sort"`
}

// Source configuration options
type Source struct {
Items []SourceItem `json:"items"`
}

type SourceItem struct {
Name string `json:"name"`
Include []string `json:"include,omitempty"`
Exclude []string `json:"exclude"`
}

// Read reads the current config file and serialize it with koanf
Expand Down Expand Up @@ -114,13 +142,13 @@ func expandEnvironment(config *Config) {
config.Backup.Directory = os.ExpandEnv(config.Backup.Directory)
config.State.File = os.ExpandEnv(config.State.File)

for i, source := range config.Sources {
for i, source := range config.Source.Items {
for j, include := range source.Include {
source.Include[j] = os.ExpandEnv(include)
}
for j, exclude := range source.Exclude {
source.Exclude[j] = os.ExpandEnv(exclude)
}
config.Sources[i] = source
config.Source.Items[i] = source
}
}
64 changes: 41 additions & 23 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,36 @@ func Test_Read(t *testing.T) {
File: filepath.Join(xdg.StateHome, "kontext", "state.json"),
History: History{},
},
Groups: []Group{
{
Name: "default",
Context: "kind-local",
Sources: []string{
"default",
Group: Group{
Items: []GroupItem{
{
Name: "default",
Context: Context{
Default: "kind-local",
Selection: Selection{
Default: "kind-local",
Sort: "desc",
},
},
Sources: []string{
"default",
},
},
},
{
Name: "dev",
Context: "",
Sources: []string{
"dev",
{
Name: "dev",
Context: Context{},
Sources: []string{
"dev",
},
},
},
Selection: Selection{
Default: "dev",
Sort: "asc",
},
},
Sources: []Source{
{
Source: Source{
Items: []SourceItem{{
Name: "default",
Include: []string{
os.ExpandEnv("$HOME/.config/kontext/**/*.yaml"),
Expand All @@ -72,13 +84,14 @@ func Test_Read(t *testing.T) {
os.ExpandEnv("$HOME/.config/kontext/**/*prod*.yaml"),
},
},
{
Name: "dev",
Include: []string{
os.ExpandEnv("$HOME/.config/kontext/dev/**/*.yaml"),
},
Exclude: []string{
os.ExpandEnv("$HOME/.config/kontext/dev/**/*prod*.yaml"),
{
Name: "dev",
Include: []string{
os.ExpandEnv("$HOME/.config/kontext/dev/**/*.yaml"),
},
Exclude: []string{
os.ExpandEnv("$HOME/.config/kontext/dev/**/*prod*.yaml"),
},
},
},
},
Expand Down Expand Up @@ -130,8 +143,13 @@ func Test_Read(t *testing.T) {
Size: DefaultStateHistoryLimit,
},
},
Groups: []Group{},
Sources: []Source{},
Group: Group{
Items: []GroupItem{},
Selection: Selection{},
},
Source: Source{
Items: []SourceItem{},
},
},
wantErr: false,
},
Expand Down
Loading

0 comments on commit d9fc8c2

Please sign in to comment.