blob: 999cc1871caa6b0e0de1b67868ffd00d8bb1df82 [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001/*
Joey Armstrong60c4f972023-03-10 14:50:59 -05002 * Copyright 2019-2023 Ciena Corporation
Joey Armstrong903c69d2024-02-01 19:46:39 -05003 * Copyright 2019-2024 Open Networking Foundation (ONF) and the ONF Contributors
Zack Williamse940c7a2019-08-21 14:25:39 -07004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package main
18
19import (
David Bainbridge16a9d692019-11-14 20:04:19 +000020 "fmt"
Zack Williamse940c7a2019-08-21 14:25:39 -070021 "os"
22 "path"
David Bainbridge7052fe82020-03-25 10:37:00 -070023
24 flags "github.com/jessevdk/go-flags"
25 "github.com/opencord/voltctl/internal/pkg/commands"
Zack Williamse940c7a2019-08-21 14:25:39 -070026)
27
28func main() {
29
David Bainbridge85a28672019-10-18 01:16:28 +000030 /*
31 * When completion is invoked the environment variable GO_FLAG_COMPLETION
32 * is set. The go-flags library uses this as a key to do completion when
33 * parsing the arguments. The problem is that when doing compleition the
34 * library doesn't bother setting the arguments into the structures. As
35 * such voltctl's configuration options, in partitular VOLTCONFIG, is
36 * not set and thus connection to voltha servers can not be established
37 * and completion for device ID etc will not work.
38 *
39 * An issue against go-flags has been opened, but as a work around if or
40 * until it is fixed there is a bit of a hack. Unset the environment var
41 * parse the global config options, and then continue with normal
42 * completion.
43 */
44 compval := os.Getenv("GO_FLAGS_COMPLETION")
45 if len(compval) > 0 {
46 os.Unsetenv("GO_FLAGS_COMPLETION")
David Bainbridge0f758d42019-10-26 05:17:48 +000047 pp := flags.NewNamedParser(path.Base(os.Args[0]), flags.HelpFlag|flags.PassDoubleDash|flags.PassAfterNonOption)
David Bainbridgea6722342019-10-24 23:55:53 +000048 if _, err := pp.AddGroup("Global Options", "", &commands.GlobalOptions); err != nil {
49 commands.Error.Fatalf("Unable to set up global options for command completion: %s", err.Error())
David Bainbridge85a28672019-10-18 01:16:28 +000050 }
David Bainbridgea6722342019-10-24 23:55:53 +000051 if _, err := pp.Parse(); err != nil {
52 commands.Error.Fatalf("Unable to parse command line options for command completion: %s", err.Error())
53 }
David Bainbridge85a28672019-10-18 01:16:28 +000054 os.Setenv("GO_FLAGS_COMPLETION", compval)
55 }
56
David Bainbridge0f758d42019-10-26 05:17:48 +000057 parser := flags.NewNamedParser(path.Base(os.Args[0]), flags.HelpFlag|flags.PassDoubleDash|flags.PassAfterNonOption)
Zack Williamse940c7a2019-08-21 14:25:39 -070058 _, err := parser.AddGroup("Global Options", "", &commands.GlobalOptions)
59 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +000060 commands.Error.Fatalf("Unable to parse global command options: %s", err.Error())
Zack Williamse940c7a2019-08-21 14:25:39 -070061 }
62 commands.RegisterAdapterCommands(parser)
63 commands.RegisterDeviceCommands(parser)
64 commands.RegisterLogicalDeviceCommands(parser)
Zack Williamse940c7a2019-08-21 14:25:39 -070065 commands.RegisterVersionCommands(parser)
66 commands.RegisterCompletionCommands(parser)
67 commands.RegisterConfigCommands(parser)
68 commands.RegisterComponentCommands(parser)
Girish Kumarb03dcee2020-04-14 11:48:15 +000069 commands.RegisterLogCommands(parser)
Scott Bakered4efab2020-01-13 19:12:25 -080070 commands.RegisterEventCommands(parser)
David K. Bainbridge9189c632021-03-26 21:52:21 +000071 commands.RegisterStackCommands(parser)
Zack Williamse940c7a2019-08-21 14:25:39 -070072
David Bainbridge85a28672019-10-18 01:16:28 +000073 _, err = parser.Parse()
Zack Williamse940c7a2019-08-21 14:25:39 -070074 if err != nil {
75 _, ok := err.(*flags.Error)
76 if ok {
77 real := err.(*flags.Error)
David Bainbridge16a9d692019-11-14 20:04:19 +000078 // Send help message to stdout, else to stderr
Zack Williamse940c7a2019-08-21 14:25:39 -070079 if real.Type == flags.ErrHelp {
David Bainbridge16a9d692019-11-14 20:04:19 +000080 parser.WriteHelp(os.Stdout)
81 } else {
David Bainbridge7052fe82020-03-25 10:37:00 -070082 if real.Error() != commands.NoReportErr.Error() {
83 fmt.Fprintf(os.Stderr, "%s\n", real.Error())
84 }
85 os.Exit(1)
Zack Williamse940c7a2019-08-21 14:25:39 -070086 }
David Bainbridge16a9d692019-11-14 20:04:19 +000087 return
David Bainbridge7052fe82020-03-25 10:37:00 -070088 } else if err != commands.NoReportErr {
David Bainbridge0f758d42019-10-26 05:17:48 +000089 commands.Error.Fatal(commands.ErrorToString(err))
Zack Williamse940c7a2019-08-21 14:25:39 -070090 }
91 os.Exit(1)
92 }
93}