-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added list status command to get a list of all status
- Loading branch information
Showing
10 changed files
with
211 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/status" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var statusCmd = &cobra.Command{ | ||
Use: "status", | ||
Short: "Lists status", | ||
Long: "Get a list of all status of the instance.", | ||
Run: listStatus, | ||
} | ||
|
||
func listStatus(_ *cobra.Command, _ []string) { | ||
if all, err := status.All(); err == nil { | ||
printer.StatusList(all) | ||
} else { | ||
printer.Error(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package printer | ||
|
||
import ( | ||
"fmt" | ||
"github.com/opf/openproject-cli/models" | ||
) | ||
|
||
func StatusList(statuss []*models.Status) { | ||
for _, p := range statuss { | ||
printStatus(p) | ||
} | ||
} | ||
|
||
func Status(status *models.Status) { | ||
printStatus(status) | ||
} | ||
|
||
func printStatus(status *models.Status) { | ||
var defaultSuffix string | ||
|
||
id := fmt.Sprintf("#%d", status.Id) | ||
if status.IsDefault { | ||
defaultSuffix = fmt.Sprintf(" (%s)", Yellow("default")) | ||
} | ||
activePrinter.Printf("%s %s%s\n", Red(id), Cyan(status.Name), defaultSuffix) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
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" | ||
) | ||
|
||
func TestStatus_Default(t *testing.T) { | ||
testingPrinter.Reset() | ||
|
||
status := models.Status{ | ||
Id: 42, | ||
Name: "My Default", | ||
IsDefault: true, | ||
} | ||
|
||
expected := fmt.Sprintf("%s %s (%s)\n", printer.Red("#42"), printer.Cyan("My Default"), printer.Yellow("default")) | ||
|
||
printer.Status(&status) | ||
|
||
if testingPrinter.Result != expected { | ||
t.Errorf("Expected %s, but got %s", expected, testingPrinter.Result) | ||
} | ||
} | ||
|
||
func TestStatus_NoDefault(t *testing.T) { | ||
testingPrinter.Reset() | ||
|
||
status := models.Status{ | ||
Id: 42, | ||
Name: "Another Status", | ||
IsDefault: false, | ||
} | ||
|
||
expected := fmt.Sprintf("%s %s\n", printer.Red("#42"), printer.Cyan("Another Status")) | ||
|
||
printer.Status(&status) | ||
|
||
if testingPrinter.Result != expected { | ||
t.Errorf("Expected %s, but got %s", expected, testingPrinter.Result) | ||
} | ||
} | ||
|
||
func TestStatusList(t *testing.T) { | ||
testingPrinter.Reset() | ||
|
||
status := []*models.Status{ | ||
{Id: 42, 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 (%s)\n", printer.Red("#123"), printer.Cyan("Third"), printer.Yellow("default")) | ||
|
||
printer.StatusList(status) | ||
|
||
if testingPrinter.Result != expected { | ||
t.Errorf("Expected %s, but got %s", expected, testingPrinter.Result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package status | ||
|
||
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.Status, error) { | ||
query := requests.NewPagedQuery(-1, nil) | ||
response, err := requests.Get(paths.Status(), &query) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
element := parser.Parse[dtos.StatusCollectionDto](response) | ||
return element.Convert(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package dtos | ||
|
||
import "github.com/opf/openproject-cli/models" | ||
|
||
type statusDto struct { | ||
Id uint64 `json:"id"` | ||
Name string `json:"name"` | ||
Color string `json:"color"` | ||
IsDefault bool `json:"isDefault"` | ||
IsClosed bool `json:"isClosed"` | ||
IsReadonly bool `json:"isReadonly"` | ||
Position uint64 `json:"position"` | ||
} | ||
|
||
type statusElements struct { | ||
Elements []*statusDto `json:"elements"` | ||
} | ||
|
||
type StatusCollectionDto struct { | ||
Embedded *statusElements `json:"_embedded"` | ||
Type string `json:"_type"` | ||
} | ||
|
||
/////////////// MODEL CONVERSION /////////////// | ||
|
||
func (dto *StatusCollectionDto) Convert() []*models.Status { | ||
var projects = make([]*models.Status, len(dto.Embedded.Elements)) | ||
|
||
for idx, p := range dto.Embedded.Elements { | ||
projects[idx] = p.Convert() | ||
} | ||
|
||
return projects | ||
} | ||
|
||
func (dto *statusDto) Convert() *models.Status { | ||
return &models.Status{ | ||
Id: dto.Id, | ||
Name: dto.Name, | ||
Color: dto.Color, | ||
IsDefault: dto.IsDefault, | ||
IsClosed: dto.IsClosed, | ||
IsReadonly: dto.IsReadonly, | ||
Position: dto.Position, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package models | ||
|
||
type Status struct { | ||
Id uint64 | ||
Name string | ||
Color string | ||
IsDefault bool | ||
IsClosed bool | ||
IsReadonly bool | ||
Position uint64 | ||
} |