GetCurrentResourceAndDatabase returns the current resource (app or database) and the current database UUID if any. It exits CLI in case of error.
(ctx context.Context, c *cli.Command)
| 95 | // and the current database UUID if any. |
| 96 | // It exits CLI in case of error. |
| 97 | func GetCurrentResourceAndDatabase(ctx context.Context, c *cli.Command) (string, string) { |
| 98 | if os.Getenv("SCALINGO_PREVIEW_FEATURES") != "true" { |
| 99 | return CurrentApp(ctx, c), "" |
| 100 | } |
| 101 | |
| 102 | currentApp := extractAppName(ctx, c) |
| 103 | currentDatabase, databaseUUID, err := currentDatabaseNameAndUUID(ctx, c) |
| 104 | if err != nil && !errors.Is(err, errDatabaseNotFound) { |
| 105 | io.Error(err) |
| 106 | os.Exit(1) |
| 107 | } |
| 108 | |
| 109 | // Check if --app flag is set explicitly |
| 110 | var appFlagSet bool |
| 111 | for _, cliContext := range c.Lineage() { |
| 112 | if cliContext.IsSet("app") { |
| 113 | appFlagSet = true |
| 114 | break |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if appFlagSet && currentDatabase != "" && databaseUUID != "" { |
| 119 | io.Error("You can't use --app and --database flags together.") |
| 120 | os.Exit(1) |
| 121 | } |
| 122 | |
| 123 | if currentApp == "" && currentDatabase == "" { |
| 124 | io.Error("No application or database found. Please use --app or --database flag.") |
| 125 | os.Exit(1) |
| 126 | } |
| 127 | |
| 128 | currentResource := currentApp |
| 129 | if currentDatabase != "" { |
| 130 | currentResource = currentDatabase |
| 131 | } |
| 132 | |
| 133 | debug.Println("[detect] Current resource is", currentResource) |
| 134 | if databaseUUID != "" { |
| 135 | debug.Println("[detect] Current database UUID is", databaseUUID) |
| 136 | } |
| 137 | |
| 138 | return currentResource, databaseUUID |
| 139 | } |
| 140 | |
| 141 | // CurrentApp returns the app name if it has been found in one of the following: |
| 142 | // app flag, environment variable "SCALINGO_APP", current Git remote. |
no test coverage detected