Skip to content

Commit

Permalink
Add list types command
Browse files Browse the repository at this point in the history
- updated status printer with indentions
  • Loading branch information
Kharonus committed May 24, 2024
1 parent 6645aa6 commit 67f8193
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 24 deletions.
1 change: 1 addition & 0 deletions cmd/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@ status.`)
workPackagesCmd,
activitiesCmd,
statusCmd,
typesCmd,
)
}
22 changes: 22 additions & 0 deletions cmd/list/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package list

import (
"github.com/opf/openproject-cli/components/printer"
"github.com/opf/openproject-cli/components/resources/types"
"github.com/spf13/cobra"
)

var typesCmd = &cobra.Command{
Use: "types",
Short: "Lists work package types",
Long: "Get a list of all work package types of the instance.",
Run: listTypes,
}

func listTypes(_ *cobra.Command, _ []string) {
if all, err := types.All(); err == nil {
printer.Types(all)
} else {
printer.Error(err)
}
}
4 changes: 4 additions & 0 deletions components/paths/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func Status() string {
return Root() + "/statuses"
}

func Types() string {
return Root() + "/types"
}

func User(id uint64) string {
return Users() + fmt.Sprintf("/%d", id)
}
Expand Down
31 changes: 22 additions & 9 deletions components/printer/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,38 @@ package printer

import (
"fmt"
"github.com/opf/openproject-cli/components/common"
"github.com/opf/openproject-cli/models"
"strings"
)

func StatusList(statuss []*models.Status) {
for _, p := range statuss {
printStatus(p)
func StatusList(status []*models.Status) {
var maxIdLength = 0
for _, s := range status {
maxIdLength = common.Max(maxIdLength, idLength(s.Id))
}

for _, s := range status {
printStatus(s, maxIdLength)
}
}

func Status(status *models.Status) {
printStatus(status)
printStatus(status, idLength(status.Id))
}

func printStatus(status *models.Status) {
var defaultSuffix string
func printStatus(status *models.Status, maxIdLength int) {
var parts []string

diff := maxIdLength - idLength(status.Id)
idStr := fmt.Sprintf("%s#%d", indent(diff), status.Id)
parts = append(parts, Red(idStr))

parts = append(parts, Cyan(status.Name))

id := fmt.Sprintf("#%d", status.Id)
if status.IsDefault {
defaultSuffix = fmt.Sprintf(" (%s)", Yellow("default"))
parts = append(parts, fmt.Sprintf("(%s)", Yellow("default")))
}
activePrinter.Printf("%s %s%s\n", Red(id), Cyan(status.Name), defaultSuffix)

activePrinter.Println(strings.Join(parts, " "))
}
15 changes: 3 additions & 12 deletions components/printer/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package printer_test

import (
"fmt"
"strconv"
"testing"

"github.com/opf/openproject-cli/components/common"
"github.com/opf/openproject-cli/components/printer"
"github.com/opf/openproject-cli/models"
)
Expand Down Expand Up @@ -50,20 +48,13 @@ func TestStatusList(t *testing.T) {
testingPrinter.Reset()

status := []*models.Status{
{Id: 42, Name: "First"},
{Id: 2, Name: "First"},
{Id: 45, Name: "Second"},
{Id: 123, Name: "Third", IsDefault: true},
}

expected := common.Reduce[*models.Status, string](
status[:len(status)-1],
func(result string, status *models.Status) string {
idString := "#" + strconv.FormatUint(status.Id, 10)

return result + fmt.Sprintf("%s %s\n", printer.Red(idString), printer.Cyan(status.Name))
},
"")

expected := fmt.Sprintf("%s %s\n", printer.Red(" #2"), printer.Cyan("First"))
expected += fmt.Sprintf("%s %s\n", printer.Red(" #45"), printer.Cyan("Second"))
expected += fmt.Sprintf("%s %s (%s)\n", printer.Red("#123"), printer.Cyan("Third"), printer.Yellow("default"))

printer.StatusList(status)
Expand Down
2 changes: 1 addition & 1 deletion components/printer/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ func printType(workPackageType *models.Type, maxIdLength int) {
idStr := fmt.Sprintf("%s#%d", indent(diff), workPackageType.Id)
parts = append(parts, Red(idStr))

parts = append(parts, Yellow(workPackageType.Name))
parts = append(parts, Cyan(workPackageType.Name))
activePrinter.Println(strings.Join(parts, " "))
}
4 changes: 2 additions & 2 deletions components/printer/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func TestTypes(t *testing.T) {
}

var expected string
expected += fmt.Sprintf("%s %s\n", printer.Red(fmt.Sprintf(" #%d", types[0].Id)), printer.Yellow(types[0].Name))
expected += fmt.Sprintf("%s %s\n", printer.Red(fmt.Sprintf("#%d", types[1].Id)), printer.Yellow(types[1].Name))
expected += fmt.Sprintf("%s %s\n", printer.Red(fmt.Sprintf(" #%d", types[0].Id)), printer.Cyan(types[0].Name))
expected += fmt.Sprintf("%s %s\n", printer.Red(fmt.Sprintf("#%d", types[1].Id)), printer.Cyan(types[1].Name))

printer.Types(types)

Expand Down
20 changes: 20 additions & 0 deletions components/resources/types/functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package types

import (
"github.com/opf/openproject-cli/components/parser"
"github.com/opf/openproject-cli/components/paths"
"github.com/opf/openproject-cli/components/requests"
"github.com/opf/openproject-cli/dtos"
"github.com/opf/openproject-cli/models"
)

func All() ([]*models.Type, error) {
query := requests.NewPagedQuery(-1, nil)
response, err := requests.Get(paths.Types(), &query)
if err != nil {
return nil, err
}

element := parser.Parse[dtos.TypeCollectionDto](response)
return element.Convert(), nil
}

0 comments on commit 67f8193

Please sign in to comment.