Skip to content
Snippets Groups Projects
Commit 7ba08868 authored by Fabian Seidl's avatar Fabian Seidl
Browse files

Resolve "Refactor CLI commands of network elements"

See merge request !529
parent b3297bcd
Branches
No related tags found
1 merge request!529Resolve "Refactor CLI commands of network elements"
Pipeline #160453 passed
/*
Copyright © 2021 da/net Research Group <danet@h-da.de>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
package cmd
import (
"github.com/spf13/cobra"
)
// networkElementPathCmd represents the path interaction command.
var networkElementPathCmd = &cobra.Command{
Use: "path {get,set,delete}",
Short: "the path command contains all sub-commands for YANG path interaction",
Long: `The path command contains all sub-commands for YANG path interaction,
including get, set and delete operations.`,
}
func init() {
networkElementCmd.AddCommand(networkElementPathCmd)
}
......@@ -40,8 +40,8 @@ import (
"github.com/spf13/cobra"
)
// networkElementDeleteCmd represents the delete command.
var networkElementDeleteCmd = &cobra.Command{
// networkElementPathDeleteCmd represents the delete path command.
var networkElementPathDeleteCmd = &cobra.Command{
Use: "delete [uuid] [path]",
Args: cobra.ExactArgs(2),
Short: "delete a path on a network element",
......@@ -86,5 +86,5 @@ The network element UUID and request path must be specified as a positional argu
}
func init() {
networkElementCmd.AddCommand(networkElementDeleteCmd)
networkElementPathCmd.AddCommand(networkElementPathDeleteCmd)
}
......@@ -39,8 +39,8 @@ import (
"google.golang.org/protobuf/encoding/protojson"
)
// networkElementGetCmd represents the get command.
var networkElementGetPathCmd = &cobra.Command{
// networkElementPathGetCmd represents the get path command.
var networkElementPathGetCmd = &cobra.Command{
Use: "get [uuid] [path]",
Args: cobra.ExactArgs(2),
Short: "gets a path from a specified network element on the controller",
......@@ -96,5 +96,5 @@ The network element UUID and request path must be specified as a positional argu
}
func init() {
networkElementCmd.AddCommand(networkElementGetPathCmd)
networkElementPathCmd.AddCommand(networkElementPathGetCmd)
}
......@@ -41,8 +41,8 @@ import (
"google.golang.org/protobuf/encoding/protojson"
)
// networkElementGetCmd represents the get command.
var networkElementGetIntendedPathCmd = &cobra.Command{
// networkElementPathGetIntendedCmd represents the get intended path command.
var networkElementPathGetIntendedCmd = &cobra.Command{
Use: "getIntended [uuid] [path]",
Args: cobra.ExactArgs(2),
Short: "gets the intended path from a specified network element on the controller",
......@@ -106,5 +106,5 @@ The network element UUID and request path must be specified as a positional argu
}
func init() {
networkElementCmd.AddCommand(networkElementGetIntendedPathCmd)
networkElementPathCmd.AddCommand(networkElementPathGetIntendedCmd)
}
......@@ -47,12 +47,12 @@ var replace bool
var file string
var forcePush bool
// networkElementSetCmd represents the set command.
var networkElementSetCmd = &cobra.Command{
// networkElementPathSetCmd represents the set path command.
var networkElementPathSetCmd = &cobra.Command{
Use: "set [uuid] [path] [value]",
Args: cobra.RangeArgs(2, 3),
Short: "set a value on a network element",
Long: `Set a path value for a given orchestrated network network element. Only one path and
Long: `Set a path value for a given managed network element. Only one path and
only one value supported for now.
The network element UUID, request path and value must be specified as positional arguments.
......@@ -159,8 +159,8 @@ func fileContentToString(path string) (string, error) {
}
func init() {
networkElementCmd.AddCommand(networkElementSetCmd)
networkElementSetCmd.Flags().BoolVarP(&replace, "replace", "r", false, "enables replace behaviour")
networkElementSetCmd.Flags().StringVar(&file, "file", "", "reference the path to a file containing your changes as JSON")
networkElementSetCmd.Flags().BoolVar(&forcePush, "force-push", false, "enables the possibility to instantly push the set without commit/confirm")
networkElementPathCmd.AddCommand(networkElementPathSetCmd)
networkElementPathSetCmd.Flags().BoolVarP(&replace, "replace", "r", false, "enables replace behaviour")
networkElementPathSetCmd.Flags().StringVar(&file, "file", "", "reference the path to a file containing your changes as JSON")
networkElementPathSetCmd.Flags().BoolVar(&forcePush, "force-push", false, "enables the possibility to instantly push the set without commit/confirm")
}
......@@ -135,11 +135,11 @@ func filterFlagSlice(input []string) (commandSlice []string, flagSlice []string)
return commandSlice, flagSlice
}
func networkElementGetCompletion(c *PromptCompleter, d prompt.Document, inputSplit []string) []prompt.Suggest {
func networkElementPathCompletion(c *PromptCompleter, d prompt.Document, inputSplit []string) []prompt.Suggest {
switch inputLen := len(inputSplit); inputLen {
case 2:
return c.updateSuggestionsThroughFunc(d, getNetworkElements)
case 3:
return c.updateSuggestionsThroughFunc(d, getNetworkElements)
case 4:
id, err := uuid.Parse(inputSplit[inputLen-1])
if err != nil {
return c.updateSuggestionsThroughFunc(d, getNetworkElements)
......@@ -155,7 +155,7 @@ func networkElementGetCompletion(c *PromptCompleter, d prompt.Document, inputSpl
if yc, ok := c.YangSchemaCompleterMap[id]; ok {
return yc.Complete(d)
}
case 4:
case 5:
if d.GetWordBeforeCursor() == "" || d.GetWordAfterCursor() != "" {
id, err := uuid.Parse(inputSplit[inputLen-2])
if err != nil {
......@@ -243,8 +243,8 @@ func completionBasedOnCmd(c *PromptCompleter, cmd *cobra.Command, inputSplit []s
} else {
return cobraCommandCompletion(cmd, d, inputFlags, []prompt.Suggest{})
}
case networkElementGetPathCmd, networkElementGetIntendedPathCmd, networkElementSetCmd:
return networkElementGetCompletion(c, d, inputSplitFiltered)
case networkElementPathGetCmd, networkElementPathGetIntendedCmd, networkElementPathSetCmd:
return networkElementPathCompletion(c, d, inputSplitFiltered)
case networkElementShowCmd:
networkElements, err := getNetworkElements()
if err != nil {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment