SDL CLI command to generate sdlcli shell completion file for bash 20/7520/4
authorPetri Ovaska <petri.ovaska@nokia.com>
Mon, 10 Jan 2022 13:56:47 +0000 (15:56 +0200)
committerPetri Ovaska <petri.ovaska@nokia.com>
Thu, 13 Jan 2022 08:32:45 +0000 (10:32 +0200)
Output shell completion code for the bash shell.

 Usage:
   sdlcli completion bash

Added a new SDL CLI 'completion' command to generate auto-complete
file for the bash shells.

Version: 0.9.4

Issue-Id: RIC-874
Change-Id: Id5a5d14cfe3c5eb2a7815439c5b2a9b94271a2bd
Signed-off-by: Petri Ovaska <petri.ovaska@nokia.com>
docs/release-notes.rst
internal/cli/cli_private_fn_test.go
internal/cli/completion.go [new file with mode: 0644]
internal/cli/completion_test.go [new file with mode: 0644]

index c5bc735..9c22980 100644 (file)
@@ -1,6 +1,6 @@
 ..
 ..  Copyright (c) 2019 AT&T Intellectual Property.
-..  Copyright (c) 2019 Nokia.
+..  Copyright (c) 2019-2022 Nokia.
 ..
 ..  Licensed under the Creative Commons Attribution 4.0 International
 ..  Public License (the "License"); you may not use this file except
@@ -29,6 +29,10 @@ This document provides the release notes of the sdlgo.
 
 Version history
 ---------------
+[0.9.4] - 2022-01-12
+
+* SDL CLI command to generate sdlcli shell completion file for bash
+
 [0.9.3] - 2021-12-30
 
 * Fix SDL CLI get -command to write results stdout stream when command success
index 2a20a51..34e3ab3 100644 (file)
@@ -1,6 +1,6 @@
 /*
    Copyright (c) 2021 AT&T Intellectual Property.
-   Copyright (c) 2018-2021 Nokia.
+   Copyright (c) 2018-2022 Nokia.
 
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
@@ -31,4 +31,5 @@ var (
        NewRemoveCmdForTest     = newRemoveCmd
        NewNamespacesCmdForTest = newNamespacesCmd
        NewStatisticsCmd        = newStatisticsCmd
+       NewCompletionCmdForTest = newCompletionCmd
 )
diff --git a/internal/cli/completion.go b/internal/cli/completion.go
new file mode 100644 (file)
index 0000000..1a6181d
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+   Copyright (c) 2022 Nokia.
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+/*
+ * This source code is part of the near-RT RIC (RAN Intelligent Controller)
+ * platform project (RICP).
+ */
+
+package cli
+
+import (
+       "io"
+       "os"
+
+       "github.com/spf13/cobra"
+)
+
+func init() {
+       rootCmd.AddCommand(newCompletionCmd(os.Stdout))
+}
+
+var completionLong = `# To load bash completions:
+source <(sdlcli completion bash)
+
+# To load completions for each session, execute once:
+sdlcli completion bash > /usr/share/bash-completion/completions/sdlcli
+`
+
+func newCompletionCmd(out io.Writer) *cobra.Command {
+       cmd := &cobra.Command{
+               Use:                   "completion [bash]",
+               Short:                 "Generate shell completion script",
+               Long:                  completionLong,
+               DisableFlagsInUseLine: true,
+               ValidArgs:             []string{"bash"},
+               Args:                  cobra.ExactValidArgs(1),
+               Run: func(cmd *cobra.Command, args []string) {
+                       switch args[0] {
+                       case "bash":
+                               cmd.Root().GenBashCompletion(out)
+                       }
+               },
+       }
+       return cmd
+}
diff --git a/internal/cli/completion_test.go b/internal/cli/completion_test.go
new file mode 100644 (file)
index 0000000..7d72068
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+   Copyright (c) 2022 Nokia.
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+/*
+ * This source code is part of the near-RT RIC (RAN Intelligent Controller)
+ * platform project (RICP).
+ */
+
+package cli_test
+
+import (
+       "bytes"
+       "errors"
+       "gerrit.o-ran-sc.org/r/ric-plt/sdlgo/internal/cli"
+       "github.com/stretchr/testify/assert"
+       "testing"
+)
+
+func TestCompletionHelp(t *testing.T) {
+       var expOkErr error
+       expHelp := "To load bash completions:\nsource <(sdlcli completion bash)"
+       expNokErr := errors.New("invalid argument \"zsh\" for \"completion\"")
+       expNokHelp := "Usage:\n  completion [bash]"
+       tests := []struct {
+               args      string
+               expErr    error
+               expOutput string
+       }{
+               {args: "-h", expErr: expOkErr, expOutput: expHelp},
+               {args: "--help", expErr: expOkErr, expOutput: expHelp},
+               {args: "zsh", expErr: expNokErr, expOutput: expNokHelp},
+       }
+
+       for _, test := range tests {
+               buf := new(bytes.Buffer)
+               cmd := cli.NewCompletionCmdForTest(buf)
+               cmd.SetOut(buf)
+               cmd.SetErr(buf)
+               cmd.SetArgs([]string{test.args})
+
+               err := cmd.Execute()
+               result := buf.String()
+               assert.Equal(t, test.expErr, err)
+               assert.Contains(t, result, test.expOutput)
+       }
+}
+
+func TestCompletionBash(t *testing.T) {
+       buf := new(bytes.Buffer)
+       cmd := cli.NewCompletionCmdForTest(buf)
+       cmd.SetArgs([]string{"bash"})
+
+       err := cmd.Execute()
+       result := buf.String()
+       assert.Nil(t, err)
+       assert.Contains(t, result, "# bash completion for completion")
+}