func newConnectionParamsCmd()

in internal/onetime/configure/mysql/mysql.go [60:118]


func newConnectionParamsCmd(cfg *cliconfig.Configure) *cobra.Command {
	var username, projectID, secretName, password string
	cpCmd := &cobra.Command{
		Use:   "connection-params",
		Short: "Add connection parameters for a MySQL database.",
		Long: `Sets the username, password, and Secret Manager details
for connecting to the MySQL database specified in the main configuration.

Existing connection parameters will be overwritten by the provided flags.

WARNING: Using the --password flag is not recommended for security reasons
as it can expose the password in shell history or logs. Please prefer storing
the password in Google Cloud Secret Manager and using the --project-id and
--secret-name flags instead.`,
		Run: func(cmd *cobra.Command, args []string) {
			cfg.ValidateMySQLConnectionParams()
			cp := cfg.Configuration.MysqlConfiguration.ConnectionParameters

			if cmd.Flags().Changed("username") {
				msg := fmt.Sprintf("Setting MySQL Username: %v", username)
				cfg.LogToBoth(cmd.Context(), msg)
				cp.Username = username
				cfg.MySQLConfigModified = true
			}
			if cmd.Flags().Changed("password") {
				msg := fmt.Sprintf("Setting MySQL Password: %v", password)
				cfg.LogToBoth(cmd.Context(), msg)
				cp.Password = password
				cfg.MySQLConfigModified = true
			}

			spChanged := cmd.Flags().Changed("project-id")
			snChanged := cmd.Flags().Changed("secret-name")
			if (spChanged || snChanged) && (cp.Secret == nil) {
				cp.Secret = &cpb.SecretRef{}
			}
			if spChanged {
				msg := fmt.Sprintf("Setting MySQL Project ID: %v", projectID)
				cfg.LogToBoth(cmd.Context(), msg)
				cp.Secret.ProjectId = projectID
				cfg.MySQLConfigModified = true
			}
			if snChanged {
				msg := fmt.Sprintf("Setting MySQL Secret Name: %v", secretName)
				cfg.LogToBoth(cmd.Context(), msg)
				cp.Secret.SecretName = secretName
				cfg.MySQLConfigModified = true
			}
		},
	}

	// Add flags for the connection
	cpCmd.Flags().StringVar(&username, "username", "", "Database username")
	cpCmd.Flags().StringVar(&projectID, "project-id", "", "Project ID")
	cpCmd.Flags().StringVar(&secretName, "secret-name", "", "Secret name")
	cpCmd.Flags().StringVar(&password, "password", "", "Password")

	return cpCmd
}