SDL CLI command to generate sdlcli shell completion file for bash
[ric-plt/sdlgo.git] / internal / cli / completion.go
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
+}