Skip to content

Commit

Permalink
test moving logic to client
Browse files Browse the repository at this point in the history
  • Loading branch information
gauron99 committed Feb 1, 2024
1 parent 80e54c7 commit 5ddc3d8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
38 changes: 16 additions & 22 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,6 @@ No local files are deleted.
}

func runDelete(cmd *cobra.Command, args []string, newClient ClientFactory) (err error) {

// TODO: (possibly) gauron99 -- I think this whole process of deletion might
// be cleaned up more for example: passing a function as a config from which
// only namespace and name is used, in sub-functions of client.Remove this is
// also the case. Furthermore the namespace being assigned to cfg&f around the
// place might be also on the list.

cfg, err := newDeleteConfig(args).Prompt()
if err != nil {
return
Expand All @@ -75,6 +68,8 @@ func runDelete(cmd *cobra.Command, args []string, newClient ClientFactory) (err
}

var function fn.Function
// initialize namespace from the config
var namespace = cfg.Namespace

// Initialize func with explicit name (when provided)
if len(args) > 0 && args[0] != "" {
Expand All @@ -96,29 +91,28 @@ func runDelete(cmd *cobra.Command, args []string, newClient ClientFactory) (err
return fn.NewErrNotInitialized(function.Root)
}

// --- Namespace determination ---

// use the function's extant namespace -- already deployed Function if viable
// cfg.Namespace can be got from global config (is flag unchanged?)
// use the function's extant namespace -- already deployed function
if !cmd.Flags().Changed("namespace") && function.Deploy.Namespace != "" {
cfg.Namespace = function.Deploy.Namespace
namespace = function.Deploy.Namespace
}
}
ff, err := fn.NewFunction(cfg.Path)
if err != nil {
return
}
if cfg.Namespace == "" {
cfg.Namespace = ff.Deploy.Namespace
}

// assign final namespace to function to be passed to client.Remove
function.Deploy.Namespace = cfg.Namespace
// TODO: gauron99 -- remove this most likely -- moved to client??
// // if still empty, get current function's yaml deployed namespace
// if namespace == "" {
// var f fn.Function
// f, err = fn.NewFunction(cfg.Path)
// if err != nil {
// return
// }
// namespace = f.Deploy.Namespace
// }

// Create a client instance from the now-final config
client, done := newClient(ClientConfig{Namespace: cfg.Namespace, Verbose: cfg.Verbose})
client, done := newClient(ClientConfig{Namespace: namespace, Verbose: cfg.Verbose})
defer done()

function.Deploy.Namespace = namespace
// Invoke remove using the concrete client impl
return client.Remove(cmd.Context(), function, cfg.DeleteAll)
}
Expand Down
19 changes: 14 additions & 5 deletions pkg/functions/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -974,11 +974,11 @@ func (c *Client) List(ctx context.Context) ([]ListItem, error) {
// in which case empty namespace is accepted because its existence is checked
// in the sub functions remover.Remove and pipilines.Remove
func (c *Client) Remove(ctx context.Context, cfg Function, deleteAll bool) error {
// If name is provided, it takes precedence.
// Otherwise load the function defined at root.
functionName := cfg.Name
functionNamespace := cfg.Deploy.Namespace

// If name is provided, it takes precedence.
// Otherwise load the function defined at root.
if cfg.Name == "" {
f, err := NewFunction(cfg.Root)
if err != nil {
Expand All @@ -987,13 +987,22 @@ func (c *Client) Remove(ctx context.Context, cfg Function, deleteAll bool) error
if !f.Initialized() {
return fmt.Errorf("function at %v can not be removed unless initialized. Try removing by name", f.Root)
}
// take the functions name and namespace and load it as current function
functionName = f.Name
if functionNamespace == "" && f.Deploy.Namespace != "" {
functionNamespace = f.Deploy.Namespace
}
functionNamespace = f.Deploy.Namespace
cfg = f
}

// if still empty, get current function's yaml deployed namespace
if functionNamespace == "" {
var f Function
f, err := NewFunction(cfg.Root)
if err != nil {
return err
}
functionNamespace = f.Deploy.Namespace
}

if functionName == "" {
return ErrNameRequired
}
Expand Down

0 comments on commit 5ddc3d8

Please sign in to comment.