Skip to content

Commit

Permalink
Add documentation for using localstack (#30)
Browse files Browse the repository at this point in the history
* Add documentation for using localstack

* Add functionality to accept localstack port from cli
  • Loading branch information
PratikJethe committed Nov 8, 2023
1 parent 70274b4 commit 785809b
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 19 deletions.
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -54,6 +54,14 @@ For updating to latest version, use:
```console
cloudlens update
```
### Using Localstack
- Configure localstack server.
- Use our repo [cloud-lens-populator](https://github.com/one2nc/cloud-lens-populator) to setup and populate dummy data.
- To run cloudlens with localstack use `aws` sub-command with `-l` or `--localstack` flag
- By default cloudlens use port `4566`. Use `--port` flag to pass different port
```console
cloudlens aws --localstack --port 4000
```

## Features

Expand Down
7 changes: 6 additions & 1 deletion cmd/aws.go
@@ -1,6 +1,8 @@
package cmd

import (
"os"

"github.com/one2nc/cloudlens/internal"
"github.com/spf13/cobra"
)
Expand All @@ -17,9 +19,10 @@ func awsCommand() *cobra.Command {
}

command.Flags().StringVarP(&profile, "profile", "p", "default", "Read aws profile")

command.Flags().StringVarP(&region, "region", "r", "", "Read aws region")

command.Flags().BoolVarP(&useLocalStack, "localstack", "l", false, "Use localsatck instead of AWS")
command.Flags().StringVarP(&localStackPort, "port", "", "4566", "Read localstack port")

return &command
}
Expand All @@ -29,6 +32,8 @@ func selectAWS() {
cloudConfig.AWSConfig.Profile = profile
cloudConfig.AWSConfig.Region = region
cloudConfig.AWSConfig.UseLocalStack = useLocalStack
cloudConfig.AWSConfig.LocalStackPort = localStackPort

os.Setenv(internal.LOCALSTACK_PORT, cloudConfig.LocalStackPort)
initView()
}
1 change: 0 additions & 1 deletion cmd/constants.go
@@ -1,7 +1,6 @@
package cmd

const (
localstackEndpoint string = "http://localhost:4566"
AWS_PROFILE string = "AWS_PROFILE"
AWS_DEFAULT_REGION string = "AWS_DEFAULT_REGION"
)
2 changes: 1 addition & 1 deletion cmd/gcp.go
Expand Up @@ -25,7 +25,7 @@ func gcpCommand() *cobra.Command {

func selectGCP() {
cloudConfig.SelectedCloud = internal.GCP
os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", gcpCredFilePath)
os.Setenv(internal.GOOGLE_APPLICATION_CREDENTIALS, gcpCredFilePath)
cloudConfig.CredFilePath = gcpCredFilePath

initView()
Expand Down
12 changes: 6 additions & 6 deletions cmd/root.go
Expand Up @@ -13,12 +13,12 @@ import (
)

var (
profile, region, gcpCredFilePath string
useLocalStack bool
version = "dev"
commit = "dev"
date = "today"
rootCmd = &cobra.Command{
profile, region, gcpCredFilePath, localStackPort string
useLocalStack bool
version = "dev"
commit = "dev"
date = "today"
rootCmd = &cobra.Command{
Use: `cloudlens`,
Short: `cli for cloud services`,
Long: `cli for cloud services[aws,gcp]`,
Expand Down
11 changes: 10 additions & 1 deletion internal/aws/config.go
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"context"
"errors"
"fmt"
"os"
"strings"

Expand All @@ -13,6 +14,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/defaults"
"github.com/one2nc/cloudlens/internal"
"github.com/rs/zerolog/log"
"gopkg.in/ini.v1"
)
Expand Down Expand Up @@ -144,7 +146,7 @@ func GetProfiles() (profiles []string, err error) {
func GetLocalstackCfg(region string) (awsV2.Config, error) {
customResolver := awsV2.EndpointResolverFunc(func(service, region string) (awsV2.Endpoint, error) {
return awsV2.Endpoint{
URL: "http://localhost:4566",
URL: GetLocastackEndpoint(),
SigningRegion: region,
}, nil
})
Expand All @@ -158,3 +160,10 @@ func GetLocalstackCfg(region string) (awsV2.Config, error) {
}
return awsLSCfg, nil
}

func GetLocastackEndpoint() string {

port := os.Getenv(internal.LOCALSTACK_PORT)

return fmt.Sprintf("http://localhost:%v", port)
}
7 changes: 4 additions & 3 deletions internal/config/cloud_config.go
Expand Up @@ -7,9 +7,10 @@ type CloudConfig struct {
}

type AWSConfig struct {
Profile string
Region string
UseLocalStack bool
Profile string
Region string
UseLocalStack bool
LocalStackPort string
}
type GCPConfig struct {
CredFilePath string
Expand Down
4 changes: 0 additions & 4 deletions internal/config/constants.go
@@ -1,5 +1 @@
package config

const (
localstackEndpoint string = "http://localhost:4566"
)
7 changes: 6 additions & 1 deletion internal/constants.go
Expand Up @@ -96,5 +96,10 @@ const (
const (
FOLDER_TYPE string = "Folder"
FILE_TYPE string = "File"
NONE string = "-"
NONE string = "-"
)

const (
LOCALSTACK_PORT string = "LOCALSTACK_PORT"
GOOGLE_APPLICATION_CREDENTIALS string = "GOOGLE_APPLICATION_CREDENTIALS"
)
2 changes: 1 addition & 1 deletion internal/view/app.go
Expand Up @@ -157,7 +157,7 @@ func (a *App) handleAWS() {
func (a *App) handleGCP() error {
ctx := a.GetContext()
ctx = context.WithValue(ctx, internal.KeySelectedCloud, internal.GCP)
credFilePath := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
credFilePath := os.Getenv(internal.GOOGLE_APPLICATION_CREDENTIALS)
if credFilePath == "" {
go func() {
<-time.After(splashDelay)
Expand Down

0 comments on commit 785809b

Please sign in to comment.