Repository: juspay/hyperswitch
File: scripts/add_connector.sh (Shell)

Code before:
sed -i '' -e "\$a\\
\\
[${payment_gateway}]\\
[${payment_gateway}.connector_auth.HeaderKey]\\
api_key = \\\"API Key\\\"" crates/connector_configs/toml/production.toml

sed -i'' -e "s/^default_imp_for_connector_request_id!(/default_imp_for_connector_request_id!(\n    connectors::${payment_gateway_camelcase},/" $src/core/payments/flows.rs
sed -i'' -e "s/^default_imp_for_fraud_check!(/default_imp_for_fraud_check!(\n    connectors::${payment_gateway_camelcase},/" $src/core/payments/flows.rs
sed -i'' -e "s/^default_imp_for_connector_authentication!(/default_imp_for_connector_authentication!(\n    connectors::${payment_gateway_camelcase},/" $src/core/payments/flows.rs
sed -i'' -e "/pub ${previous_connector}: Option<ConnectorTomlConfig>,/a\\
    pub ${payment_gateway}: Option<ConnectorTomlConfig>,
" crates/connector_configs/src/connector.rs

sed -i'' -e "/mod utils;/ s/mod utils;/mod ${payment_gateway};\nmod utils;/" crates/router/tests/connectors/main.rs
sed -i'' -e "s/^default_imp_for_new_connector_integration_payouts!(/default_imp_for_new_connector_integration_payouts!(\n    connector::${payment_gateway_camelcase},/" crates/router/src/core/payments/connector_integration_v2_impls.rs
sed -i'' -e "s/^default_imp_for_new_connector_integration_frm!(/default_imp_for_new_connector_integration_frm!(\n    connector::${payment_gateway_camelcase},/" crates/router/src/core/payments/connector_integration_v2_impls.rs
sed -i'' -e "s/^default_imp_for_new_connector_integration_connector_authentication!(/default_imp_for_new_connector_integration_connector_authentication!(\n    connector::${payment_gateway_camelcase},/" crates/router/src/core/payments/connector_integration_v2_impls.rs



# Remove temporary files created in above step
rm $conn.rs-e $src/types/api.rs-e $src/configs/settings.rs-e config/development.toml-e config/docker_compose.toml-e config/config.example.toml-e loadtest/config/development.toml-e crates/api_models/src/connector_enums.rs-e crates/euclid/src/enums.rs-e crates/api_models/src/routing.rs-e $src/core/payments/flows.rs-e crates/common_enums/src/connector_enums.rs-e $src/types/transformers.rs-e $src/core/admin.rs-e crates/hyperswitch_connectors/src/default_implementations.rs-e crates/hyperswitch_connectors/src/default_implementations_v2.rs-e crates/hyperswitch_interfaces/src/configs.rs-e $src/connector.rs-e config/deployments/integration_test.toml-e config/deployments/production.toml-e config/deployments/sandbox.toml-e temp crates/connector_configs/src/connector.rs-e crates/router/tests/connectors/main.rs-e crates/router/src/core/payments/connector_integration_v2_impls.rs-e

sed -i'' -e "/pub ${previous_connector}: ConnectorParams,/a\\
    pub ${payment_gateway}: ConnectorParams,
" crates/hyperswitch_domain_models/src/configs.rs
rm crates/hyperswitch_domain_models/src/configs.rs-e

cd $conn/

# Generate template files for the connector
cargo install cargo-generate
cargo generate --path ../../../../connector-template -n $payment_gateway

# Move sub files and test files to appropriate folder
mv $payment_gateway/mod.rs $payment_gateway.rs
mkdir -p ../../../router/tests/connectors
mv "$payment_gateway/test.rs" ../../../router/tests/connectors/$payment_gateway.rs


# Remove changes from tests if already done for this connector
git checkout ${tests}/main.rs ${test_utils}/connector_auth.rs ${tests}/sample_auth.toml

# Add enum for this connector in test folder
sed -i'' -e "s/mod utils;/mod ${payment_gateway};\nmod utils;/" ${tests}/main.rs
sed -i'' -e "s/    pub $previous_connector: \(.*\)/\tpub $previous_connector: \1\n\tpub ${payment_gateway}: Option<HeaderKey>,/" ${test_utils}/connector_auth.rs
echo "\n\n[${payment_gateway}]\napi_key=\"API Key\"" >> ${tests}/sample_auth.toml

# Remove temporary files created in above step
rm ${tests}/main.rs-e ${test_utils}/connector_auth.rs-e
cargo +nightly fmt --all

