Clean up code

This commit is contained in:
Melroy van den Berg 2025-04-24 18:21:44 +02:00
parent cd2f554e50
commit e3381774b1
No known key found for this signature in database
GPG key ID: 71D11FF23454B9D7
3 changed files with 108 additions and 112 deletions

View file

@ -6,8 +6,8 @@ import (
"os"
"strings"
"gitlab.melroy.org/melroy/fediresolve/resolver"
"github.com/spf13/cobra"
"gitlab.melroy.org/melroy/fediresolve/resolver"
)
var rootCmd = &cobra.Command{
@ -48,7 +48,3 @@ The tool supports both direct URLs to posts/comments/threads and Fediverse handl
func Execute() error {
return rootCmd.Execute()
}
func init() {
// Here you can define flags and configuration settings
}

View file

@ -5,14 +5,14 @@ import (
"crypto/rsa"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"strings"
"time"
"gitlab.melroy.org/melroy/fediresolve/formatter"
"github.com/go-fed/httpsig"
"github.com/tidwall/gjson"
"gitlab.melroy.org/melroy/fediresolve/formatter"
)
// Define common constants
@ -93,12 +93,12 @@ func (r *Resolver) fetchActivityPubObjectWithSignature(objectURL string) (string
}
// Read body for error info
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
return "", fmt.Errorf("signed request failed with status: %s, body: %s", resp.Status, string(body))
}
// Read and parse the response
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("error reading response: %v", err)
}
@ -156,7 +156,7 @@ func (r *Resolver) fetchActivityPubObjectDirect(objectURL string) (string, error
// Check if we got a redirect (302, 301, etc.)
if resp.StatusCode == http.StatusFound || resp.StatusCode == http.StatusMovedPermanently ||
resp.StatusCode == http.StatusTemporaryRedirect || resp.StatusCode == http.StatusPermanentRedirect {
resp.StatusCode == http.StatusTemporaryRedirect || resp.StatusCode == http.StatusPermanentRedirect {
// Get the redirect URL from the Location header
redirectURL := resp.Header.Get("Location")
if redirectURL != "" {
@ -168,12 +168,12 @@ func (r *Resolver) fetchActivityPubObjectDirect(objectURL string) (string, error
if resp.StatusCode != http.StatusOK {
// Read body for error info
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
return "", fmt.Errorf("request failed with status: %s, body: %s", resp.Status, string(body))
}
// Read and parse the response
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("error reading response: %v", err)
}
@ -251,12 +251,12 @@ func (r *Resolver) fetchWithSignature(objectURL string) (string, error) {
fmt.Printf("Received response with status: %s\n", resp.Status)
if resp.StatusCode != http.StatusOK {
// Read body for error info
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
return "", fmt.Errorf("signed request failed with status: %s, body: %s", resp.Status, string(body))
}
// Read and parse the response
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("error reading response: %v", err)
}
@ -373,7 +373,7 @@ func (r *Resolver) fetchActorData(actorURL string) (map[string]interface{}, erro
}
// Read and parse the response
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading actor response: %v", err)
}
@ -478,7 +478,7 @@ func (r *Resolver) resolveActorViaWebFinger(username, domain string) (string, er
}
// Read and parse the response
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("error reading WebFinger response: %v", err)
}

View file

@ -3,7 +3,7 @@ package resolver
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"strings"
@ -118,7 +118,7 @@ func (r *Resolver) resolveHandle(handle string) (string, error) {
}
// Read and parse the WebFinger response
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("error reading WebFinger response: %v", err)
}