Skip to content

Commit

Permalink
fix merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
RamanaReddy0M committed Feb 7, 2023
1 parent 71b0da4 commit 80ed68e
Show file tree
Hide file tree
Showing 13 changed files with 412 additions and 33 deletions.
4 changes: 4 additions & 0 deletions internal/aws/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ type EC2Resp struct {
MonitoringState string
LaunchTime string
}

type S3Object struct {
Name, ObjectType, LastModified, Size, StorageClass string
}
154 changes: 154 additions & 0 deletions internal/config/alias.go
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)
}
4 changes: 2 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ type Config struct {
// CloudlensHome returns Cloudlens configs home directory.
func CloudlensHome() string {
if env := os.Getenv(CloudlensConfig); env != "" {
log.Debug().Msg("env CL: " + env)
//log.Debug().Msg("env CL: " + env)
return env
}

xdgCLHome, err := xdg.ConfigFile("cloudlens")
log.Debug().Msg("xdgsclhome: " + xdgCLHome)
//log.Debug().Msg("xdgsclhome: " + xdgCLHome)

if err != nil {
log.Fatal().Err(err).Msg("Unable to create configuration directory for cloudlens")
Expand Down
72 changes: 72 additions & 0 deletions internal/dao/alias.go
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()
}
14 changes: 6 additions & 8 deletions internal/dao/buck_obj.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (bo *BObj) List(ctx context.Context) ([]Object, error) {
log.Info().Msg(fmt.Sprintf("In Dao Folder Name: %v", fn))
bucketInfo := aws.GetInfoAboutBucket(*sess, bucketName, "/", fn)
folderArrayInfo, fileArrayInfo := getBucLevelInfo(bucketInfo)
var s3Objects []S3Object
var s3Objects []aws.S3Object
if len(folderArrayInfo) != 0 || len(fileArrayInfo) != 0 {
s3Objects = setFoldersAndFIles(bucketInfo.CommonPrefixes, bucketInfo.Contents)
}
Expand Down Expand Up @@ -59,16 +59,14 @@ func getBucLevelInfo(bucketInfo *s3.ListObjectsV2Output) ([]string, []string) {
return folderArrayInfo, fileArrayInfo
}

type S3Object struct {
Name, ObjectType, LastModified, Size, StorageClass string
}

func setFoldersAndFIles(Folder []*s3.CommonPrefix, File []*s3.Object) []S3Object {
var s3Objects []S3Object

func setFoldersAndFIles(Folder []*s3.CommonPrefix, File []*s3.Object) []aws.S3Object {
var s3Objects []aws.S3Object
indx := 0
for _, bi := range Folder {
keyA := strings.Split(*bi.Prefix, "/")
o := S3Object{
o := aws.S3Object{
Name: keyA[len(keyA)-2],
ObjectType: "Folder",
LastModified: "-",
Expand All @@ -81,7 +79,7 @@ func setFoldersAndFIles(Folder []*s3.CommonPrefix, File []*s3.Object) []S3Object

for _, fi := range File {
keyA := strings.Split(*fi.Key, "/")
o := S3Object{
o := aws.S3Object{
Name: keyA[len(keyA)-1],
ObjectType: "File",
LastModified: fi.LastModified.String(),
Expand Down
1 change: 1 addition & 0 deletions internal/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ const (
BucketName ContextKey = "bucket_name"
ObjectName ContextKey = "object_name"
FolderName ContextKey = "folder_name"
KeyAliases ContextKey = "aliases"
)
49 changes: 49 additions & 0 deletions internal/render/alias.go
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
}
4 changes: 2 additions & 2 deletions internal/render/buck_obj.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"

"github.com/derailed/tview"
"github.com/one2nc/cloud-lens/internal/dao"
"github.com/one2nc/cloud-lens/internal/aws"
)

type BObj struct {
Expand All @@ -21,7 +21,7 @@ func (obj BObj) Header() Header {
}

func (obj BObj) Render(o interface{}, ns string, row *Row) error {
s3Resp, ok := o.(dao.S3Object)
s3Resp, ok := o.(aws.S3Object)
if !ok {
return fmt.Errorf("expected S3Resp, but got %T", o)
}
Expand Down
3 changes: 3 additions & 0 deletions internal/ui/dialog/confirm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package dialog

const confirmKey = "confirm"
Loading

0 comments on commit 80ed68e

Please sign in to comment.