-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from synfinatic/complete
Add auto-complete support
- Loading branch information
Showing
20 changed files
with
277 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
linters: | ||
enable: | ||
- asciicheck | ||
- ineffassign | ||
- gocyclo | ||
- dupl | ||
# - funlen | ||
- gofmt | ||
- gosec | ||
- misspell | ||
- whitespace | ||
# - unparam |
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
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,135 @@ | ||
package main | ||
|
||
/* | ||
* AWS SSO CLI | ||
* Copyright (c) 2021 Aaron Turner <aturner at synfin dot net> | ||
* | ||
* This program is free software: you can redistribute it | ||
* and/or modify it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or with the authors permission any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import ( | ||
"io/ioutil" | ||
|
||
"github.com/goccy/go-yaml" | ||
"github.com/posener/complete" | ||
"github.com/synfinatic/aws-sso-cli/sso" | ||
"github.com/synfinatic/aws-sso-cli/utils" | ||
) | ||
|
||
type Predictor struct { | ||
configFile string | ||
accountids []string | ||
roles []string | ||
arns []string | ||
} | ||
|
||
// AvailableAwsRegions lists all the AWS regions that AWS provides | ||
// https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html#concepts-available-regions | ||
var AvailableAwsRegions []string = []string{ | ||
"us-east-1", | ||
"us-east-2", | ||
"us-west-1", | ||
"us-west-2", | ||
"af-south-1", | ||
"ap-east-1", | ||
"ap-south-1", | ||
"ap-northeast-1", | ||
"ap-northeast-2", | ||
"ap-northeast-3", | ||
"ap-southeast-1", | ||
"ap-southeast-2", | ||
"ap-northeast-1", | ||
"ca-central-1", | ||
"eu-central-1", | ||
"eu-west-1", | ||
"eu-west-2", | ||
"eu-south-1", | ||
"eu-west-3", | ||
"eu-north-1", | ||
"me-south-1", | ||
"sa-east-1", | ||
} | ||
|
||
// NewPredictor loads our cache file (if exists) and loads the values | ||
func NewPredictor(cacheFile, configFile string) *Predictor { | ||
p := Predictor{ | ||
configFile: configFile, | ||
} | ||
c, err := sso.OpenCache(cacheFile, &sso.Settings{}) | ||
if err != nil { | ||
return &p | ||
} | ||
|
||
uniqueRoles := map[string]bool{} | ||
|
||
for i, a := range c.Roles.Accounts { | ||
id, _ := utils.AccountIdToString(i) | ||
p.accountids = append(p.accountids, id) | ||
for role, r := range a.Roles { | ||
p.arns = append(p.arns, r.Arn) | ||
uniqueRoles[role] = true | ||
} | ||
} | ||
|
||
for k := range uniqueRoles { | ||
p.roles = append(p.roles, k) | ||
} | ||
|
||
return &p | ||
} | ||
|
||
// FieldListComplete returns a completor for the `list` fields | ||
func (p *Predictor) FieldListComplete() complete.Predictor { | ||
set := []string{} | ||
for f := range allListFields { | ||
set = append(set, f) | ||
} | ||
|
||
return complete.PredictSet(set...) | ||
} | ||
|
||
// AccountComplete returns a list of all the valid AWS Accounts we have in the cache | ||
func (p *Predictor) AccountComplete() complete.Predictor { | ||
return complete.PredictSet(p.accountids...) | ||
} | ||
|
||
// RoleComplete returns a list of all the valid AWS Roles we have in the cache | ||
func (p *Predictor) RoleComplete() complete.Predictor { | ||
return complete.PredictSet(p.roles...) | ||
} | ||
|
||
// ArnComplete returns a list of all the valid AWS Role ARNs we have in the cache | ||
func (p *Predictor) ArnComplete() complete.Predictor { | ||
return complete.PredictSet(p.arns...) | ||
} | ||
|
||
// RegionsComplete returns a list of all the valid AWS Regions | ||
func (p *Predictor) RegionComplete() complete.Predictor { | ||
return complete.PredictSet(AvailableAwsRegions...) | ||
} | ||
|
||
// SsoComplete returns a list of the valid AWS SSO Instances | ||
func (p *Predictor) SsoComplete() complete.Predictor { | ||
ssos := []string{} | ||
s := sso.Settings{} | ||
|
||
if config, err := ioutil.ReadFile(p.configFile); err == nil { | ||
if err = yaml.Unmarshal(config, &s); err == nil { | ||
for sso := range s.SSO { | ||
ssos = append(ssos, sso) | ||
} | ||
} | ||
} | ||
return complete.PredictSet(ssos...) | ||
} |
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
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
Oops, something went wrong.