-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring and add account list if account is not specified
- Loading branch information
Showing
7 changed files
with
191 additions
and
148 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
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,79 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
const defaultAWSCredsFile = ".aws/credentials" | ||
|
||
func DefaultAWSCredsFilePath() string { | ||
homedir, _ := os.UserHomeDir() | ||
return filepath.Join(homedir, defaultAWSCredsFile) | ||
} | ||
|
||
func awsListAvailableProfiles(credsFile string) ([]string, error) { | ||
f, err := os.Open(credsFile) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to open AWS creds file %v: %w", credsFile, err) | ||
} | ||
profiles := []string{} | ||
scanner := bufio.NewScanner(f) | ||
|
||
for scanner.Scan() { | ||
line := strings.TrimSpace(scanner.Text()) | ||
if strings.HasPrefix(line, "[") { | ||
profile := strings.TrimRight(strings.TrimLeft(line, "["), "]") | ||
profiles = append(profiles, profile) | ||
} | ||
} | ||
return profiles, nil | ||
} | ||
|
||
func awsReadCredentials(credsFile string) ([]awsCreds, error) { | ||
f, err := os.Open(credsFile) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to open AWS creds file %v: %w", credsFile, err) | ||
} | ||
creds := []awsCreds{} | ||
scanner := bufio.NewScanner(f) | ||
|
||
var section []string | ||
for scanner.Scan() { | ||
line := strings.TrimSpace(scanner.Text()) | ||
if strings.HasPrefix(line, "#") { | ||
continue | ||
} else if strings.HasPrefix(line, "[") { | ||
if len(section) > 0 { | ||
creds = append(creds, awsParseCredentials(section)) | ||
} | ||
section = []string{line} | ||
} else if line != "" { | ||
section = append(section, line) | ||
} | ||
} | ||
creds = append(creds, awsParseCredentials(section)) | ||
return creds, nil | ||
} | ||
|
||
func awsParseCredentials(section []string) awsCreds { | ||
creds := awsCreds{ | ||
profile: strings.TrimSuffix(strings.TrimPrefix(section[0], "["), "]"), | ||
} | ||
for _, line := range section[1:] { | ||
pair := strings.Split(line, "=") | ||
if len(pair) > 1 { | ||
switch strings.TrimSpace(pair[0]) { | ||
case "aws_access_key_id": | ||
creds.accessKeyID = strings.TrimSpace(pair[1]) | ||
case "aws_secret_access_key": | ||
creds.secretAccessKey = strings.TrimSpace(pair[1]) | ||
} | ||
} | ||
|
||
} | ||
return creds | ||
} |
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,17 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
) | ||
|
||
// RunCommand runs the given command and prints the output | ||
func RunCommand(command string, args ...string) error { | ||
cmd := exec.Command(command, args...) | ||
output, err := cmd.CombinedOutput() | ||
fmt.Print(string(output)) | ||
if err != nil { | ||
return fmt.Errorf("failed command: %v %w", cmd, err) | ||
} | ||
return nil | ||
} |
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,30 @@ | ||
package main | ||
|
||
import ( | ||
"runtime" | ||
"testing" | ||
) | ||
|
||
func TestRunCommand(t *testing.T) { | ||
var err error | ||
fooCmd := "foo" | ||
err = RunCommand(fooCmd) | ||
if err == nil { | ||
t.Fatal("expected foo to fail but it succeeded") | ||
} | ||
|
||
emptyCmd := " " | ||
err = RunCommand(emptyCmd) | ||
if err == nil { | ||
t.Fatal("expected foo to fail but it succeeded") | ||
} | ||
|
||
gcloudCmd := "ls -l" | ||
if runtime.GOOS == "windows" { | ||
gcloudCmd = "dir" | ||
} | ||
err = RunCommand(gcloudCmd) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
Oops, something went wrong.