blob: 8a342cab94b78960948b403d735167c4e55b82bc [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 (
Zack Williamse940c7a2019-08-21 14:25:39 -070019 flags "github.com/jessevdk/go-flags"
Scott Baker2b0ad652019-08-21 14:57:07 -070020 "github.com/opencord/voltctl/internal/pkg/commands"
Zack Williamse940c7a2019-08-21 14:25:39 -070021 "os"
22 "path"
23)
24
25func main() {
26
David Bainbridge85a28672019-10-18 01:16:28 +000027 /*
28 * When completion is invoked the environment variable GO_FLAG_COMPLETION
29 * is set. The go-flags library uses this as a key to do completion when
30 * parsing the arguments. The problem is that when doing compleition the
31 * library doesn't bother setting the arguments into the structures. As
32 * such voltctl's configuration options, in partitular VOLTCONFIG, is
33 * not set and thus connection to voltha servers can not be established
34 * and completion for device ID etc will not work.
35 *
36 * An issue against go-flags has been opened, but as a work around if or
37 * until it is fixed there is a bit of a hack. Unset the environment var
38 * parse the global config options, and then continue with normal
39 * completion.
40 */
41 compval := os.Getenv("GO_FLAGS_COMPLETION")
42 if len(compval) > 0 {
43 os.Unsetenv("GO_FLAGS_COMPLETION")
David Bainbridge0f758d42019-10-26 05:17:48 +000044 pp := flags.NewNamedParser(path.Base(os.Args[0]), flags.HelpFlag|flags.PassDoubleDash|flags.PassAfterNonOption)
David Bainbridgea6722342019-10-24 23:55:53 +000045 if _, err := pp.AddGroup("Global Options", "", &commands.GlobalOptions); err != nil {
46 commands.Error.Fatalf("Unable to set up global options for command completion: %s", err.Error())
David Bainbridge85a28672019-10-18 01:16:28 +000047 }
David Bainbridgea6722342019-10-24 23:55:53 +000048 if _, err := pp.Parse(); err != nil {
49 commands.Error.Fatalf("Unable to parse command line options for command completion: %s", err.Error())
50 }
David Bainbridge85a28672019-10-18 01:16:28 +000051 os.Setenv("GO_FLAGS_COMPLETION", compval)
52 }
53
David Bainbridge0f758d42019-10-26 05:17:48 +000054 parser := flags.NewNamedParser(path.Base(os.Args[0]), flags.HelpFlag|flags.PassDoubleDash|flags.PassAfterNonOption)
Zack Williamse940c7a2019-08-21 14:25:39 -070055 _, err := parser.AddGroup("Global Options", "", &commands.GlobalOptions)
56 if err != nil {
David Bainbridge0f758d42019-10-26 05:17:48 +000057 commands.Error.Fatalf("Unable to parse global command options: %s", err.Error())
Zack Williamse940c7a2019-08-21 14:25:39 -070058 }
59 commands.RegisterAdapterCommands(parser)
60 commands.RegisterDeviceCommands(parser)
61 commands.RegisterLogicalDeviceCommands(parser)
62 commands.RegisterDeviceGroupCommands(parser)
63 commands.RegisterVersionCommands(parser)
64 commands.RegisterCompletionCommands(parser)
65 commands.RegisterConfigCommands(parser)
66 commands.RegisterComponentCommands(parser)
Scott Bakerd69e4222019-08-21 16:46:05 -070067 commands.RegisterLogLevelCommands(parser)
Zack Williamse940c7a2019-08-21 14:25:39 -070068
David Bainbridge85a28672019-10-18 01:16:28 +000069 _, err = parser.Parse()
Zack Williamse940c7a2019-08-21 14:25:39 -070070 if err != nil {
71 _, ok := err.(*flags.Error)
72 if ok {
73 real := err.(*flags.Error)
74 if real.Type == flags.ErrHelp {
75 return
76 }
77 } else {
David Bainbridge0f758d42019-10-26 05:17:48 +000078 commands.Error.Fatal(commands.ErrorToString(err))
Zack Williamse940c7a2019-08-21 14:25:39 -070079 }
80 os.Exit(1)
81 }
82}