Skip to content

Commit

Permalink
feat: add update project command
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrox committed Feb 6, 2024
1 parent 71177cb commit a155b15
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
55 changes: 55 additions & 0 deletions update/project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package update

import (
"context"
"fmt"
"log"

"github.com/crossplane/crossplane-runtime/pkg/resource"
management "github.com/ninech/apis/management/v1alpha1"
"github.com/ninech/nctl/api"
"github.com/ninech/nctl/auth"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type projectCmd struct {
Name string `arg:"" default:"" help:"Name of the project to update."`
DisplayName *string `default:"" help:"Display Name of the project."`
}

func (cmd *projectCmd) Run(ctx context.Context, client *api.Client) error {
cfg, err := auth.ReadConfig(client.KubeconfigPath, client.KubeconfigContext)
if err != nil {
if auth.IsConfigNotFoundError(err) {
return auth.ReloginNeeded(err)
}
return err
}

log.Println(cfg.Organization)
project := &management.Project{
ObjectMeta: metav1.ObjectMeta{
Name: cmd.Name,
Namespace: cfg.Organization,
},
}

upd := newUpdater(client, project, management.ProjectKind, func(current resource.Managed) error {
project, ok := current.(*management.Project)
if !ok {
return fmt.Errorf("resource is of type %T, expected %T", current, management.Project{})
}

cmd.applyUpdates(project)

return nil
})

return upd.Update(ctx)
}

func (cmd *projectCmd) applyUpdates(project *management.Project) {
if cmd.DisplayName != nil {
project.Spec.DisplayName = *cmd.DisplayName
}
}
3 changes: 2 additions & 1 deletion update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
type Cmd struct {
Application applicationCmd `cmd:"" group:"deplo.io" name:"application" aliases:"app" help:"Update an existing deplo.io Application. (Beta - requires access)"`
Config configCmd `cmd:"" group:"deplo.io" name:"config" help:"Update an existing deplo.io Project Configuration. (Beta - requires access)"`
Project projectCmd `cmd:"" group:"management.nine.ch" name:"project" help:"Update an existing Project"`
}

type updater struct {
Expand All @@ -27,7 +28,7 @@ func newUpdater(client *api.Client, mg resource.Managed, kind string, f updateFu
}

func (u *updater) Update(ctx context.Context) error {
if err := u.client.Get(ctx, u.client.Name(u.mg.GetName()), u.mg); err != nil {
if err := u.client.Get(ctx, api.ObjectName(u.mg), u.mg); err != nil {
return err
}

Expand Down

0 comments on commit a155b15

Please sign in to comment.