github.com/Redstoneguy129/cli@v0.0.0-20230211220159-15dca4e91917/internal/bans/update/update.go (about) 1 package update 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 "net" 8 9 "github.com/spf13/afero" 10 "github.com/Redstoneguy129/cli/internal/utils" 11 "github.com/Redstoneguy129/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 fmt.Errorf("only IPv4 supported at the moment: %s", ip) 19 } 20 } 21 return nil 22 } 23 24 func Run(ctx context.Context, projectRefArg string, dbIpsToUnban []string, fsys afero.Fs) error { 25 // 1. Sanity checks. 26 projectRef := projectRefArg 27 28 // 1. sanity checks 29 { 30 if len(projectRefArg) == 0 { 31 ref, err := utils.LoadProjectRef(fsys) 32 if err != nil { 33 return err 34 } 35 projectRef = ref 36 } else if !utils.ProjectRefPattern.MatchString(projectRef) { 37 return errors.New("Invalid project ref format. Must be like `abcdefghijklmnopqrst`.") 38 } 39 err := validateIps(dbIpsToUnban) 40 if err != nil { 41 return err 42 } 43 } 44 45 // 2. remove bans 46 { 47 resp, err := utils.GetSupabase().RemoveNetworkBanWithResponse(ctx, projectRef, api.RemoveNetworkBanRequest{ 48 Ipv4Addresses: dbIpsToUnban, 49 }) 50 if err != nil { 51 return err 52 } 53 if resp.StatusCode() != 200 { 54 return errors.New("failed to remove network bans: " + string(resp.Body)) 55 } 56 fmt.Printf("Successfully removed bans for %+v.\n", dbIpsToUnban) 57 return nil 58 } 59 }