Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package main |
| 17 | |
| 18 | import ( |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 19 | flags "github.com/jessevdk/go-flags" |
Scott Baker | 2b0ad65 | 2019-08-21 14:57:07 -0700 | [diff] [blame] | 20 | "github.com/opencord/voltctl/internal/pkg/commands" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 21 | "os" |
| 22 | "path" |
| 23 | ) |
| 24 | |
| 25 | func main() { |
| 26 | |
David Bainbridge | 85a2867 | 2019-10-18 01:16:28 +0000 | [diff] [blame] | 27 | /* |
| 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) |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame^] | 45 | 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 Bainbridge | 85a2867 | 2019-10-18 01:16:28 +0000 | [diff] [blame] | 47 | } |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame^] | 48 | if _, err := pp.Parse(); err != nil { |
| 49 | commands.Error.Fatalf("Unable to parse command line options for command completion: %s", err.Error()) |
| 50 | } |
David Bainbridge | 85a2867 | 2019-10-18 01:16:28 +0000 | [diff] [blame] | 51 | os.Setenv("GO_FLAGS_COMPLETION", compval) |
| 52 | } |
| 53 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 54 | parser := flags.NewNamedParser(path.Base(os.Args[0]), flags.Default|flags.PassAfterNonOption) |
| 55 | _, err := parser.AddGroup("Global Options", "", &commands.GlobalOptions) |
| 56 | if err != nil { |
| 57 | panic(err) |
| 58 | } |
| 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 Baker | d69e422 | 2019-08-21 16:46:05 -0700 | [diff] [blame] | 67 | commands.RegisterLogLevelCommands(parser) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 68 | |
David Bainbridge | 85a2867 | 2019-10-18 01:16:28 +0000 | [diff] [blame] | 69 | _, err = parser.Parse() |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 70 | 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 { |
| 78 | panic(err) |
| 79 | } |
| 80 | os.Exit(1) |
| 81 | } |
| 82 | } |