Skip to content

Commit

Permalink
only get token if tab completion was executed
Browse files Browse the repository at this point in the history
This changes the completion mechanism to only gather a valid OIDC token if completion was requested rather then on every invocation of nctl.
  • Loading branch information
thirdeyenick committed Sep 18, 2024
1 parent 1fdf9ab commit abe05b9
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions predictor/predictor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package predictor

import (
"context"
"log"
"os"
"reflect"
"strings"
"time"
Expand Down Expand Up @@ -31,22 +33,29 @@ var argResourceMap = map[string]string{
}

type Resource struct {
client *api.Client
clientCreator func() (*api.Client, error)
client *api.Client
logger *log.Logger
}

func NewResourceName(clientCreator func() (*api.Client, error)) *Resource {
c, err := clientCreator()
if err != nil {
return &Resource{}
return &Resource{
clientCreator: clientCreator,
logger: log.New(os.Stderr, "completion", log.LstdFlags),
}

return &Resource{client: c}
}

func (r *Resource) Predict(args complete.Args) []string {
if r.client == nil {
if r.clientCreator == nil {
return []string{}
}
if r.client == nil {
var err error
if r.client, err = r.clientCreator(); err != nil {
r.logger.Printf("error: %v", err)
return []string{}
}
}

u := &unstructured.UnstructuredList{}
u.SetGroupVersionKind(r.findKind(args.LastCompleted))
Expand All @@ -59,12 +68,14 @@ func (r *Resource) Predict(args complete.Args) []string {
if u.GetObjectKind().GroupVersionKind().Kind == reflect.TypeOf(v1alpha1.ProjectList{}).Name() {
cfg, err := auth.ReadConfig(r.client.KubeconfigPath, r.client.KubeconfigContext)
if err != nil {
r.logger.Printf("error: %v", err)
return []string{}
}
ns = cfg.Organization
}

if err := r.client.List(ctx, u, client.InNamespace(ns)); err != nil {
r.logger.Printf("error: %v", err)
return []string{}
}

Expand Down

0 comments on commit abe05b9

Please sign in to comment.