Skip to content

Commit

Permalink
Merge pull request #15 from nanih98/fix/clipboard-profile
Browse files Browse the repository at this point in the history
Refactoring profile
  • Loading branch information
nanih98 authored Oct 6, 2022
2 parents 0c3d5d3 + fd5fd9a commit 0238ed5
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 25 deletions.
16 changes: 2 additions & 14 deletions aws/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/nanih98/aws-sso/dto"
"github.com/nanih98/aws-sso/logger"
"github.com/nanih98/aws-sso/utils"
"golang.design/x/clipboard"
)

func getProfiles(filepath string) []dto.Profile {
Expand All @@ -35,18 +34,7 @@ func getProfiles(filepath string) []dto.Profile {
return items
}

func clipBoard(log *logger.CustomLogger, profile string) {
err := clipboard.Init()
if err != nil {
log.Fatal(err)
}
clipboard.Write(clipboard.FmtText, []byte(fmt.Sprintf("export AWS_PROFILE='%s'", profile)))
log.Info(fmt.Sprintf("Profile %s copied to the clipboard, paste the command in your terminal to set the AWS_PROFILE env", profile))
}

func Profile(log *logger.CustomLogger) {
log.Info("Setting your AWS_PROFILE environment variable...")
log.Info("Reading file .aws/credentials")
credentialsPath := utils.GetUserHome(log) + "/.aws/credentials"

profiles := getProfiles(credentialsPath)
Expand Down Expand Up @@ -81,6 +69,6 @@ func Profile(log *logger.CustomLogger) {
return
}

// Return the selected profile to the clipboard
clipBoard(log, profiles[i].Key)
log.Info(fmt.Sprintf("Execute $ export AWS_PROFILE='%s' or see this following README...", profiles[i].Key))
utils.RenderREADME("https://raw.githubusercontent.com/nanih98/aws-sso/main/docs/profile.md")
}
3 changes: 2 additions & 1 deletion cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ func Usage() *cobra.Command {
Short: "Usage will print the README.md of the project",
Long: "Usage will print the readme of the project. You need internet connection because it will download the README from the github repository",
Run: func(cmd *cobra.Command, args []string) {
utils.RenderREADME()
var url string = "https://raw.githubusercontent.com/nanih98/aws-sso/main/docs/usage.md"
utils.RenderREADME(url)
},
}
}
23 changes: 23 additions & 0 deletions docs/profile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
```bash
$ aws-sso profile
```

Once your credentials are configured (with the previous symlink) inside your **.aws/credentials** file, this command will read and promt each profile in an interactive terminal list. Then, the profile you selected, will be copied in your clipboard and then you will execute the command **export AWS_PROFILE=yourprofile** You can export the variable directly from a child process. Something like this issue <https://stackoverflow.com/questions/1506010/how-to-use-export-with-python-on-linux>

> Note: in the interactive terminal, you can search for your profiles using SHIFT+/
This command at the end will do nothing, since we can export a variable from a child process. So, other solutions once you have all your credentials in inside `.aws/credentials` is the following:
```bash
aws-profile () {
PROFILE=$(cat ~/.aws/credentials|grep "^\["|sed "s/]$//"|sed "s/^\[//"| fzf)
export AWS_PROFILE=$PROFILE
}
```
Save this function inside your `.bashrc` or `.zshrc`.

* Requirements: `fzf` (`brew install fzf`)

Is an interactive menu terminal to change `AWS_PROFILE` at the moment in your terminal.

https://raw.githubusercontent.com/nanih98/aws-sso/main/docs/profile.md

10 changes: 3 additions & 7 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ $ aws-sso config --startURL "https://myaccount.awsapps.com/start" --region us-ea
You can save more accounts:

```bash
aws-sso config --startURL "https://myaccount2.awsapps.com/start" --region eu-west-1 --profileName="myaccount2"
$ aws-sso config --startURL "https://myaccount2.awsapps.com/start" --region eu-west-1 --profileName="myaccount2"
```
* **startURL:** The AWS SSO url (get it from the AWS CONSOLE where you have your configured SSO). *Is required*
* **region:** region of AWS of your SSO resource. The region where you created the SSO inside your AWS account. *Is required*
* **profileName:** the name of the account, for example, the name of the company or the platform. *Is required*

> **NOTE:** this configuration will be saved inside your home `.aws-sso/profilename.json`
2. Start the application

This will open the browser where you are login with your external IDP provider and will start getting the aws credentials for each account your user have access. Will write all the access key, secret key and token inside your **.aws/credentials** file.
Expand Down Expand Up @@ -51,9 +49,7 @@ This will create a symlink in your home `.aws/`. The selected profile will be a

4. Set the profile

Once your credentials are configured (with the previous symlink) inside your **.aws/credentials** file, this command will read and promt each profile in an interactive terminal list. Then, the profile you selected, will be copied in your clipboard and then you will execute the command **export AWS_PROFILE=yourprofile** You can export the variable directly from a child process. Something like this issue <https://stackoverflow.com/questions/1506010/how-to-use-export-with-python-on-linux>

> Note: in the interactive terminal, you can search for your profiles using SHIFT+/
Take a look [profile](./profile.md)

5. Usage

Expand All @@ -63,7 +59,7 @@ This will show this file in a terminal markdown render.
$ aws-sso usage
```

1. Version
6. Version

Get your aws-sso version installed in your local.

Expand Down
4 changes: 1 addition & 3 deletions utils/readme.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import (
"github.com/charmbracelet/glamour"
)

var url string = "https://raw.githubusercontent.com/nanih98/aws-sso/main/docs/usage.md"

func RenderREADME() {
func RenderREADME(url string) {
resp, err := http.Get(url)

if err != nil {
Expand Down

0 comments on commit 0238ed5

Please sign in to comment.