github.com/supabase/cli@v1.168.1/internal/bans/update/update.go (about)

     1  package update
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net"
     7  
     8  	"github.com/go-errors/errors"
     9  	"github.com/spf13/afero"
    10  	"github.com/supabase/cli/internal/utils"
    11  	"github.com/supabase/cli/pkg/api"
    12  )
    13  
    14  func validateIps(ips []string) error {
    15  	for _, ip := range ips {
    16  		ip := net.ParseIP(ip)
    17  		if ip.To4() == nil {
    18  			return errors.Errorf("only IPv4 supported at the moment: %s", ip)
    19  		}
    20  	}
    21  	return nil
    22  }
    23  
    24  func Run(ctx context.Context, projectRef string, dbIpsToUnban []string, fsys afero.Fs) error {
    25  	// 1. sanity checks
    26  	{
    27  		err := validateIps(dbIpsToUnban)
    28  		if err != nil {
    29  			return err
    30  		}
    31  	}
    32  
    33  	// 2. remove bans
    34  	{
    35  		resp, err := utils.GetSupabase().RemoveNetworkBanWithResponse(ctx, projectRef, api.RemoveNetworkBanRequest{
    36  			Ipv4Addresses: dbIpsToUnban,
    37  		})
    38  		if err != nil {
    39  			return errors.Errorf("failed to remove network bans: %w", err)
    40  		}
    41  		if resp.StatusCode() != 200 {
    42  			return errors.New("Unexpected error removing network bans: " + string(resp.Body))
    43  		}
    44  		fmt.Printf("Successfully removed bans for %+v.\n", dbIpsToUnban)
    45  		return nil
    46  	}
    47  }