Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/url parsing #11

Merged
merged 2 commits into from
Sep 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Build & Upload documentation:

Upload documentation to specific docat server:

docatl push --host localhost:8000 ./docs.zip myproject 1.0.0 -t latest
docatl push --host https://localhost:8000 ./docs.zip myproject 1.0.0 -t latest
`,
Args: cobra.RangeArgs(1, 3),
Run: func(cmd *cobra.Command, args []string) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Upload documentation:

Upload documentation to specific docat server:

docatl push --host localhost:8000 ./docs.zip myproject 1.0.0 -t latest
docatl push --host https://localhost:8000 ./docs.zip myproject 1.0.0 -t latest
`,
}

Expand Down
26 changes: 19 additions & 7 deletions pkg/docat.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
)
Expand Down Expand Up @@ -39,12 +40,11 @@ func (docat *Docat) Post(project string, version string, docsPath string) error
}
writer.Close()

//remove trailing slash from host
if docat.Host[len(docat.Host)-1:] == "/" {
docat.Host = docat.Host[:len(docat.Host)-1]
apiUrl, err := url.JoinPath(docat.Host, "api", project, version)
if err != nil {
return fmt.Errorf("unable to upload documentation because cannot create an url for host: %s error: %s", docat.Host, err)
}

apiUrl := fmt.Sprintf("%s/api/%s/%s", docat.Host, project, version)
request, err := http.NewRequest(http.MethodPost, apiUrl, body)
if err != nil {
return fmt.Errorf("unable to upload documentation because cannot create POST request: %s", err)
Expand Down Expand Up @@ -73,7 +73,11 @@ func (docat *Docat) Post(project string, version string, docsPath string) error
}

func (docat *Docat) Delete(project string, version string) error {
apiUrl := fmt.Sprintf("%s/api/%s/%s", docat.Host, project, version)
apiUrl, err := url.JoinPath(docat.Host, "api", project, version)
if err != nil {
return fmt.Errorf("unable to delete documentation because cannot create an url for host: %s error: %s", docat.Host, err)
}

request, err := http.NewRequest(http.MethodDelete, apiUrl, nil)
if err != nil {
return fmt.Errorf("unable to delete documentation because cannot create DELETE request: %s", err)
Expand All @@ -99,7 +103,11 @@ func (docat *Docat) Delete(project string, version string) error {
}

func (docat *Docat) Claim(project string) (ProjectClaim, error) {
apiUrl := fmt.Sprintf("%s/api/%s/claim", docat.Host, project)
apiUrl, err := url.JoinPath(docat.Host, "api", project, "claim")
if err != nil {
return ProjectClaim{}, fmt.Errorf("unable to claim project because cannot create an url for host: %s error: %s", docat.Host, err)
}

response, err := http.Get(apiUrl)
if err != nil {
return ProjectClaim{}, fmt.Errorf("unable to claim project because request failed: %s", err)
Expand All @@ -123,7 +131,11 @@ func (docat *Docat) Claim(project string) (ProjectClaim, error) {
}

func (docat *Docat) Tag(project string, version string, tag string) error {
apiUrl := fmt.Sprintf("%s/api/%s/%s/tags/%s", docat.Host, project, version, tag)
apiUrl, err := url.JoinPath(docat.Host, "api", project, version, "tags", tag)
if err != nil {
return fmt.Errorf("unable to tag documentation because cannot create an url for host: %s error: %s", docat.Host, err)
}

request, err := http.NewRequest(http.MethodPut, apiUrl, nil)
if err != nil {
return fmt.Errorf("unable to tag documentation because cannot create PUT request: %s", err)
Expand Down