blob: 02de9920e25ae8915a40bc7c00dd56675e1ddf74 [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 flags "github.com/jessevdk/go-flags"
Scott Baker2b0ad652019-08-21 14:57:07 -070021 "github.com/opencord/voltctl/internal/pkg/commands"
Zack Williamse940c7a2019-08-21 14:25:39 -070022 "os"
23 "path"
24)
25
26func main() {
27
David Bainbridge85a28672019-10-18 01:16:28 +000028 /*
29 * When completion is invoked the environment variable GO_FLAG_COMPLETION
30 * is set. The go-flags library uses this as a key to do completion when
31 * parsing the arguments. The problem is that when doing compleition the
32 * library doesn't bother setting the arguments into the structures. As
33 * such voltctl's configuration options, in partitular VOLTCONFIG, is
34 * not set and thus connection to voltha servers can not be established
35 * and completion for device ID etc will not work.
36 *
37 * An issue against go-flags has been opened, but as a work around if or
38 * until it is fixed there is a bit of a hack. Unset the environment var
39 * parse the global config options, and then continue with normal
40 * completion.
41 */
42 compval := os.Getenv("GO_FLAGS_COMPLETION")
43 if len(compval) > 0 {
44 os.Unsetenv("GO_FLAGS_COMPLETION")
David Bainbridge0f758d42019-10-26 05:17:48 +000045 pp := flags.NewNamedParser(path.Base(os.Args[0]), flags.HelpFlag|flags.PassDoubleDash|flags.PassAfterNonOption)
David Bainbridgea6722342019-10-24 23:55:53 +000046 if _, err := pp.AddGroup("Global Options", "", &commands.GlobalOptions); err != nil {
47 commands.Error.Fatalf("Unable to set up global options for command completion: %s", err.Error())
David Bainbridge85a28672019-10-18 01:16:28 +000048 }
David Bainbridgea6722342019-10-24 23:55:53 +000049 if _, err := pp.Parse(); err != nil {
50 commands.Error.Fatalf("Unable to parse command line options for command completion: %s", err.Error())
51 }
David Bainbridge85a28672019-10-18 01:16:28 +000052 os.Setenv("GO_FLAGS_COMPLETION", compval)
53 }
54
David Bainbridge0f758d42019-10-26 05:17:48 +000055 parser := flags.NewNamedParser(path.Base(os.Args[0]), flags.HelpFlag|flags.PassDoubleDash|flags.PassAfterNonOption)
Zack Williamse940c7a2019-08-21 14:25:39 -070056 _, err := parser.AddGroup("Global Options", "", &commands.GlobalOptions)
57 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +000058 commands.Error.Fatalf("Unable to parse global command options: %s", err.Error())
Zack Williamse940c7a2019-08-21 14:25:39 -070059 }
60 commands.RegisterAdapterCommands(parser)
61 commands.RegisterDeviceCommands(parser)
62 commands.RegisterLogicalDeviceCommands(parser)
63 commands.RegisterDeviceGroupCommands(parser)
64 commands.RegisterVersionCommands(parser)
65 commands.RegisterCompletionCommands(parser)
66 commands.RegisterConfigCommands(parser)
67 commands.RegisterComponentCommands(parser)
Scott Bakerd69e4222019-08-21 16:46:05 -070068 commands.RegisterLogLevelCommands(parser)
Scott Bakered4efab2020-01-13 19:12:25 -080069 commands.RegisterEventCommands(parser)
Zack Williamse940c7a2019-08-21 14:25:39 -070070
David Bainbridge85a28672019-10-18 01:16:28 +000071 _, err = parser.Parse()
Zack Williamse940c7a2019-08-21 14:25:39 -070072 if err != nil {
73 _, ok := err.(*flags.Error)
74 if ok {
75 real := err.(*flags.Error)
David Bainbridge16a9d692019-11-14 20:04:19 +000076 // Send help message to stdout, else to stderr
Zack Williamse940c7a2019-08-21 14:25:39 -070077 if real.Type == flags.ErrHelp {
David Bainbridge16a9d692019-11-14 20:04:19 +000078 parser.WriteHelp(os.Stdout)
79 } else {
80 fmt.Fprintf(os.Stderr, "%s\n", real.Error())
Zack Williamse940c7a2019-08-21 14:25:39 -070081 }
David Bainbridge16a9d692019-11-14 20:04:19 +000082 return
Zack Williamse940c7a2019-08-21 14:25:39 -070083 } else {
David Bainbridge0f758d42019-10-26 05:17:48 +000084 commands.Error.Fatal(commands.ErrorToString(err))
Zack Williamse940c7a2019-08-21 14:25:39 -070085 }
86 os.Exit(1)
87 }
88}