blob: 5206c8fc6ecd9f0fa416730d9c72d29cc466a620 [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001/*
2 * Copyright 2019-present Ciena Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package main
17
18import (
David Bainbridge16a9d692019-11-14 20:04:19 +000019 "fmt"
Zack Williamse940c7a2019-08-21 14:25:39 -070020 "os"
21 "path"
David Bainbridge7052fe82020-03-25 10:37:00 -070022
23 flags "github.com/jessevdk/go-flags"
24 "github.com/opencord/voltctl/internal/pkg/commands"
Zack Williamse940c7a2019-08-21 14:25:39 -070025)
26
27func main() {
28
David Bainbridge85a28672019-10-18 01:16:28 +000029 /*
30 * When completion is invoked the environment variable GO_FLAG_COMPLETION
31 * is set. The go-flags library uses this as a key to do completion when
32 * parsing the arguments. The problem is that when doing compleition the
33 * library doesn't bother setting the arguments into the structures. As
34 * such voltctl's configuration options, in partitular VOLTCONFIG, is
35 * not set and thus connection to voltha servers can not be established
36 * and completion for device ID etc will not work.
37 *
38 * An issue against go-flags has been opened, but as a work around if or
39 * until it is fixed there is a bit of a hack. Unset the environment var
40 * parse the global config options, and then continue with normal
41 * completion.
42 */
43 compval := os.Getenv("GO_FLAGS_COMPLETION")
44 if len(compval) > 0 {
45 os.Unsetenv("GO_FLAGS_COMPLETION")
David Bainbridge0f758d42019-10-26 05:17:48 +000046 pp := flags.NewNamedParser(path.Base(os.Args[0]), flags.HelpFlag|flags.PassDoubleDash|flags.PassAfterNonOption)
David Bainbridgea6722342019-10-24 23:55:53 +000047 if _, err := pp.AddGroup("Global Options", "", &commands.GlobalOptions); err != nil {
48 commands.Error.Fatalf("Unable to set up global options for command completion: %s", err.Error())
David Bainbridge85a28672019-10-18 01:16:28 +000049 }
David Bainbridgea6722342019-10-24 23:55:53 +000050 if _, err := pp.Parse(); err != nil {
51 commands.Error.Fatalf("Unable to parse command line options for command completion: %s", err.Error())
52 }
David Bainbridge85a28672019-10-18 01:16:28 +000053 os.Setenv("GO_FLAGS_COMPLETION", compval)
54 }
55
David Bainbridge0f758d42019-10-26 05:17:48 +000056 parser := flags.NewNamedParser(path.Base(os.Args[0]), flags.HelpFlag|flags.PassDoubleDash|flags.PassAfterNonOption)
Zack Williamse940c7a2019-08-21 14:25:39 -070057 _, err := parser.AddGroup("Global Options", "", &commands.GlobalOptions)
58 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +000059 commands.Error.Fatalf("Unable to parse global command options: %s", err.Error())
Zack Williamse940c7a2019-08-21 14:25:39 -070060 }
61 commands.RegisterAdapterCommands(parser)
62 commands.RegisterDeviceCommands(parser)
63 commands.RegisterLogicalDeviceCommands(parser)
Zack Williamse940c7a2019-08-21 14:25:39 -070064 commands.RegisterVersionCommands(parser)
65 commands.RegisterCompletionCommands(parser)
66 commands.RegisterConfigCommands(parser)
67 commands.RegisterComponentCommands(parser)
Girish Kumarb03dcee2020-04-14 11:48:15 +000068 commands.RegisterLogCommands(parser)
Scott Bakered4efab2020-01-13 19:12:25 -080069 commands.RegisterEventCommands(parser)
David K. Bainbridge9189c632021-03-26 21:52:21 +000070 commands.RegisterStackCommands(parser)
Zack Williamse940c7a2019-08-21 14:25:39 -070071
David Bainbridge85a28672019-10-18 01:16:28 +000072 _, err = parser.Parse()
Zack Williamse940c7a2019-08-21 14:25:39 -070073 if err != nil {
74 _, ok := err.(*flags.Error)
75 if ok {
76 real := err.(*flags.Error)
David Bainbridge16a9d692019-11-14 20:04:19 +000077 // Send help message to stdout, else to stderr
Zack Williamse940c7a2019-08-21 14:25:39 -070078 if real.Type == flags.ErrHelp {
David Bainbridge16a9d692019-11-14 20:04:19 +000079 parser.WriteHelp(os.Stdout)
80 } else {
David Bainbridge7052fe82020-03-25 10:37:00 -070081 if real.Error() != commands.NoReportErr.Error() {
82 fmt.Fprintf(os.Stderr, "%s\n", real.Error())
83 }
84 os.Exit(1)
Zack Williamse940c7a2019-08-21 14:25:39 -070085 }
David Bainbridge16a9d692019-11-14 20:04:19 +000086 return
David Bainbridge7052fe82020-03-25 10:37:00 -070087 } else if err != commands.NoReportErr {
David Bainbridge0f758d42019-10-26 05:17:48 +000088 commands.Error.Fatal(commands.ErrorToString(err))
Zack Williamse940c7a2019-08-21 14:25:39 -070089 }
90 os.Exit(1)
91 }
92}