(ctx context.Context, id string)
| 14 | ) |
| 15 | |
| 16 | func ImportKeys(ctx context.Context, id string) error { |
| 17 | var keys []scalingo.Key |
| 18 | |
| 19 | c, err := config.ScalingoAuthClient(ctx) |
| 20 | if err != nil { |
| 21 | return errors.Wrapf(ctx, err, "fail to get Scalingo client") |
| 22 | } |
| 23 | |
| 24 | integration, err := c.SCMIntegrationsShow(ctx, id) |
| 25 | if err != nil { |
| 26 | return errors.Wrapf(ctx, err, "not linked SCM integration or unknown SCM integration") |
| 27 | } |
| 28 | |
| 29 | importedKeys, err := c.SCMIntegrationsImportKeys(ctx, id) |
| 30 | if err != nil { |
| 31 | return errors.Wrapf(ctx, err, "fail to import keys") |
| 32 | } |
| 33 | |
| 34 | nbrKeys := len(importedKeys) |
| 35 | if nbrKeys == 0 { |
| 36 | alreadyImportedKeys, err := keysContainsName(ctx, c, integration.SCMType.Str()) |
| 37 | if err != nil { |
| 38 | return errors.Wrapf(ctx, err, "fail to list already imported keys") |
| 39 | } |
| 40 | alreadyImportedKeysLength := len(alreadyImportedKeys) |
| 41 | |
| 42 | pluralKey := "" |
| 43 | if alreadyImportedKeysLength > 1 { |
| 44 | pluralKey = "s" |
| 45 | } |
| 46 | |
| 47 | io.Statusf("0 key imported from %s.\n", scalingo.SCMTypeDisplay[integration.SCMType]) |
| 48 | if alreadyImportedKeysLength == 0 { |
| 49 | io.Infof("No public key is available in your %s account\n", scalingo.SCMTypeDisplay[integration.SCMType]) |
| 50 | return nil |
| 51 | } |
| 52 | io.Info() |
| 53 | |
| 54 | io.Statusf( |
| 55 | "%d key%s have already been imported from %s:\n", |
| 56 | alreadyImportedKeysLength, pluralKey, scalingo.SCMTypeDisplay[integration.SCMType], |
| 57 | ) |
| 58 | keys = alreadyImportedKeys |
| 59 | } else { |
| 60 | keys = importedKeys |
| 61 | } |
| 62 | |
| 63 | t := tablewriter.NewWriter(os.Stdout) |
| 64 | t.Header([]string{"Name", "Content"}) |
| 65 | for _, k := range keys { |
| 66 | t.Append([]string{k.Name, k.Content[0:20] + "..." + k.Content[len(k.Content)-30:]}) |
| 67 | } |
| 68 | t.Render() |
| 69 | |
| 70 | if nbrKeys != 0 { |
| 71 | pluralKey := "" |
| 72 | if nbrKeys > 1 { |
| 73 | pluralKey = "s" |
no test coverage detected