From c8f47fc03fce04f7e1aa85e8c18007641d7bff9a Mon Sep 17 00:00:00 2001
From: Malte Bauch <malte.bauch@stud.h-da.de>
Date: Mon, 11 Sep 2023 14:26:33 +0200
Subject: [PATCH] All suggestions get functions are provided with deadline
 context

Interrupt is not possible, since Keypresses are only executed after the
specific functions has finished. os.Interrupt is not possible aswell,
since it also triggers the handleSignals go routine within go-prompt and
therefore shuts down the whole program.
---
 cli/cmd/prompt.go | 21 +++++++--------------
 cli/cmd/utils.go  |  2 +-
 2 files changed, 8 insertions(+), 15 deletions(-)

diff --git a/cli/cmd/prompt.go b/cli/cmd/prompt.go
index fcbfb97cc..54d2ec6b3 100644
--- a/cli/cmd/prompt.go
+++ b/cli/cmd/prompt.go
@@ -346,8 +346,7 @@ func completionBasedOnCmd(c *PromptCompleter, cmd *cobra.Command, inputSplit []s
 func getNetworkElements() ([]prompt.Suggest, error) {
 	spinner, _ := pterm.DefaultSpinner.Start("Fetching Network Elements from controller.")
 	// create a authorizedContext for further requests
-	ctx, ctxCancelFn := createContextWithAuthorization()
-	startContextListener(ctx, ctxCancelFn)
+	ctx, _ := createContextWithAuthorization()
 	resp, err := pndAdapter.GetFlattenedNetworkElements(ctx)
 	if err != nil {
 		spinner.Fail(err)
@@ -365,8 +364,7 @@ func getNetworkElements() ([]prompt.Suggest, error) {
 func getAvailablePlugins() ([]prompt.Suggest, error) {
 	spinner, _ := pterm.DefaultSpinner.Start("Fetching available plugins from controller.")
 	// create a authorizedContext for further requests
-	ctx, ctxCancelFn := createContextWithAuthorization()
-	startContextListener(ctx, ctxCancelFn)
+	ctx, _ := createContextWithAuthorization()
 	resp, err := pndAdapter.GetAvailablePlugins(ctx)
 	if err != nil {
 		spinner.Fail(err)
@@ -387,8 +385,7 @@ func getAvailablePlugins() ([]prompt.Suggest, error) {
 func getSchemaTreeForNetworkElementID(id uuid.UUID) (map[string]*yang.Entry, error) {
 	spinner, _ := pterm.DefaultSpinner.Start("Fetching schema tree for Device with ID: ", id)
 	// create a authorizedContext for further requests
-	ctx, ctxCancelFn := createContextWithAuthorization()
-	startContextListener(ctx, ctxCancelFn)
+	ctx, _ := createContextWithAuthorization()
 	networkElement, err := pndAdapter.GetFlattenedNetworkElement(ctx, id.String())
 	if err != nil {
 		spinner.Fail(err)
@@ -398,8 +395,7 @@ func getSchemaTreeForNetworkElementID(id uuid.UUID) (map[string]*yang.Entry, err
 	pluginID := networkElement.GetMne().GetPluginid()
 	pluginUUID := uuid.MustParse(pluginID)
 	// create a authorizedContext for further requests
-	ctx, ctxCancelFn = createContextWithAuthorization()
-	startContextListener(ctx, ctxCancelFn)
+	ctx, _ = createContextWithAuthorization()
 	schemaTree, err := pndAdapter.GetPluginSchemaTree(ctx, pluginUUID)
 	if err != nil {
 		spinner.Fail(err)
@@ -415,8 +411,7 @@ func getSchemaTreeForNetworkElementID(id uuid.UUID) (map[string]*yang.Entry, err
 func getPnds() ([]prompt.Suggest, error) {
 	spinner, _ := pterm.DefaultSpinner.Start("Fetching PNDs from controller.")
 	// create a authorizedContext for further requests
-	ctx, ctxCancelFn := createContextWithAuthorization()
-	startContextListener(ctx, ctxCancelFn)
+	ctx, _ := createContextWithAuthorization()
 	resp, err := api.GetIds(ctx, viper.GetString("controllerAPIEndpoint"))
 	if err != nil {
 		spinner.Fail(err)
@@ -438,8 +433,7 @@ func getPnds() ([]prompt.Suggest, error) {
 func getPendingChanges() ([]prompt.Suggest, error) {
 	spinner, _ := pterm.DefaultSpinner.Start("Fetching committed changes.")
 	// create a authorizedContext for further requests
-	ctx, ctxCancelFn := createContextWithAuthorization()
-	startContextListener(ctx, ctxCancelFn)
+	ctx, _ := createContextWithAuthorization()
 	resp, err := pndAdapter.PendingChanges(ctx)
 	if err != nil {
 		spinner.Fail(err)
@@ -461,8 +455,7 @@ func getPendingChanges() ([]prompt.Suggest, error) {
 func getCommittedChanges() ([]prompt.Suggest, error) {
 	spinner, _ := pterm.DefaultSpinner.Start("Fetching pending changes.")
 	// create a authorizedContext for further requests
-	ctx, ctxCancelFn := createContextWithAuthorization()
-	startContextListener(ctx, ctxCancelFn)
+	ctx, _ := createContextWithAuthorization()
 	resp, err := pndAdapter.CommittedChanges(ctx)
 	if err != nil {
 		spinner.Fail(err)
diff --git a/cli/cmd/utils.go b/cli/cmd/utils.go
index 2a849b02a..6bbd626f4 100644
--- a/cli/cmd/utils.go
+++ b/cli/cmd/utils.go
@@ -74,7 +74,7 @@ func sliceContains[T comparable](slice []T, toCompare T) bool {
 func createContextWithAuthorization() (context.Context, context.CancelFunc) {
 	//TODO: try to get token string first, if "" return err, followed by print in cli about required login
 	md := metadata.Pairs("authorize", userToken)
-	deadline := time.Now().Add(10 * time.Second)
+	deadline := time.Now().Add(5 * time.Second)
 	c, cancel := context.WithDeadline(context.Background(), deadline)
 	return metadata.NewOutgoingContext(c, md), cancel
 }
-- 
GitLab