|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
4 | 5 | "context"
|
5 | 6 | "crypto/md5"
|
6 | 7 | "encoding/hex"
|
@@ -126,12 +127,14 @@ func main(){
|
126 | 127 | func writeCachedFile(awsSsoCachePath, awsSSOProfileName string, credentialProcessJson CredentialProcessJson) error {
|
127 | 128 | cachedFileName := getCachedFileName(awsSSOProfileName)
|
128 | 129 | cachedFilePath := filepath.Join(awsSsoCachePath, cachedFileName)
|
129 |
| - |
130 |
| - prettyJson, err := json.MarshalIndent(credentialProcessJson, "", " ") |
| 130 | + buffer, err := jsonEncode(credentialProcessJson) |
131 | 131 | if err != nil {
|
132 | 132 | return err
|
133 | 133 | }
|
134 |
| - err = ioutil.WriteFile(cachedFilePath, prettyJson, 0600) |
| 134 | + if err != nil { |
| 135 | + return err |
| 136 | + } |
| 137 | + err = ioutil.WriteFile(cachedFilePath, buffer.Bytes(), 0600) |
135 | 138 | if err != nil {
|
136 | 139 | return err
|
137 | 140 | }
|
@@ -170,14 +173,23 @@ func getCachedFileName(awsSSOProfileName string) string {
|
170 | 173 | }
|
171 | 174 |
|
172 | 175 | func printProfile(credentialProcessJson CredentialProcessJson) {
|
173 |
| - prettyJson, err := json.MarshalIndent(credentialProcessJson, "", " ") |
| 176 | + buffer, err := jsonEncode(credentialProcessJson) |
174 | 177 | if err != nil {
|
175 |
| - log.Fatal().Err(err).Msg("marshalling json exploded") |
| 178 | + log.Fatal().Err(err).Msg("encoding json exploded") |
176 | 179 | }
|
177 |
| - |
178 |
| - fmt.Println(string(prettyJson)) |
| 180 | + fmt.Printf("%s", buffer.String()) |
179 | 181 | }
|
180 | 182 |
|
| 183 | +func jsonEncode(credentialProcessJson CredentialProcessJson) (*bytes.Buffer, error) { |
| 184 | + buffer := new(bytes.Buffer) |
| 185 | + encoder := json.NewEncoder(buffer) |
| 186 | + encoder.SetIndent("", " ") |
| 187 | + err := encoder.Encode(credentialProcessJson) |
| 188 | + if err != nil { |
| 189 | + return nil, err |
| 190 | + } |
| 191 | + return buffer, nil |
| 192 | +} |
181 | 193 |
|
182 | 194 | func getSsoRoleCredentials(profile Profile, awsSSOCredential AWSSSOCredential) (CredentialProcessJson, error) {
|
183 | 195 |
|
|
0 commit comments