Skip to content

Commit

Permalink
adding project list support
Browse files Browse the repository at this point in the history
  • Loading branch information
coryodaniel committed Nov 6, 2024
1 parent e4fc058 commit 6a784b1
Show file tree
Hide file tree
Showing 7 changed files with 301 additions and 3 deletions.
60 changes: 60 additions & 0 deletions cmd/project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package cmd

import (
"fmt"
"os"
"text/tabwriter"

"github.com/massdriver-cloud/mass/docs/helpdocs"
"github.com/massdriver-cloud/mass/pkg/api"
"github.com/massdriver-cloud/mass/pkg/config"
"github.com/spf13/cobra"
)

var projCmdHelp = helpdocs.MustRender("project")
var projListCmdHelp = helpdocs.MustRender("project/list")
var projCmd = &cobra.Command{
Use: "project",
Aliases: []string{"prj"},
Short: "Manage Projects",
Long: projCmdHelp,
}

var projListCmd = &cobra.Command{
Use: `list`,
Short: "List projects",
Aliases: []string{"ls"},
Long: projListCmdHelp,
RunE: runProjList,
}

func init() {
rootCmd.AddCommand(projCmd)
projCmd.AddCommand(projListCmd)
}

func runProjList(cmd *cobra.Command, args []string) error {
config, configErr := config.Get()
if configErr != nil {
return configErr
}

client := api.NewClient(config.URL, config.APIKey)

projects, err := api.ListProjects(client, config.OrgID)

w := tabwriter.NewWriter(os.Stdout, 10, 1, 5, ' ', 0)
fmt.Fprintln(w, "ID\tNAME\tSLUG")

for _, project := range *projects {
line := fmt.Sprintf("%s\t%s\t%s", project.ID, project.Name, project.Slug)
fmt.Fprintln(w, line)
}

w.Flush()

// TODO: present UI
// _, err := commands.DeployPackage(client, config.OrgID, name)

return err
}
5 changes: 5 additions & 0 deletions docs/helpdocs/project.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Manage Projects

[Projects](https://docs.massdriver.cloud/concepts/projects) act as permission and replication boundaries in Massdriver.

A project can encompass many environments (permanent or ephemeral) and manages the parity across those environments.
3 changes: 3 additions & 0 deletions docs/helpdocs/project/list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# List Projects

Lists Massdriver projects.
10 changes: 7 additions & 3 deletions pkg/api/genqlient.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ query getArtifactsByType($organizationId: ID!, $artifactType: String!) {
}
}

query projects($organizationId: ID!){
projects(organizationId: $organizationId){
id, name, defaultParams, slug, description
}
}

query getProjectById($organizationId: ID!, $id: ID!) {
project(organizationId: $organizationId, id: $id) {
id
defaultParams
slug
id, name, defaultParams, slug, description
}
}

Expand Down
25 changes: 25 additions & 0 deletions pkg/api/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (

type Project struct {
ID string
Name string
Slug string
Description string
DefaultParams map[string]interface{}
}

Expand All @@ -23,11 +25,34 @@ func GetProject(client graphql.Client, orgID string, idOrSlug string) (*Project,
func (p *getProjectByIdProject) toProject() *Project {
return &Project{
ID: p.Id,
Name: p.Name,
Slug: p.Slug,
Description: p.Description,
DefaultParams: p.DefaultParams,
}
}

func (p *projectsProjectsProject) toProject() Project {
return Project{
ID: p.Id,
Slug: p.Slug,
Name: p.Name,
Description: p.Description,
DefaultParams: p.DefaultParams,
}
}

func ListProjects(client graphql.Client, orgID string) (*[]Project, error) {
response, err := projects(context.Background(), client, orgID)
records := []Project{}

for _, prj := range response.Projects {
records = append(records, prj.toProject())
}

return &records, err
}

func (p *Project) GetDefaultParams() map[string]PreviewPackage {
packages := make(map[string]PreviewPackage)

Expand Down
31 changes: 31 additions & 0 deletions pkg/api/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,34 @@ func TestGetProject(t *testing.T) {
t.Errorf("got %s, wanted %s", got, want)
}
}

func TestListProjects(t *testing.T) {
client := gqlmock.NewClientWithSingleJSONResponse(map[string]interface{}{
"data": map[string]interface{}{
"projects": []map[string]interface{}{
{
"id": "uuid1",
"name": "project1",
},
{
"id": "uuid2",
"name": "project2",
},
},
},
})

projects, err := api.ListProjects(client, "faux-org-id")

if err != nil {
t.Fatal(err)
}

got := len(*projects)

want := 2

if got != want {
t.Errorf("got %d, wanted %d", got, want)
}
}
Loading

0 comments on commit 6a784b1

Please sign in to comment.