Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 1 | /* |
Joey Armstrong | 60c4f97 | 2023-03-10 14:50:59 -0500 | [diff] [blame] | 2 | * Copyright 2019-2023 Ciena Corporation |
Joey Armstrong | 903c69d | 2024-02-01 19:46:39 -0500 | [diff] [blame] | 3 | * Copyright 2019-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | package main |
| 18 | |
| 19 | import ( |
David Bainbridge | 16a9d69 | 2019-11-14 20:04:19 +0000 | [diff] [blame] | 20 | "fmt" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 21 | "os" |
| 22 | "path" |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 23 | |
| 24 | flags "github.com/jessevdk/go-flags" |
| 25 | "github.com/opencord/voltctl/internal/pkg/commands" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | func main() { |
| 29 | |
David Bainbridge | 85a2867 | 2019-10-18 01:16:28 +0000 | [diff] [blame] | 30 | /* |
| 31 | * When completion is invoked the environment variable GO_FLAG_COMPLETION |
| 32 | * is set. The go-flags library uses this as a key to do completion when |
| 33 | * parsing the arguments. The problem is that when doing compleition the |
| 34 | * library doesn't bother setting the arguments into the structures. As |
| 35 | * such voltctl's configuration options, in partitular VOLTCONFIG, is |
| 36 | * not set and thus connection to voltha servers can not be established |
| 37 | * and completion for device ID etc will not work. |
| 38 | * |
| 39 | * An issue against go-flags has been opened, but as a work around if or |
| 40 | * until it is fixed there is a bit of a hack. Unset the environment var |
| 41 | * parse the global config options, and then continue with normal |
| 42 | * completion. |
| 43 | */ |
| 44 | compval := os.Getenv("GO_FLAGS_COMPLETION") |
| 45 | if len(compval) > 0 { |
| 46 | os.Unsetenv("GO_FLAGS_COMPLETION") |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 47 | pp := flags.NewNamedParser(path.Base(os.Args[0]), flags.HelpFlag|flags.PassDoubleDash|flags.PassAfterNonOption) |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 48 | if _, err := pp.AddGroup("Global Options", "", &commands.GlobalOptions); err != nil { |
| 49 | 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] | 50 | } |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 51 | if _, err := pp.Parse(); err != nil { |
| 52 | commands.Error.Fatalf("Unable to parse command line options for command completion: %s", err.Error()) |
| 53 | } |
David Bainbridge | 85a2867 | 2019-10-18 01:16:28 +0000 | [diff] [blame] | 54 | os.Setenv("GO_FLAGS_COMPLETION", compval) |
| 55 | } |
| 56 | |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 57 | parser := flags.NewNamedParser(path.Base(os.Args[0]), flags.HelpFlag|flags.PassDoubleDash|flags.PassAfterNonOption) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 58 | _, err := parser.AddGroup("Global Options", "", &commands.GlobalOptions) |
| 59 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 60 | commands.Error.Fatalf("Unable to parse global command options: %s", err.Error()) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 61 | } |
| 62 | commands.RegisterAdapterCommands(parser) |
| 63 | commands.RegisterDeviceCommands(parser) |
| 64 | commands.RegisterLogicalDeviceCommands(parser) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 65 | commands.RegisterVersionCommands(parser) |
| 66 | commands.RegisterCompletionCommands(parser) |
| 67 | commands.RegisterConfigCommands(parser) |
| 68 | commands.RegisterComponentCommands(parser) |
Girish Kumar | b03dcee | 2020-04-14 11:48:15 +0000 | [diff] [blame] | 69 | commands.RegisterLogCommands(parser) |
Scott Baker | ed4efab | 2020-01-13 19:12:25 -0800 | [diff] [blame] | 70 | commands.RegisterEventCommands(parser) |
David K. Bainbridge | 9189c63 | 2021-03-26 21:52:21 +0000 | [diff] [blame] | 71 | commands.RegisterStackCommands(parser) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 72 | |
David Bainbridge | 85a2867 | 2019-10-18 01:16:28 +0000 | [diff] [blame] | 73 | _, err = parser.Parse() |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 74 | if err != nil { |
| 75 | _, ok := err.(*flags.Error) |
| 76 | if ok { |
| 77 | real := err.(*flags.Error) |
David Bainbridge | 16a9d69 | 2019-11-14 20:04:19 +0000 | [diff] [blame] | 78 | // Send help message to stdout, else to stderr |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 79 | if real.Type == flags.ErrHelp { |
David Bainbridge | 16a9d69 | 2019-11-14 20:04:19 +0000 | [diff] [blame] | 80 | parser.WriteHelp(os.Stdout) |
| 81 | } else { |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 82 | if real.Error() != commands.NoReportErr.Error() { |
| 83 | fmt.Fprintf(os.Stderr, "%s\n", real.Error()) |
| 84 | } |
| 85 | os.Exit(1) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 86 | } |
David Bainbridge | 16a9d69 | 2019-11-14 20:04:19 +0000 | [diff] [blame] | 87 | return |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 88 | } else if err != commands.NoReportErr { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 89 | commands.Error.Fatal(commands.ErrorToString(err)) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 90 | } |
| 91 | os.Exit(1) |
| 92 | } |
| 93 | } |