From f6508b5dc50cf978f70a933e7b6d8631814dacf6 Mon Sep 17 00:00:00 2001 From: Bob van den Hoogen Date: Tue, 9 Aug 2022 16:40:18 +0200 Subject: [PATCH] feat(strm-1462): add create project command (#99) Co-authored-by: Bob van den Hoogen --- pkg/cmd/create.go | 2 ++ pkg/context/cmd.go | 13 ++++++------- pkg/entity/project/cmd.go | 20 +++++++++++++++++++- pkg/entity/project/printers.go | 19 +++++++++++++++++-- pkg/entity/project/project.go | 20 ++++++++++++++++++++ 5 files changed, 64 insertions(+), 10 deletions(-) diff --git a/pkg/cmd/create.go b/pkg/cmd/create.go index 6e767de..9c30472 100644 --- a/pkg/cmd/create.go +++ b/pkg/cmd/create.go @@ -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" ) @@ -29,4 +30,5 @@ func init() { CreateCmd.AddCommand(kafka_user.CreateCmd()) CreateCmd.AddCommand(schema.CreateCmd()) CreateCmd.AddCommand(event_contract.CreateCmd()) + CreateCmd.AddCommand(project.CreateCmd()) } diff --git a/pkg/context/cmd.go b/pkg/context/cmd.go index 33ec6b0..3f0d045 100644 --- a/pkg/context/cmd.go +++ b/pkg/context/cmd.go @@ -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 { @@ -67,7 +67,6 @@ func Account() *cobra.Command { return cmd } - func EntityInfo() *cobra.Command { entityInfo := &cobra.Command{ Use: entityInfoCommandName, @@ -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) @@ -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 -} \ No newline at end of file +} diff --git a/pkg/entity/project/cmd.go b/pkg/entity/project/cmd.go index bd7f953..2bd4b7d 100644 --- a/pkg/entity/project/cmd.go +++ b/pkg/entity/project/cmd.go @@ -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) @@ -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 +} diff --git a/pkg/entity/project/printers.go b/pkg/entity/project/printers.go index 58ead94..70075ec 100644 --- a/pkg/entity/project/printers.go +++ b/pkg/entity/project/printers.go @@ -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)) diff --git a/pkg/entity/project/project.go b/pkg/entity/project/project.go index d8e4782..78b3ebb 100644 --- a/pkg/entity/project/project.go +++ b/pkg/entity/project/project.go @@ -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) @@ -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 +}