private static Optional parseProfileDefinition()

in core/profiles/src/main/java/software/amazon/awssdk/profiles/internal/ProfileFileReader.java [186:238]


    private static Optional<String> parseProfileDefinition(ParserState state, String lineWithoutWhitespace) {
        String lineWithoutBrackets = lineWithoutWhitespace.substring(1, lineWithoutWhitespace.length() - 1);
        String rawProfileName = StringUtils.trim(lineWithoutBrackets);
        boolean hasProfilePrefix = rawProfileName.startsWith("profile ") || rawProfileName.startsWith("profile\t");

        String standardizedProfileName;
        if (state.fileType == ProfileFile.Type.CONFIGURATION) {
            if (hasProfilePrefix) {
                standardizedProfileName = StringUtils.trim(rawProfileName.substring(ProfileFile.PROFILES_SECTION_TITLE.length()));
            } else if (rawProfileName.equals("default")) {
                standardizedProfileName = "default";
            } else {
                log.warn(() -> "Ignoring profile '" + rawProfileName + "' on line " + state.currentLineNumber + " because it " +
                               "did not start with 'profile ' and it was not 'default'.");
                return Optional.empty();
            }
        } else if (state.fileType == ProfileFile.Type.CREDENTIALS) {
            standardizedProfileName = rawProfileName;
        } else {
            throw new IllegalStateException("Unknown profile file type: " + state.fileType);
        }

        String profileName = StringUtils.trim(standardizedProfileName);

        // If the profile name includes invalid characters, it should be ignored.
        if (!isValidIdentifier(profileName)) {
            log.warn(() -> "Ignoring profile '" + standardizedProfileName + "' on line " + state.currentLineNumber + " because " +
                           "it was not alphanumeric with only these special characters: - / . % @ _ : +");
            return Optional.empty();
        }

        // [profile default] must take priority over [default] in configuration files.
        boolean isDefaultProfile = profileName.equals("default");
        boolean seenProfileBefore = state.profiles.get(ProfileFile.PROFILES_SECTION_TITLE).containsKey(profileName);

        if (state.fileType == ProfileFile.Type.CONFIGURATION && isDefaultProfile && seenProfileBefore) {
            if (!hasProfilePrefix && state.seenDefaultProfileWithProfilePrefix) {
                log.warn(() -> "Ignoring profile '[default]' on line " + state.currentLineNumber + ", because " +
                               "'[profile default]' was already seen in the same file.");
                return Optional.empty();
            } else if (hasProfilePrefix && !state.seenDefaultProfileWithProfilePrefix) {
                log.warn(() -> "Ignoring earlier-seen '[default]', because '[profile default]' was found on line " +
                               state.currentLineNumber);
                state.profiles.get(ProfileFile.PROFILES_SECTION_TITLE).remove("default");
            }
        }

        if (isDefaultProfile && hasProfilePrefix) {
            state.seenDefaultProfileWithProfilePrefix = true;
        }

        return Optional.of(profileName);
    }