2021-02-07 01:55:09 -08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type aws struct {
|
2021-11-26 01:37:33 -08:00
|
|
|
props properties
|
2021-02-07 01:55:09 -08:00
|
|
|
env environmentInfo
|
|
|
|
Profile string
|
|
|
|
Region string
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultUser = "default"
|
|
|
|
)
|
|
|
|
|
2021-11-26 01:37:33 -08:00
|
|
|
func (a *aws) init(props properties, env environmentInfo) {
|
2021-02-07 01:55:09 -08:00
|
|
|
a.props = props
|
|
|
|
a.env = env
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *aws) enabled() bool {
|
|
|
|
getEnvFirstMatch := func(envs ...string) string {
|
|
|
|
for _, env := range envs {
|
|
|
|
value := a.env.getenv(env)
|
|
|
|
if value != "" {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2021-02-18 09:47:53 -08:00
|
|
|
displayDefaultUser := a.props.getBool(DisplayDefault, true)
|
2021-02-07 01:55:09 -08:00
|
|
|
a.Profile = getEnvFirstMatch("AWS_VAULT", "AWS_PROFILE")
|
2021-02-18 09:47:53 -08:00
|
|
|
if !displayDefaultUser && a.Profile == defaultUser {
|
|
|
|
return false
|
|
|
|
}
|
2021-08-04 11:46:59 -07:00
|
|
|
a.Region = getEnvFirstMatch("AWS_REGION", "AWS_DEFAULT_REGION")
|
2021-02-07 01:55:09 -08:00
|
|
|
if a.Profile != "" && a.Region != "" {
|
|
|
|
return true
|
|
|
|
}
|
2021-03-08 12:01:28 -08:00
|
|
|
if a.Profile == "" && a.Region != "" && displayDefaultUser {
|
2021-02-07 01:55:09 -08:00
|
|
|
a.Profile = defaultUser
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
a.getConfigFileInfo()
|
2021-02-18 09:47:53 -08:00
|
|
|
if !displayDefaultUser && a.Profile == defaultUser {
|
|
|
|
return false
|
|
|
|
}
|
2021-02-07 01:55:09 -08:00
|
|
|
return a.Profile != ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *aws) getConfigFileInfo() {
|
|
|
|
configPath := a.env.getenv("AWS_CONFIG_FILE")
|
|
|
|
if configPath == "" {
|
|
|
|
configPath = fmt.Sprintf("%s/.aws/config", a.env.homeDir())
|
|
|
|
}
|
|
|
|
config := a.env.getFileContent(configPath)
|
|
|
|
configSection := "[default]"
|
|
|
|
if a.Profile != "" {
|
|
|
|
configSection = fmt.Sprintf("[profile %s]", a.Profile)
|
|
|
|
}
|
|
|
|
configLines := strings.Split(config, "\n")
|
|
|
|
var sectionActive bool
|
|
|
|
for _, line := range configLines {
|
|
|
|
if strings.HasPrefix(line, configSection) {
|
|
|
|
sectionActive = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if sectionActive && strings.HasPrefix(line, "region") {
|
|
|
|
a.Region = strings.TrimSpace(strings.Split(line, "=")[1])
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if a.Profile == "" && a.Region != "" {
|
|
|
|
a.Profile = defaultUser
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *aws) string() string {
|
|
|
|
segmentTemplate := a.props.getString(SegmentTemplate, "{{.Profile}}{{if .Region}}@{{.Region}}{{end}}")
|
|
|
|
template := &textTemplate{
|
|
|
|
Template: segmentTemplate,
|
|
|
|
Context: a,
|
2021-05-26 12:12:58 -07:00
|
|
|
Env: a.env,
|
2021-02-07 01:55:09 -08:00
|
|
|
}
|
2021-04-11 06:24:03 -07:00
|
|
|
text, err := template.render()
|
|
|
|
if err != nil {
|
|
|
|
return err.Error()
|
|
|
|
}
|
|
|
|
return text
|
2021-02-07 01:55:09 -08:00
|
|
|
}
|