Skip to content

Commit

Permalink
Cleanup URL before using it
Browse files Browse the repository at this point in the history
  • Loading branch information
lustefaniak committed May 9, 2023
1 parent 7a564e3 commit 81348d1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
16 changes: 14 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
"github.com/getsynq/connections-tableau/metadata"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"io/ioutil"
"net/url"
"os"
"strings"
"time"
)
Expand Down Expand Up @@ -67,6 +68,8 @@ func init() {

rootCmd.RunE = func(cmd *cobra.Command, args []string) error {

TableauUrl = cleanupUrl(TableauUrl)

token, _, err := internal.LoginPersonalAccessToken(TableauUrl, TableauSite, TableauTokenName, TableauTokenValue)

if err != nil {
Expand Down Expand Up @@ -112,7 +115,7 @@ func init() {
}

fileName := strings.ReplaceAll(fmt.Sprintf("tables-%s.json", time.Now().UTC().Format(time.RFC3339)), ":", "_")
err = ioutil.WriteFile(fileName, jsonBytes, 0644)
err = os.WriteFile(fileName, jsonBytes, 0644)
if err != nil {
return errors.Wrapf(err, "failed to write file %s", fileName)
}
Expand All @@ -124,6 +127,15 @@ func init() {

}

func cleanupUrl(in string) string {
u, err := url.Parse(in)
if err != nil {
panic(fmt.Sprintf("failed to parse url %s", in))
}
u.Fragment = ""
return u.String()
}

//go:generate go run github.com/Khan/genqlient
func main() {

Expand Down
22 changes: 22 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import "testing"

func Test_cleanupUrl(t *testing.T) {
tests := []struct {
url string
want string
}{
{
url: "https://reports.foo.io/#/site/",
want: "https://reports.foo.io/",
},
}
for _, tt := range tests {
t.Run(tt.url, func(t *testing.T) {
if got := cleanupUrl(tt.url); got != tt.want {
t.Errorf("cleanupUrl() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 81348d1

Please sign in to comment.