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 ( |
David Bainbridge | 16a9d69 | 2019-11-14 20:04:19 +0000 | [diff] [blame] | 19 | "fmt" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 20 | "os" |
| 21 | "path" |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 22 | |
| 23 | flags "github.com/jessevdk/go-flags" |
| 24 | "github.com/opencord/voltctl/internal/pkg/commands" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | func main() { |
| 28 | |
David Bainbridge | 85a2867 | 2019-10-18 01:16:28 +0000 | [diff] [blame] | 29 | /* |
| 30 | * When completion is invoked the environment variable GO_FLAG_COMPLETION |
| 31 | * is set. The go-flags library uses this as a key to do completion when |
| 32 | * parsing the arguments. The problem is that when doing compleition the |
| 33 | * library doesn't bother setting the arguments into the structures. As |
| 34 | * such voltctl's configuration options, in partitular VOLTCONFIG, is |
| 35 | * not set and thus connection to voltha servers can not be established |
| 36 | * and completion for device ID etc will not work. |
| 37 | * |
| 38 | * An issue against go-flags has been opened, but as a work around if or |
| 39 | * until it is fixed there is a bit of a hack. Unset the environment var |
| 40 | * parse the global config options, and then continue with normal |
| 41 | * completion. |
| 42 | */ |
| 43 | compval := os.Getenv("GO_FLAGS_COMPLETION") |
| 44 | if len(compval) > 0 { |
| 45 | os.Unsetenv("GO_FLAGS_COMPLETION") |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 46 | 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] | 47 | if _, err := pp.AddGroup("Global Options", "", &commands.GlobalOptions); err != nil { |
| 48 | 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] | 49 | } |
David Bainbridge | a672234 | 2019-10-24 23:55:53 +0000 | [diff] [blame] | 50 | if _, err := pp.Parse(); err != nil { |
| 51 | commands.Error.Fatalf("Unable to parse command line options for command completion: %s", err.Error()) |
| 52 | } |
David Bainbridge | 85a2867 | 2019-10-18 01:16:28 +0000 | [diff] [blame] | 53 | os.Setenv("GO_FLAGS_COMPLETION", compval) |
| 54 | } |
| 55 | |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 56 | 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] | 57 | _, err := parser.AddGroup("Global Options", "", &commands.GlobalOptions) |
| 58 | if err != nil { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 59 | commands.Error.Fatalf("Unable to parse global command options: %s", err.Error()) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 60 | } |
| 61 | commands.RegisterAdapterCommands(parser) |
| 62 | commands.RegisterDeviceCommands(parser) |
| 63 | commands.RegisterLogicalDeviceCommands(parser) |
| 64 | commands.RegisterDeviceGroupCommands(parser) |
| 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) |
Scott Baker | a1e53fa | 2020-04-01 11:13:34 -0700 | [diff] [blame] | 71 | commands.RegisterMessageCommands(parser) |
David K. Bainbridge | 9189c63 | 2021-03-26 21:52:21 +0000 | [diff] [blame] | 72 | commands.RegisterStackCommands(parser) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 73 | |
David Bainbridge | 85a2867 | 2019-10-18 01:16:28 +0000 | [diff] [blame] | 74 | _, err = parser.Parse() |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 75 | if err != nil { |
| 76 | _, ok := err.(*flags.Error) |
| 77 | if ok { |
| 78 | real := err.(*flags.Error) |
David Bainbridge | 16a9d69 | 2019-11-14 20:04:19 +0000 | [diff] [blame] | 79 | // Send help message to stdout, else to stderr |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 80 | if real.Type == flags.ErrHelp { |
David Bainbridge | 16a9d69 | 2019-11-14 20:04:19 +0000 | [diff] [blame] | 81 | parser.WriteHelp(os.Stdout) |
| 82 | } else { |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 83 | if real.Error() != commands.NoReportErr.Error() { |
| 84 | fmt.Fprintf(os.Stderr, "%s\n", real.Error()) |
| 85 | } |
| 86 | os.Exit(1) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 87 | } |
David Bainbridge | 16a9d69 | 2019-11-14 20:04:19 +0000 | [diff] [blame] | 88 | return |
David Bainbridge | 7052fe8 | 2020-03-25 10:37:00 -0700 | [diff] [blame] | 89 | } else if err != commands.NoReportErr { |
David Bainbridge | 0f758d4 | 2019-10-26 05:17:48 +0000 | [diff] [blame] | 90 | commands.Error.Fatal(commands.ErrorToString(err)) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 91 | } |
| 92 | os.Exit(1) |
| 93 | } |
| 94 | } |