Skip to content

Commit

Permalink
Implement list for pages
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Rüger authored and mrueg committed Oct 21, 2024
1 parent a55e9b1 commit 479968c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 17 deletions.
61 changes: 49 additions & 12 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package cmd

import (
"cmp"
"encoding/json"
"fmt"
"os"
"slices"
"text/tabwriter"

"github.com/kovetskiy/lorg"
Expand All @@ -23,21 +25,22 @@ var listFlags = []cli.Flag{
EnvVars: []string{"MARK_SPACE"},
})}

var pageFlags = []cli.Flag{
altsrc.NewStringFlag(&cli.StringFlag{
Name: "space",
Value: "",
Usage: "lists all pages in a space.",
EnvVars: []string{"MARK_SPACE"},
})}

var ListCmd = &cli.Command{
Name: "list",
Usage: "lists pages, spaces and labels",
Subcommands: []*cli.Command{
{
Name: "pages",
Usage: "lists pages.",
Flags: []cli.Flag{
altsrc.NewStringFlag(&cli.StringFlag{
Name: "space",
Value: "",
Usage: "lists all pages in a space.",
EnvVars: []string{"MARK_SPACE"},
}),
},
Name: "pages",
Usage: "lists pages.",
Flags: append(append(flags, listFlags...), pageFlags...),
Action: ListPages,
},
{
Expand Down Expand Up @@ -76,6 +79,36 @@ func ListPages(cCtx *cli.Context) error {
log.GetLogger().SetOutput(os.Stderr)
}

creds, err := auth.GetCredentials(cCtx.String("username"), cCtx.String("password"), "", cCtx.String("base-url"), false)
if err != nil {
return err
}

api := confluence.NewAPI(creds.BaseURL, creds.Username, creds.Password)

pages, err := api.ListPages(cCtx.String("space"))

if err != nil {
log.Fatal(err)
os.Exit(0)
}

if cCtx.String("output-format") == "json" {
p, _ := json.MarshalIndent(pages.Pages, "", "\t")
fmt.Print(string(p))
} else {
w := tabwriter.NewWriter(os.Stdout, 6, 4, 3, ' ', 0)
fmt.Fprintln(w, "Name\tID")

slices.SortFunc(pages.Pages, func(a, b confluence.PageInfo) int {
return cmp.Compare(a.Title, b.Title)
})
for _, page := range pages.Pages {
fmt.Fprintf(w, "%s\t%s\n", page.Title, page.ID)
}
w.Flush()
}

return nil
}

Expand Down Expand Up @@ -116,8 +149,12 @@ func ListSpaces(cCtx *cli.Context) error {
s, _ := json.MarshalIndent(spaces.Spaces, "", "\t")
fmt.Print(string(s))
} else {
w := tabwriter.NewWriter(os.Stdout, 0, 8, 0, '\t', 0)
fmt.Fprintln(w, "ID\tKey\tName")
w := tabwriter.NewWriter(os.Stdout, 6, 4, 3, ' ', 0)
fmt.Fprintln(w, "Key\tName\tID")

slices.SortFunc(spaces.Spaces, func(a, b confluence.SpaceInfo) int {
return cmp.Compare(a.Key, b.Key)
})
for _, space := range spaces.Spaces {
fmt.Fprintf(w, "%s\t%s\t%d\n", space.Key, space.Name, space.ID)
}
Expand Down
32 changes: 27 additions & 5 deletions confluence/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ type PageInfo struct {
} `json:"_links"`
}

type PageInfoList struct {
Pages []PageInfo `json:"results"`
}

type AttachmentInfo struct {
Filename string `json:"title"`
ID string `json:"id"`
Expand Down Expand Up @@ -183,9 +187,7 @@ func (api *API) FindPage(
title string,
pageType string,
) (*PageInfo, error) {
result := struct {
Results []PageInfo `json:"results"`
}{}
var result PageInfoList

payload := map[string]string{
"spaceKey": space,
Expand All @@ -210,11 +212,11 @@ func (api *API) FindPage(
return nil, newErrorStatusNotOK(request)
}

if len(result.Results) == 0 {
if len(result.Pages) == 0 {
return nil, nil
}

return &result.Results[0], nil
return &result.Pages[0], nil
}

func (api *API) CreateAttachment(
Expand Down Expand Up @@ -798,6 +800,26 @@ func (api *API) ListSpaces() (*SpaceInfoList, error) {
return request.Response.(*SpaceInfoList), nil
}

func (api *API) ListPages(spaceKey string) (*PageInfoList, error) {
payload := map[string]string{
"limit": "1000",
"spaceKey": spaceKey,
}

request, err := api.rest.Res(
"content", &PageInfoList{},
).Get(payload)
if err != nil {
return nil, err
}

if request.Raw.StatusCode != http.StatusOK {
return nil, newErrorStatusNotOK(request)
}

return request.Response.(*PageInfoList), nil
}

func newErrorStatusNotOK(request *gopencils.Resource) error {
if request.Raw.StatusCode == http.StatusUnauthorized {
return errors.New(
Expand Down

0 comments on commit 479968c

Please sign in to comment.