Skip to content

Commit

Permalink
Project interface
Browse files Browse the repository at this point in the history
  • Loading branch information
feloy committed Dec 15, 2021
1 parent 014c0ec commit e0f6d31
Show file tree
Hide file tree
Showing 14 changed files with 268 additions and 176 deletions.
21 changes: 15 additions & 6 deletions pkg/odo/cli/component/common_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,27 @@ import (

// CommonPushOptions has data needed for all pushes
type CommonPushOptions struct {
// Context
*genericclioptions.Context

// Clients
prjClient project.Client

//Flags
// TODO(feloy) Fixme
showFlag bool //nolint:structcheck
componentContext string
configFlag bool
sourceFlag bool
EnvSpecificInfo *envinfo.EnvSpecificInfo
*genericclioptions.Context

EnvSpecificInfo *envinfo.EnvSpecificInfo
}

// NewCommonPushOptions instantiates a commonPushOptions object
func NewCommonPushOptions() *CommonPushOptions {
return &CommonPushOptions{}
func NewCommonPushOptions(prjClient project.Client) *CommonPushOptions {
return &CommonPushOptions{
prjClient: prjClient,
}
}

//InitEnvInfoFromContext initializes envinfo from the context
Expand Down Expand Up @@ -60,12 +69,12 @@ func (cpo *CommonPushOptions) ResolveSrcAndConfigFlags() {
func (cpo *CommonPushOptions) ResolveProject(prjName string) (err error) {

// check if project exist
isPrjExists, err := project.Exists(cpo.Context.KClient, prjName)
isPrjExists, err := cpo.prjClient.Exists(prjName)
if err != nil {
return errors.Wrapf(err, "failed to check if project with name %s exists", prjName)
}
if !isPrjExists {
err = project.Create(cpo.Context.KClient, prjName, true)
err = cpo.prjClient.Create(prjName, true)
if err != nil {
return errors.Wrapf(
err,
Expand Down
10 changes: 7 additions & 3 deletions pkg/odo/cli/component/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"path/filepath"
"strings"

"github.com/redhat-developer/odo/pkg/kclient"
registryUtil "github.com/redhat-developer/odo/pkg/odo/cli/registry/util"
"github.com/redhat-developer/odo/pkg/odo/cmdline"
"github.com/redhat-developer/odo/pkg/project"
"github.com/zalando/go-keyring"

"github.com/devfile/library/pkg/devfile"
Expand Down Expand Up @@ -113,9 +115,9 @@ odo catalog list components
%[1]s nodejs --app myapp --project myproject`)

// NewCreateOptions returns new instance of CreateOptions
func NewCreateOptions() *CreateOptions {
func NewCreateOptions(prjClient project.Client) *CreateOptions {
return &CreateOptions{
PushOptions: NewPushOptions(),
PushOptions: NewPushOptions(prjClient),
}
}

Expand Down Expand Up @@ -345,7 +347,9 @@ func (co *CreateOptions) Run() (err error) {

// NewCmdCreate implements the create odo command
func NewCmdCreate(name, fullName string) *cobra.Command {
co := NewCreateOptions()
// The error is not handled at this point, it will be handled during Context creation
kubclient, _ := kclient.New()
co := NewCreateOptions(project.NewClient(kubclient))
var componentCreateCmd = &cobra.Command{
Use: fmt.Sprintf("%s <component_type> [component_name] [flags]", name),
Short: "Create a new component",
Expand Down
9 changes: 6 additions & 3 deletions pkg/odo/cli/component/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path/filepath"

"github.com/redhat-developer/odo/pkg/kclient"
"github.com/redhat-developer/odo/pkg/project"
scontext "github.com/redhat-developer/odo/pkg/segment/context"

"github.com/redhat-developer/odo/pkg/component"
Expand Down Expand Up @@ -73,9 +74,9 @@ type PushOptions struct {

// NewPushOptions returns new instance of PushOptions
// with "default" values for certain values, for example, show is "false"
func NewPushOptions() *PushOptions {
func NewPushOptions(prjClient project.Client) *PushOptions {
return &PushOptions{
CommonPushOptions: NewCommonPushOptions(),
CommonPushOptions: NewCommonPushOptions(prjClient),
}
}

Expand Down Expand Up @@ -228,7 +229,9 @@ func (po *PushOptions) Run() (err error) {

// NewCmdPush implements the push odo command
func NewCmdPush(name, fullName string) *cobra.Command {
po := NewPushOptions()
// The error is not handled at this point, it will be handled during Context creation
kubclient, _ := kclient.New()
po := NewPushOptions(project.NewClient(kubclient))

var pushCmd = &cobra.Command{
Use: fmt.Sprintf("%s [component name]", name),
Expand Down
12 changes: 9 additions & 3 deletions pkg/odo/cli/config/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"fmt"
"strings"

"github.com/redhat-developer/odo/pkg/kclient"
"github.com/redhat-developer/odo/pkg/odo/cmdline"
"github.com/redhat-developer/odo/pkg/project"
"github.com/redhat-developer/odo/pkg/util"

"github.com/pkg/errors"
Expand Down Expand Up @@ -54,8 +56,10 @@ type SetOptions struct {
}

// NewSetOptions creates a new SetOptions instance
func NewSetOptions() *SetOptions {
return &SetOptions{PushOptions: clicomponent.NewPushOptions()}
func NewSetOptions(prjClient project.Client) *SetOptions {
return &SetOptions{
PushOptions: clicomponent.NewPushOptions(prjClient),
}
}

// Complete completes SetOptions after they've been created
Expand Down Expand Up @@ -174,7 +178,9 @@ func isValidArgumentList(args []string) error {

// NewCmdSet implements the config set odo command
func NewCmdSet(name, fullName string) *cobra.Command {
o := NewSetOptions()
// The error is not handled at this point, it will be handled during Context creation
kubclient, _ := kclient.New()
o := NewSetOptions(project.NewClient(kubclient))
configurationSetCmd := &cobra.Command{
Use: name,
Short: "Set a value in odo config file",
Expand Down
12 changes: 9 additions & 3 deletions pkg/odo/cli/config/unset.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"strings"

"github.com/redhat-developer/odo/pkg/kclient"
"github.com/redhat-developer/odo/pkg/project"
"github.com/redhat-developer/odo/pkg/util"

"github.com/redhat-developer/odo/pkg/config"
Expand Down Expand Up @@ -49,8 +51,10 @@ type UnsetOptions struct {
}

// NewUnsetOptions creates a new UnsetOptions instance
func NewUnsetOptions() *UnsetOptions {
return &UnsetOptions{PushOptions: clicomponent.NewPushOptions()}
func NewUnsetOptions(prjClient project.Client) *UnsetOptions {
return &UnsetOptions{
PushOptions: clicomponent.NewPushOptions(prjClient),
}
}

// Complete completes UnsetOptions after they've been created
Expand Down Expand Up @@ -125,7 +129,9 @@ func (o *UnsetOptions) Run() error {

// NewCmdUnset implements the config unset odo command
func NewCmdUnset(name, fullName string) *cobra.Command {
o := NewUnsetOptions()
// The error is not handled at this point, it will be handled during Context creation
kubclient, _ := kclient.New()
o := NewUnsetOptions(project.NewClient(kubclient))
configurationUnsetCmd := &cobra.Command{
Use: name,
Short: "Unset a value in odo config file",
Expand Down
18 changes: 13 additions & 5 deletions pkg/odo/cli/project/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package project
import (
"fmt"

"github.com/redhat-developer/odo/pkg/kclient"
"github.com/redhat-developer/odo/pkg/log"
"github.com/redhat-developer/odo/pkg/machineoutput"
"github.com/redhat-developer/odo/pkg/odo/cmdline"
Expand Down Expand Up @@ -35,6 +36,9 @@ type ProjectCreateOptions struct {
// Context
*genericclioptions.Context

// Clients
prjClient project.Client

// Parameters
projectName string

Expand All @@ -43,8 +47,10 @@ type ProjectCreateOptions struct {
}

// NewProjectCreateOptions creates a ProjectCreateOptions instance
func NewProjectCreateOptions() *ProjectCreateOptions {
return &ProjectCreateOptions{}
func NewProjectCreateOptions(prjClient project.Client) *ProjectCreateOptions {
return &ProjectCreateOptions{
prjClient: prjClient,
}
}

// Complete completes ProjectCreateOptions after they've been created
Expand Down Expand Up @@ -77,7 +83,7 @@ func (pco *ProjectCreateOptions) Run() (err error) {
}

// Create the project & end the spinner (if there is any..)
err = project.Create(pco.Context.KClient, pco.projectName, pco.waitFlag)
err = pco.prjClient.Create(pco.projectName, pco.waitFlag)
if err != nil {
return err
}
Expand All @@ -87,7 +93,7 @@ func (pco *ProjectCreateOptions) Run() (err error) {
log.Successf(successMessage)

// Set the current project when created
err = project.SetCurrent(pco.Context.KClient, pco.projectName)
err = pco.prjClient.SetCurrent(pco.projectName)
if err != nil {
return err
}
Expand All @@ -105,7 +111,9 @@ func (pco *ProjectCreateOptions) Run() (err error) {

// NewCmdProjectCreate creates the project create command
func NewCmdProjectCreate(name, fullName string) *cobra.Command {
o := NewProjectCreateOptions()
// The error is not handled at this point, it will be handled during Context creation
kubclient, _ := kclient.New()
o := NewProjectCreateOptions(project.NewClient(kubclient))

projectCreateCmd := &cobra.Command{
Use: name,
Expand Down
20 changes: 14 additions & 6 deletions pkg/odo/cli/project/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

odoerrors "github.com/redhat-developer/odo/pkg/errors"
"github.com/redhat-developer/odo/pkg/kclient"
"github.com/redhat-developer/odo/pkg/log"
"github.com/redhat-developer/odo/pkg/machineoutput"
"github.com/redhat-developer/odo/pkg/odo/cli/ui"
Expand Down Expand Up @@ -36,6 +37,9 @@ type ProjectDeleteOptions struct {
// Context
*genericclioptions.Context

// Clients
prjClient project.Client

// Parameters
projectName string

Expand All @@ -45,8 +49,10 @@ type ProjectDeleteOptions struct {
}

// NewProjectDeleteOptions creates a ProjectDeleteOptions instance
func NewProjectDeleteOptions() *ProjectDeleteOptions {
return &ProjectDeleteOptions{}
func NewProjectDeleteOptions(prjClient project.Client) *ProjectDeleteOptions {
return &ProjectDeleteOptions{
prjClient: prjClient,
}
}

// Complete completes ProjectDeleteOptions after they've been created
Expand All @@ -59,7 +65,7 @@ func (pdo *ProjectDeleteOptions) Complete(name string, cmdline cmdline.Cmdline,
// Validate validates the parameters of the ProjectDeleteOptions
func (pdo *ProjectDeleteOptions) Validate() error {
// Validate existence of the project to be deleted
isValidProject, err := project.Exists(pdo.Context.KClient, pdo.projectName)
isValidProject, err := pdo.prjClient.Exists(pdo.projectName)
if kerrors.IsForbidden(err) {
return &odoerrors.Unauthorized{}
}
Expand All @@ -76,7 +82,7 @@ func (pdo *ProjectDeleteOptions) Run() (err error) {
s := &log.Status{}

// This to set the project in the file and runtime
err = project.SetCurrent(pdo.Context.KClient, pdo.projectName)
err = pdo.prjClient.SetCurrent(pdo.projectName)
if err != nil {
return err
}
Expand All @@ -97,7 +103,7 @@ func (pdo *ProjectDeleteOptions) Run() (err error) {
defer s.End(false)
}

err := project.Delete(pdo.Context.KClient, pdo.projectName, pdo.waitFlag)
err := pdo.prjClient.Delete(pdo.projectName, pdo.waitFlag)
if err != nil {
return err
}
Expand All @@ -118,7 +124,9 @@ func (pdo *ProjectDeleteOptions) Run() (err error) {

// NewCmdProjectDelete creates the project delete command
func NewCmdProjectDelete(name, fullName string) *cobra.Command {
o := NewProjectDeleteOptions()
// The error is not handled at this point, it will be handled during Context creation
kubclient, _ := kclient.New()
o := NewProjectDeleteOptions(project.NewClient(kubclient))

projectDeleteCmd := &cobra.Command{
Use: name,
Expand Down
16 changes: 12 additions & 4 deletions pkg/odo/cli/project/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"text/tabwriter"

"github.com/redhat-developer/odo/pkg/kclient"
"github.com/redhat-developer/odo/pkg/log"
"github.com/redhat-developer/odo/pkg/machineoutput"
"github.com/redhat-developer/odo/pkg/odo/cmdline"
Expand All @@ -30,11 +31,16 @@ var (
type ProjectListOptions struct {
// Context
*genericclioptions.Context

// Clients
prjClient project.Client
}

// NewProjectListOptions creates a new ProjectListOptions instance
func NewProjectListOptions() *ProjectListOptions {
return &ProjectListOptions{}
func NewProjectListOptions(prjClient project.Client) *ProjectListOptions {
return &ProjectListOptions{
prjClient: prjClient,
}
}

// Complete completes ProjectListOptions after they've been created
Expand All @@ -50,7 +56,7 @@ func (plo *ProjectListOptions) Validate() (err error) {

// Run contains the logic for the odo project list command
func (plo *ProjectListOptions) Run() error {
projects, err := project.List(plo.Context.KClient)
projects, err := plo.prjClient.List()
if err != nil {
return err
}
Expand All @@ -68,7 +74,9 @@ func (plo *ProjectListOptions) Run() error {

// NewCmdProjectList implements the odo project list command.
func NewCmdProjectList(name, fullName string) *cobra.Command {
o := NewProjectListOptions()
// The error is not handled at this point, it will be handled during Context creation
kubclient, _ := kclient.New()
o := NewProjectListOptions(project.NewClient(kubclient))
projectListCmd := &cobra.Command{
Use: name,
Short: listLongDesc,
Expand Down
Loading

0 comments on commit e0f6d31

Please sign in to comment.