Review comment (refactor):
Isn't this going to generate more temporary files, but we' have a step before this which says "Remove temporary files created in above step".

Can we move this line above the `rm` command?

Code after:

sed -i '' -e "\$a\\
\\
[${payment_gateway}]\\
[${payment_gateway}.connector_auth.HeaderKey]\\
api_key = \\\"API Key\\\"" crates/connector_configs/toml/development.toml

sed -i '' -e "\$a\\
\\
[${payment_gateway}]\\
[${payment_gateway}.connector_auth.HeaderKey]\\
api_key = \\\"API Key\\\"" crates/connector_configs/toml/production.toml

sed -i'' -e "s/^default_imp_for_connector_request_id!(/default_imp_for_connector_request_id!(\n    connectors::${payment_gateway_camelcase},/" $src/core/payments/flows.rs
sed -i'' -e "s/^default_imp_for_fraud_check!(/default_imp_for_fraud_check!(\n    connectors::${payment_gateway_camelcase},/" $src/core/payments/flows.rs
sed -i'' -e "s/^default_imp_for_connector_authentication!(/default_imp_for_connector_authentication!(\n    connectors::${payment_gateway_camelcase},/" $src/core/payments/flows.rs
sed -i'' -e "/pub ${previous_connector}: Option<ConnectorTomlConfig>,/a\\
    pub ${payment_gateway}: Option<ConnectorTomlConfig>,
" crates/connector_configs/src/connector.rs

sed -i'' -e "/mod utils;/ s/mod utils;/mod ${payment_gateway};\nmod utils;/" crates/router/tests/connectors/main.rs
sed -i'' -e "s/^default_imp_for_new_connector_integration_payouts!(/default_imp_for_new_connector_integration_payouts!(\n    connector::${payment_gateway_camelcase},/" crates/router/src/core/payments/connector_integration_v2_impls.rs
sed -i'' -e "s/^default_imp_for_new_connector_integration_frm!(/default_imp_for_new_connector_integration_frm!(\n    connector::${payment_gateway_camelcase},/" crates/router/src/core/payments/connector_integration_v2_impls.rs
sed -i'' -e "s/^default_imp_for_new_connector_integration_connector_authentication!(/default_imp_for_new_connector_integration_connector_authentication!(\n    connector::${payment_gateway_camelcase},/" crates/router/src/core/payments/connector_integration_v2_impls.rs

sed -i'' -e "/pub ${previous_connector}: ConnectorParams,/a\\
    pub ${payment_gateway}: ConnectorParams,
" crates/hyperswitch_domain_models/src/configs.rs


# Remove temporary files created in above step
rm $conn.rs-e $src/types/api.rs-e $src/configs/settings.rs-e config/development.toml-e config/docker_compose.toml-e config/config.example.toml-e loadtest/config/development.toml-e crates/api_models/src/connector_enums.rs-e crates/euclid/src/enums.rs-e crates/api_models/src/routing.rs-e $src/core/payments/flows.rs-e crates/common_enums/src/connector_enums.rs-e $src/types/transformers.rs-e $src/core/admin.rs-e crates/hyperswitch_connectors/src/default_implementations.rs-e crates/hyperswitch_connectors/src/default_implementations_v2.rs-e crates/hyperswitch_interfaces/src/configs.rs-e $src/connector.rs-e config/deployments/integration_test.toml-e config/deployments/production.toml-e config/deployments/sandbox.toml-e temp crates/connector_configs/src/connector.rs-e crates/router/tests/connectors/main.rs-e crates/router/src/core/payments/connector_integration_v2_impls.rs-e crates/hyperswitch_domain_models/src/configs.rs-e

cd $conn/

# Generate template files for the connector
cargo install cargo-generate
cargo generate --path ../../../../connector-template -n $payment_gateway

# Move sub files and test files to appropriate folder
mv $payment_gateway/mod.rs $payment_gateway.rs
mkdir -p ../../../router/tests/connectors
mv "$payment_gateway/test.rs" ../../../router/tests/connectors/$payment_gateway.rs


# Remove changes from tests if already done for this connector
git checkout ${tests}/main.rs ${test_utils}/connector_auth.rs ${tests}/sample_auth.toml

# Add enum for this connector in test folder
sed -i'' -e "s/mod utils;/mod ${payment_gateway};\nmod utils;/" ${tests}/main.rs
sed -i'' -e "s/    pub $previous_connector: \(.*\)/\tpub $previous_connector: \1\n\tpub ${payment_gateway}: Option<HeaderKey>,/" ${test_utils}/connector_auth.rs