|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 The GoPlus Authors (goplus.org). All rights reserved. |
| 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 | + |
| 17 | +package cobra |
| 18 | + |
| 19 | +import ( |
| 20 | + "reflect" |
| 21 | + |
| 22 | + "github.com/spf13/cobra" |
| 23 | +) |
| 24 | + |
| 25 | +const ( |
| 26 | + GopPackage = true |
| 27 | +) |
| 28 | + |
| 29 | +// ----------------------------------------------------------------------------- |
| 30 | + |
| 31 | +// Command is the worker class of Cobra classfile. |
| 32 | +type Command struct { |
| 33 | + cobra.Command |
| 34 | +} |
| 35 | + |
| 36 | +func (p *Command) cobraCmd() *cobra.Command { |
| 37 | + return &p.Command |
| 38 | +} |
| 39 | + |
| 40 | +// Main is required by Go+ compiler as the entry of a Cobra command. |
| 41 | +func (p *Command) Main(fname string) { |
| 42 | + p.Command.Use = fname |
| 43 | +} |
| 44 | + |
| 45 | +// Use sets the one-line usage message. |
| 46 | +func (p *Command) Use(use string) { |
| 47 | + p.Command.Use = use |
| 48 | +} |
| 49 | + |
| 50 | +// Short sets the short description shown in the 'help' output. |
| 51 | +func (p *Command) Short(s string) { |
| 52 | + p.Command.Short = s |
| 53 | +} |
| 54 | + |
| 55 | +// Long sets the long message shown in the 'help <this-command>' output. |
| 56 | +func (p *Command) Long(l string) { |
| 57 | + p.Command.Long = l |
| 58 | +} |
| 59 | + |
| 60 | +// Run sets the actual work function. Most commands will only implement this. |
| 61 | +func (p *Command) Run(fn func()) { |
| 62 | + p.Command.Run = func(cmd *cobra.Command, args []string) { |
| 63 | + fn() |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// ----------------------------------------------------------------------------- |
| 68 | + |
| 69 | +// App is the project class of Cobra classfile. |
| 70 | +type App struct { |
| 71 | + Command |
| 72 | +} |
| 73 | + |
| 74 | +func (p *App) initApp(projname string) *Command { |
| 75 | + p.Use(projname) |
| 76 | + return &p.Command |
| 77 | +} |
| 78 | + |
| 79 | +type iAppProto interface { |
| 80 | + initApp(projname string) *Command |
| 81 | + Classprojname() string |
| 82 | +} |
| 83 | + |
| 84 | +type iCommandProto interface { |
| 85 | + cobraCmd() *cobra.Command |
| 86 | + Main(fname string) |
| 87 | + Classfname() string |
| 88 | +} |
| 89 | + |
| 90 | +// Gopt_App_Main is required by Go+ compiler as the entry of a Cobra project. |
| 91 | +func Gopt_App_Main(app iAppProto, cmds ...iCommandProto) { |
| 92 | + root := app.initApp(app.Classprojname()) |
| 93 | + for _, cmd := range cmds { |
| 94 | + reflect.ValueOf(cmd).Elem().Field(1).Set(reflect.ValueOf(app)) // (*command).App = app |
| 95 | + cmd.Main(cmd.Classfname()) |
| 96 | + root.AddCommand(cmd.cobraCmd()) |
| 97 | + } |
| 98 | + root.Execute() |
| 99 | +} |
| 100 | + |
| 101 | +// ----------------------------------------------------------------------------- |
0 commit comments