Skip to content

Commit

Permalink
- Add feature to switch pages thorugh prompt
Browse files Browse the repository at this point in the history
- Add config params to hold active view, profile and region
- Fix viewing flash msg under crumbs
  • Loading branch information
RamanaReddy0M committed Feb 6, 2023
1 parent 4fdaaa3 commit 599a765
Show file tree
Hide file tree
Showing 15 changed files with 1,138 additions and 15 deletions.
7 changes: 5 additions & 2 deletions go.mod
Expand Up @@ -15,7 +15,10 @@ require (
github.com/spf13/cobra v1.6.1
)

require github.com/atotto/clipboard v0.1.4
require (
github.com/adrg/xdg v0.4.0
github.com/atotto/clipboard v0.1.4
)

require (
github.com/aws/aws-sdk-go-v2/credentials v1.13.8 // indirect
Expand All @@ -41,5 +44,5 @@ require (
golang.org/x/sys v0.3.0 // indirect
golang.org/x/term v0.1.0 // indirect
golang.org/x/text v0.4.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v2 v2.4.0
)
7 changes: 7 additions & 0 deletions go.sum
@@ -1,3 +1,5 @@
github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls=
github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E=
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/aws/aws-sdk-go v1.44.177 h1:ckMJhU5Gj+4Rta+bJIUiUd7jvHom84aim3zkGPblq0s=
Expand Down Expand Up @@ -86,7 +88,9 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand All @@ -105,6 +109,7 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220318055525-2edf467146b5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand All @@ -128,9 +133,11 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
20 changes: 20 additions & 0 deletions internal/config/cloudlens.go
@@ -0,0 +1,20 @@
package config

type Active struct {
Profile string `yaml:"profile"`
Region string `yaml:"region"`
View string `yaml:"view"`
}

type Cloudlens struct {
EnableMouse bool `yaml:"enableMouse"`
Headless bool `yaml:"headless"`
Logoless bool `yaml:"logoless"`
Crumbsless bool `yaml:"crumbsless"`
Active *Active `yaml:"active"`
}

// NewCloudlens create a new Cloudlens configuration.
func NewCloudlens() *Cloudlens {
return &Cloudlens{}
}
70 changes: 69 additions & 1 deletion internal/config/config.go
Expand Up @@ -4,28 +4,96 @@ import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strings"

"github.com/adrg/xdg"
awsV2 "github.com/aws/aws-sdk-go-v2/aws"
awsV2Config "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v2"
)

// CloudlensConfig represents Cloudlens configuration dir env var.
const CloudlensConfig = "CLOUDLENSCONFIG"

var (
//CloudlensConfigFile represents config file location.
CloudlensConfigFile = filepath.Join(CloudlensHome(), "config.yml")
)

type Config struct {
Cloudlens *Cloudlens `yaml:"cloudlens"`
// List of profiles in (~/.aws/credentials)
Profiles []string
AwsConfig awsV2.Config
}

// CloudlensHome returns Cloudlens configs home directory.
func CloudlensHome() string {
if env := os.Getenv(CloudlensConfig); env != "" {
log.Debug().Msg("env CL: " + env)
return env
}

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

if err != nil {
log.Fatal().Err(err).Msg("Unable to create configuration directory for cloudlens")
}

return xdgCLHome
}

// Load K9s configuration from file.
func (c *Config) Load(path string) error {
f, err := os.ReadFile(path)
if err != nil {
return err
}
c.Cloudlens = NewCloudlens()

var cfg Config
if err := yaml.Unmarshal(f, &cfg); err != nil {
return err
}
if cfg.Cloudlens != nil {
c.Cloudlens = cfg.Cloudlens
}
return nil
}

// Save configuration to disk.
func (c *Config) Save() error {
//c.Validate()

return c.SaveFile(CloudlensConfigFile)
}

// SaveFile K9s configuration to disk.
func (c *Config) SaveFile(path string) error {
EnsurePath(path, DefaultDirMod)
cfg, err := yaml.Marshal(c)
if err != nil {
log.Error().Msgf("[Config] Unable to save cloudlens config file: %v", err)
return err
}
log.Info().Msg(fmt.Sprintf("Config Path: %v", path))
return os.WriteFile(path, cfg, 0644)
}

var config Config

func GetSession(profile, region string, awsCfg awsV2.Config) (*session.Session, error) {
//crds, _ := awsCfg.Credentials.Retrieve(context.TODO())
sess, err := session.NewSessionWithOptions(session.Options{Config: aws.Config{
//TODO: remove hardcoded enpoint
//Endpoint: aws.String("http://localhost:4566"),
Endpoint: aws.String("http://localhost:4566"),
Region: aws.String(region),
//Credentials: credentials.NewStaticCredentials(crds.AccessKeyID, crds.SecretAccessKey, ""),
S3ForcePathStyle: aws.Bool(true),
Expand Down
30 changes: 30 additions & 0 deletions internal/config/helpers.go
@@ -0,0 +1,30 @@
package config

import (
"os"
"path/filepath"

"github.com/rs/zerolog/log"
)

const (
// DefaultDirMod default unix perms for k9s directory.
DefaultDirMod os.FileMode = 0755
// DefaultFileMod default unix perms for k9s files.
DefaultFileMod os.FileMode = 0600
)

// EnsurePath ensures a directory exist from the given path.
func EnsurePath(path string, mod os.FileMode) {
dir := filepath.Dir(path)
EnsureFullPath(dir, mod)
}

// EnsureFullPath ensures a directory exist from the given path.
func EnsureFullPath(path string, mod os.FileMode) {
if _, err := os.Stat(path); os.IsNotExist(err) {
if err = os.MkdirAll(path, mod); err != nil {
log.Fatal().Msgf("Unable to create dir %q %v", path, err)
}
}
}

0 comments on commit 599a765

Please sign in to comment.