-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
106 lines (89 loc) · 2.88 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright © 2024 Meroxa, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"fmt"
"os"
"github.com/conduitio/ecdysis"
)
func main() {
e := ecdysis.New()
cmd := e.MustBuildCobraCommand(&RootCommand{})
if err := cmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
type RootFlags struct {
Config string `long:"config" usage:"config file (default is $HOME/.example-cli.yaml)" persistent:"true"`
Author string `long:"author" short:"a" usage:"author name for copyright attribution" persistent:"true"`
License string `long:"license" short:"l" usage:"name of license for the project" persistent:"true"`
Viper bool `long:"viper" usage:"use Viper for configuration" persistent:"true"`
}
type RootCommand struct {
flags RootFlags
}
var (
_ ecdysis.CommandWithFlags = (*RootCommand)(nil)
_ ecdysis.CommandWithDocs = (*RootCommand)(nil)
_ ecdysis.CommandWithSubCommands = (*RootCommand)(nil)
)
func (c *RootCommand) Usage() string { return "example-cli" }
func (c *RootCommand) Flags() []ecdysis.Flag {
flags := ecdysis.BuildFlags(&c.flags)
flags.SetDefault("author", "YOUR NAME")
flags.SetDefault("viper", true)
return flags
}
func (c *RootCommand) Docs() ecdysis.Docs {
return ecdysis.Docs{
Short: "An example CLI for ecdysis based Applications",
Long: `Example CLI showcases the power of ecdysis.
This application is an example made using ecdysis,
a wrapper around Cobra that allows you to declare
commands as Go types.`,
}
}
func (c *RootCommand) SubCommands() []ecdysis.Command {
return []ecdysis.Command{
// inject root flags in sub-command
&AddCommand{rootFlags: &c.flags},
&VersionCommand{},
}
}
type AddCommand struct {
rootFlags *RootFlags
}
var _ ecdysis.CommandWithExecute = (*AddCommand)(nil)
func (c *AddCommand) Usage() string { return "add" }
func (c *AddCommand) Execute(context.Context) error {
fmt.Printf("root flags: %#v\n", c.rootFlags)
return nil
}
type VersionCommand struct{}
var (
_ ecdysis.CommandWithExecute = (*VersionCommand)(nil)
_ ecdysis.CommandWithDocs = (*VersionCommand)(nil)
)
func (c *VersionCommand) Usage() string { return "version" }
func (c *VersionCommand) Docs() ecdysis.Docs {
return ecdysis.Docs{
Short: "Print the version number of example-cli",
}
}
func (c *VersionCommand) Execute(context.Context) error {
fmt.Println("example-cli v0.1.0")
return nil
}