Skip to content

Commit

Permalink
feat(strm-1462): add create project command (#99)
Browse files Browse the repository at this point in the history
Co-authored-by: Bob van den Hoogen <bob@strmprivacy.io>
  • Loading branch information
bobvandenhoogen and Bob van den Hoogen authored Aug 9, 2022
1 parent fda393d commit f6508b5
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 10 deletions.
2 changes: 2 additions & 0 deletions pkg/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strmprivacy/strm/pkg/entity/event_contract"
"strmprivacy/strm/pkg/entity/kafka_exporter"
"strmprivacy/strm/pkg/entity/kafka_user"
"strmprivacy/strm/pkg/entity/project"
"strmprivacy/strm/pkg/entity/schema"
"strmprivacy/strm/pkg/entity/stream"
)
Expand All @@ -29,4 +30,5 @@ func init() {
CreateCmd.AddCommand(kafka_user.CreateCmd())
CreateCmd.AddCommand(schema.CreateCmd())
CreateCmd.AddCommand(event_contract.CreateCmd())
CreateCmd.AddCommand(project.CreateCmd())
}
13 changes: 6 additions & 7 deletions pkg/context/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
)

const (
configCommandName = "config"
entityInfoCommandName = "info"
accountCommandName = "account"
projectCommandName = "project"
configCommandName = "config"
entityInfoCommandName = "info"
accountCommandName = "account"
projectCommandName = "project"
)

func Configuration() *cobra.Command {
Expand Down Expand Up @@ -67,7 +67,6 @@ func Account() *cobra.Command {
return cmd
}


func EntityInfo() *cobra.Command {
entityInfo := &cobra.Command{
Use: entityInfoCommandName,
Expand Down Expand Up @@ -102,7 +101,7 @@ func Project() *cobra.Command {
cmd := &cobra.Command{
Use: projectCommandName + " [name]",
Short: "Show or set the active project",
Args: cobra.MinimumNArgs(0),
Args: cobra.MinimumNArgs(0),
DisableAutoGenTag: true,
PreRun: func(cmd *cobra.Command, args []string) {
printer = configurePrinter(cmd)
Expand Down Expand Up @@ -131,4 +130,4 @@ func Project() *cobra.Command {

func savedEntitiesCompletion(cmd *cobra.Command, args []string, complete string) ([]string, cobra.ShellCompDirective) {
return listSavedEntities(path.Join(common.ConfigPath, common.SavedEntitiesDirectory)), cobra.ShellCompDirectiveNoFileComp
}
}
20 changes: 19 additions & 1 deletion pkg/entity/project/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "github.com/spf13/cobra"

func ListCmd() *cobra.Command {
return &cobra.Command{
Use: "projects",
Use: "projects",
Short: "List all projects you have access to",
PreRun: func(cmd *cobra.Command, args []string) {
printer = configurePrinter(cmd)
Expand All @@ -15,3 +15,21 @@ func ListCmd() *cobra.Command {
},
}
}

func CreateCmd() *cobra.Command {
project := &cobra.Command{
Use: "project [name]",
Short: "Create a new project",
PreRun: func(cmd *cobra.Command, args []string) {
printer = configurePrinter(cmd)
},
DisableAutoGenTag: true,
Run: func(cmd *cobra.Command, args []string) {
create(&args[0], cmd)
},
Args: cobra.ExactArgs(1), //
}
flags := project.Flags()
flags.String(descriptionFlag, "", "description of the project")
return project
}
19 changes: 17 additions & 2 deletions pkg/entity/project/printers.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,40 @@ func availablePrinters() map[string]util.Printer {
return util.MergePrinterMaps(
util.DefaultPrinters,
map[string]util.Printer{
common.OutputFormatTable + common.ListCommandName: listTablePrinter{},
common.OutputFormatPlain + common.ListCommandName: listPlainPrinter{},
common.OutputFormatTable + common.ListCommandName: listTablePrinter{},
common.OutputFormatTable + common.CreateCommandName: createTablePrinter{},
common.OutputFormatPlain + common.ListCommandName: listPlainPrinter{},
common.OutputFormatPlain + common.CreateCommandName: createPlainPrinter{},
},
)
}

type listPlainPrinter struct{}
type createPlainPrinter struct{}

type listTablePrinter struct{}
type createTablePrinter struct{}

func (p listTablePrinter) Print(data interface{}) {
listResponse, _ := (data).(*projects.ListProjectsResponse)
printTable(listResponse.Projects)
}

func (p createTablePrinter) Print(data interface{}) {
createResponse, _ := (data).(*projects.CreateProjectResponse)
printTable([]*v1.Project{createResponse.Project})
}

func (p listPlainPrinter) Print(data interface{}) {
listResponse, _ := (data).(*projects.ListProjectsResponse)
printPlain(listResponse.Projects)
}

func (p createPlainPrinter) Print(data interface{}) {
createResponse, _ := (data).(*projects.CreateProjectResponse)
printPlain([]*v1.Project{createResponse.Project})
}

func printTable(projects []*v1.Project) {
rows := make([]table.Row, 0, len(projects))

Expand Down
20 changes: 20 additions & 0 deletions pkg/entity/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ import (
"context"
"errors"
"fmt"
"github.com/spf13/cobra"
"github.com/strmprivacy/api-definitions-go/v2/api/entities/v1"
"github.com/strmprivacy/api-definitions-go/v2/api/projects/v1"
"google.golang.org/grpc"
"strmprivacy/strm/pkg/common"
"strmprivacy/strm/pkg/util"
)

var client projects.ProjectsServiceClient
var apiContext context.Context

const (
descriptionFlag = "description"
)

func SetupClient(clientConnection *grpc.ClientConn, ctx context.Context) {
apiContext = ctx
client = projects.NewProjectsServiceClient(clientConnection)
Expand All @@ -36,3 +42,17 @@ func GetProject(projectName string) *entities.Project {
}
return nil
}

func create(projectName *string, cmd *cobra.Command) *projects.CreateProjectResponse {
flags := cmd.Flags()
description := util.GetStringAndErr(flags, descriptionFlag)
req := &projects.CreateProjectRequest{
Project: &entities.Project{
Name: *projectName,
Description: description,
},
}
response, err := client.CreateProject(apiContext, req)
common.CliExit(err)
return response
}

0 comments on commit f6508b5

Please sign in to comment.