-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
71b0da4
commit 80ed68e
Showing
13 changed files
with
412 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package config | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"sync" | ||
|
||
"github.com/rs/zerolog/log" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
// CloudlensAlias manages Cloudlens aliases. | ||
var CloudlensAlias = filepath.Join(CloudlensHome(), "alias.yml") | ||
|
||
// Alias tracks shortname to GVR mappings. | ||
type Alias map[string]string | ||
|
||
// ShortNames represents a collection of shortnames for aliases. | ||
type ShortNames map[string][]string | ||
|
||
// Aliases represents a collection of aliases. | ||
type Aliases struct { | ||
Alias Alias `yaml:"alias"` | ||
mx sync.RWMutex | ||
} | ||
|
||
// NewAliases return a new alias. | ||
func NewAliases() *Aliases { | ||
return &Aliases{ | ||
Alias: make(Alias, 50), | ||
} | ||
} | ||
|
||
// Keys returns all aliases keys. | ||
func (a *Aliases) Keys() []string { | ||
a.mx.RLock() | ||
defer a.mx.RUnlock() | ||
|
||
ss := make([]string, 0, len(a.Alias)) | ||
for k := range a.Alias { | ||
ss = append(ss, k) | ||
} | ||
return ss | ||
} | ||
|
||
// ShortNames return all shortnames. | ||
func (a *Aliases) ShortNames() ShortNames { | ||
a.mx.RLock() | ||
defer a.mx.RUnlock() | ||
|
||
m := make(ShortNames, len(a.Alias)) | ||
for alias, res := range a.Alias { | ||
if v, ok := m[res]; ok { | ||
m[res] = append(v, alias) | ||
} else { | ||
m[res] = []string{alias} | ||
} | ||
} | ||
|
||
return m | ||
} | ||
|
||
// Clear remove all aliases. | ||
func (a *Aliases) Clear() { | ||
a.mx.Lock() | ||
defer a.mx.Unlock() | ||
|
||
for k := range a.Alias { | ||
delete(a.Alias, k) | ||
} | ||
} | ||
|
||
// Get retrieves an alias. | ||
func (a *Aliases) Get(k string) (string, bool) { | ||
a.mx.RLock() | ||
defer a.mx.RUnlock() | ||
|
||
v, ok := a.Alias[k] | ||
return v, ok | ||
} | ||
|
||
// Define declares a new alias. | ||
func (a *Aliases) Define(resource string, aliases ...string) { | ||
a.mx.Lock() | ||
defer a.mx.Unlock() | ||
|
||
for _, alias := range aliases { | ||
if _, ok := a.Alias[alias]; ok { | ||
continue | ||
} | ||
a.Alias[alias] = resource | ||
} | ||
} | ||
|
||
// Load Cloudlens aliases. | ||
func (a *Aliases) Load() error { | ||
a.loadDefaultAliases() | ||
return a.LoadFileAliases(CloudlensAlias) | ||
} | ||
|
||
// LoadFileAliases loads alias from a given file. | ||
func (a *Aliases) LoadFileAliases(path string) error { | ||
f, err := os.ReadFile(path) | ||
if err == nil { | ||
var aa Aliases | ||
if err := yaml.Unmarshal(f, &aa); err != nil { | ||
return err | ||
} | ||
|
||
a.mx.Lock() | ||
defer a.mx.Unlock() | ||
for k, v := range aa.Alias { | ||
a.Alias[k] = v | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (a *Aliases) declare(key string, aliases ...string) { | ||
a.Alias[key] = key | ||
for _, alias := range aliases { | ||
a.Alias[alias] = key | ||
} | ||
} | ||
|
||
func (a *Aliases) loadDefaultAliases() { | ||
a.mx.Lock() | ||
defer a.mx.Unlock() | ||
|
||
a.declare("ec2", "Ec2", "EC2") | ||
a.declare("s3", "S3") | ||
a.declare("sg", "SG") | ||
|
||
a.declare("help", "h", "?") | ||
a.declare("quit", "q", "q!", "Q") | ||
a.declare("aliases", "alias", "a") | ||
} | ||
|
||
// Save alias to disk. | ||
func (a *Aliases) Save() error { | ||
log.Debug().Msg("[Config] Saving Aliases...") | ||
return a.SaveAliases(CloudlensAlias) | ||
} | ||
|
||
// SaveAliases saves aliases to a given file. | ||
func (a *Aliases) SaveAliases(path string) error { | ||
EnsurePath(path, DefaultDirMod) | ||
cfg, err := yaml.Marshal(a) | ||
if err != nil { | ||
return err | ||
} | ||
return os.WriteFile(path, cfg, 0644) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package dao | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"sort" | ||
|
||
"github.com/one2nc/cloud-lens/internal" | ||
"github.com/one2nc/cloud-lens/internal/config" | ||
"github.com/one2nc/cloud-lens/internal/render" | ||
) | ||
|
||
var _ Accessor = (*Alias)(nil) | ||
|
||
// Alias tracks standard and custom command aliases. | ||
type Alias struct { | ||
*config.Aliases | ||
} | ||
|
||
// NewAlias returns a new set of aliases. | ||
func NewAlias() *Alias { | ||
a := Alias{Aliases: config.NewAliases()} | ||
|
||
return &a | ||
} | ||
|
||
// Check verifies an alias is defined for this command. | ||
func (a *Alias) Check(cmd string) bool { | ||
_, ok := a.Aliases.Get(cmd) | ||
return ok | ||
} | ||
|
||
// List returns a collection of aliases. | ||
func (a *Alias) List(ctx context.Context) ([]Object, error) { | ||
aa, ok := ctx.Value(internal.KeyAliases).(*Alias) | ||
if !ok { | ||
return nil, fmt.Errorf("expecting *Alias but got %T", ctx.Value(internal.KeyAliases)) | ||
} | ||
m := aa.ShortNames() | ||
oo := make([]Object, 0, len(m)) | ||
for res, aliases := range m { | ||
sort.StringSlice(aliases).Sort() | ||
oo = append(oo, render.AliasRes{Resource: res, Aliases: aliases}) | ||
} | ||
|
||
return oo, nil | ||
} | ||
|
||
// AsResource returns a matching resource if it exists. | ||
func (a *Alias) AsResource(cmd string) (string, bool) { | ||
res, ok := a.Aliases.Get(cmd) | ||
if ok { | ||
return res, true | ||
} | ||
return "", false | ||
} | ||
|
||
// Get fetch a resource. | ||
func (a *Alias) Get(_ context.Context, _ string) (Object, error) { | ||
return nil, errors.New("NYI!!") | ||
} | ||
|
||
// Ensure makes sure alias are loaded. | ||
func (a *Alias) Ensure() (config.Alias, error) { | ||
|
||
return a.Alias, a.load() | ||
} | ||
|
||
func (a *Alias) load() error { | ||
return a.Load() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package render | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// Alias renders a aliases to screen. | ||
type Alias struct { | ||
} | ||
|
||
// Header returns a header row. | ||
func (Alias) Header(ns string) Header { | ||
return Header{ | ||
HeaderColumn{Name: "RESOURCE"}, | ||
HeaderColumn{Name: "COMMAND"}, | ||
} | ||
} | ||
|
||
// Render renders a K8s resource to screen. | ||
// BOZO!! Pass in a row with pre-alloc fields?? | ||
func (Alias) Render(o interface{}, ns string, r *Row) error { | ||
a, ok := o.(AliasRes) | ||
if !ok { | ||
return fmt.Errorf("expected AliasRes, but got %T", o) | ||
} | ||
|
||
r.ID = a.Resource | ||
r.Fields = append(r.Fields, | ||
a.Resource, | ||
strings.Join(a.Aliases, ","), | ||
) | ||
|
||
return nil | ||
} | ||
|
||
// ---------------------------------------------------------------------------- | ||
// Helpers... | ||
|
||
// AliasRes represents an alias resource. | ||
type AliasRes struct { | ||
Resource string | ||
Aliases []string | ||
} | ||
|
||
// DeepCopyObject returns a container copy. | ||
func (a AliasRes) DeepCopyObject() interface{} { | ||
return a | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package dialog | ||
|
||
const confirmKey = "confirm" |
Oops, something went wrong.