blob: caa0f40db2968b1b48f191e8aaf6f3907e4dede7 [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")
44 pp := flags.NewNamedParser(path.Base(os.Args[0]), flags.Default|flags.PassAfterNonOption)
45 _, err := pp.AddGroup("Global Options", "", &commands.GlobalOptions)
46 if err != nil {
47 panic(err)
48 }
49 pp.Parse()
50 os.Setenv("GO_FLAGS_COMPLETION", compval)
51 }
52
Zack Williamse940c7a2019-08-21 14:25:39 -070053 parser := flags.NewNamedParser(path.Base(os.Args[0]), flags.Default|flags.PassAfterNonOption)
54 _, err := parser.AddGroup("Global Options", "", &commands.GlobalOptions)
55 if err != nil {
56 panic(err)
57 }
58 commands.RegisterAdapterCommands(parser)
59 commands.RegisterDeviceCommands(parser)
60 commands.RegisterLogicalDeviceCommands(parser)
61 commands.RegisterDeviceGroupCommands(parser)
62 commands.RegisterVersionCommands(parser)
63 commands.RegisterCompletionCommands(parser)
64 commands.RegisterConfigCommands(parser)
65 commands.RegisterComponentCommands(parser)
Scott Bakerd69e4222019-08-21 16:46:05 -070066 commands.RegisterLogLevelCommands(parser)
Zack Williamse940c7a2019-08-21 14:25:39 -070067
David Bainbridge85a28672019-10-18 01:16:28 +000068 _, err = parser.Parse()
Zack Williamse940c7a2019-08-21 14:25:39 -070069 if err != nil {
70 _, ok := err.(*flags.Error)
71 if ok {
72 real := err.(*flags.Error)
73 if real.Type == flags.ErrHelp {
74 return
75 }
76 } else {
77 panic(err)
78 }
79 os.Exit(1)
80 }
81}