Skip to content
This repository was archived by the owner on Oct 21, 2024. It is now read-only.

Commit 16e95b7

Browse files
committed
change introspect
1 parent db78fc0 commit 16e95b7

File tree

2 files changed

+124
-39
lines changed

2 files changed

+124
-39
lines changed

cmd/sst/dev.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
)
1818

1919
func CmdDev(cli *Cli) error {
20-
args := cli.positionals
20+
args := cli.arguments
2121
hasTarget := len(args) > 0
2222

2323
cfgPath, err := project.Discover()

cmd/sst/main.go

+123-38
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func run() error {
9595
}
9696
for _, c := range last.Children {
9797
if c.Name == arg {
98-
cmd = &c
98+
cmd = c
9999
break
100100
}
101101
}
@@ -118,11 +118,11 @@ func run() error {
118118
flag.Parse()
119119

120120
cli := &Cli{
121-
flags: parsedFlags,
122-
positionals: positionals,
123-
path: cmds,
124-
Context: ctx,
125-
cancel: cancel,
121+
flags: parsedFlags,
122+
arguments: positionals,
123+
path: cmds,
124+
Context: ctx,
125+
cancel: cancel,
126126
}
127127

128128
configureLog(cli)
@@ -155,7 +155,15 @@ func run() error {
155155
spin.Stop()
156156

157157
active := cmds[len(cmds)-1]
158-
if cli.Bool("help") || active.Run == nil {
158+
159+
required := 0
160+
for _, arg := range active.Args {
161+
if !arg.Required {
162+
continue
163+
}
164+
required += 1
165+
}
166+
if cli.Bool("help") || active.Run == nil || len(cli.arguments) != required {
159167
return cli.PrintHelp()
160168
} else {
161169
return active.Run(cli)
@@ -182,7 +190,7 @@ var Root = Command{
182190
Description: "print help",
183191
},
184192
},
185-
Children: []Command{
193+
Children: []*Command{
186194
{
187195
Name: "version",
188196
Description: "print the version",
@@ -194,7 +202,23 @@ var Root = Command{
194202
{
195203
Name: "import",
196204
Description: "import existing resource",
197-
Args: "<type> <name> <id>",
205+
Args: []Argument{
206+
{
207+
Name: "type",
208+
Required: true,
209+
Description: "The type of the resource",
210+
},
211+
{
212+
Name: "name",
213+
Required: true,
214+
Description: "The name of the resource",
215+
},
216+
{
217+
Name: "id",
218+
Required: true,
219+
Description: "The id of the resource",
220+
},
221+
},
198222
Flags: []Flag{
199223
{
200224
Type: "string",
@@ -230,25 +254,39 @@ var Root = Command{
230254
{
231255
Name: "dev",
232256
Description: "run in development mode",
233-
Args: "[target command]",
257+
Args: []Argument{{Name: "command", Description: "The command to run"}},
234258
Run: CmdDev,
235259
},
236260
{
237261
Name: "secret",
238262
Description: "manage secrets",
239-
Children: []Command{
263+
Children: []*Command{
240264
{
241265
Name: "set",
242266
Description: "set a secret",
243-
Args: "<name> <value>",
244-
Examples: []string{
245-
"sst secret set StripeSecret 123456789",
246-
"sst secret set StripeSecret productionsecret --stage=production",
267+
Args: []Argument{
268+
{
269+
Name: "name",
270+
Required: true,
271+
Description: "The name of the secret",
272+
},
273+
{
274+
Name: "value",
275+
Required: true,
276+
Description: "The value of the secret",
277+
},
278+
},
279+
Examples: []Example{
280+
{
281+
Content: "sst secret set StripeSecret 123456789",
282+
Description: "Set the StripeSecret to 123456789",
283+
},
284+
{
285+
Content: "sst secret set StripeSecret productionsecret --stage=production",
286+
Description: "Set the StripeSecret to production",
287+
},
247288
},
248289
Run: func(cli *Cli) error {
249-
if cli.PositionalLength() < 2 {
250-
return cli.PrintHelp()
251-
}
252290
key := cli.Positional(0)
253291
value := cli.Positional(1)
254292
p, err := initProject(cli)
@@ -295,7 +333,7 @@ var Root = Command{
295333
},
296334
{
297335
Name: "shell",
298-
Args: "[command]",
336+
Args: []Argument{{Name: "command", Description: "The command to run"}},
299337
Description: "run command with all resource linked in environment",
300338
Run: func(cli *Cli) error {
301339
p, err := initProject(cli)
@@ -309,7 +347,7 @@ var Root = Command{
309347
if err != nil {
310348
return err
311349
}
312-
args := cli.positionals
350+
args := cli.arguments
313351
if len(args) == 0 {
314352
args = append(args, "sh")
315353
}
@@ -531,7 +569,7 @@ var Root = Command{
531569
{
532570
Name: "state",
533571
Description: "manage state of your deployment",
534-
Children: []Command{
572+
Children: []*Command{
535573
{
536574
Name: "edit",
537575
Description: "edit the state of your deployment",
@@ -574,12 +612,16 @@ var Root = Command{
574612
},
575613
}
576614

615+
func init() {
616+
Root.init()
617+
}
618+
577619
type Cli struct {
578-
flags map[string]interface{}
579-
positionals []string
580-
path CommandPath
581-
Context context.Context
582-
cancel context.CancelFunc
620+
flags map[string]interface{}
621+
arguments []string
622+
path CommandPath
623+
Context context.Context
624+
cancel context.CancelFunc
583625
}
584626

585627
func (c *Cli) Cancel() {
@@ -604,25 +646,68 @@ func (c *Cli) PrintHelp() error {
604646
return c.path.PrintHelp()
605647
}
606648

607-
func (c *Cli) PositionalLength() int {
608-
return len(c.positionals)
649+
func (c *Cli) Arguments() []string {
650+
return c.arguments
609651
}
610652

611653
func (c *Cli) Positional(index int) string {
612-
return c.positionals[index]
654+
return c.arguments[index]
613655
}
614656

615657
type Command struct {
616658
Name string `json:"name"`
617659
Hidden bool `json:"hidden"`
618660
Description string `json:"description"`
619-
Args string `json:"args"`
661+
Args ArgumentList `json:"args"`
620662
Flags []Flag `json:"flags"`
621-
Examples []string `json:"examples"`
622-
Children []Command `json:"children"`
663+
Examples []Example `json:"examples"`
664+
Children []*Command `json:"children"`
623665
Run func(cli *Cli) error `json:"-"`
624666
}
625667

668+
func (c *Command) init() {
669+
if c.Args == nil {
670+
c.Args = ArgumentList{}
671+
}
672+
if c.Flags == nil {
673+
c.Flags = []Flag{}
674+
}
675+
if c.Examples == nil {
676+
c.Examples = []Example{}
677+
}
678+
if c.Children == nil {
679+
c.Children = []*Command{}
680+
}
681+
for _, cmd := range c.Children {
682+
cmd.init()
683+
}
684+
}
685+
686+
type Example struct {
687+
Content string `json:"content"`
688+
Description string `json:"description"`
689+
}
690+
691+
type Argument struct {
692+
Name string `json:"name"`
693+
Description string `json:"description"`
694+
Required bool `json:"required"`
695+
}
696+
697+
type ArgumentList []Argument
698+
699+
func (a ArgumentList) String() string {
700+
args := []string{}
701+
for _, arg := range a {
702+
if arg.Required {
703+
args = append(args, "<"+arg.Name+">")
704+
} else {
705+
args = append(args, "["+arg.Name+"]")
706+
}
707+
}
708+
return strings.Join(args, " ")
709+
}
710+
626711
type Flag struct {
627712
Name string `json:"name"`
628713
Type string `json:"type"`
@@ -650,8 +735,8 @@ func (c CommandPath) PrintHelp() error {
650735
continue
651736
}
652737
next := len(child.Name)
653-
if child.Args != "" {
654-
next += len(child.Args) + 1
738+
if len(child.Args) > 0 {
739+
next += len(child.Args.String())
655740
}
656741
if next > maxSubcommand {
657742
maxSubcommand = next
@@ -666,7 +751,7 @@ func (c CommandPath) PrintHelp() error {
666751
fmt.Printf(
667752
" %s %s %s\n",
668753
strings.Join(prefix, " "),
669-
color.New(color.FgWhite, color.Bold).Sprintf("%-*s", maxSubcommand, strings.Join([]string{child.Name, child.Args}, " ")),
754+
color.New(color.FgWhite, color.Bold).Sprintf("%-*s", maxSubcommand, strings.Join([]string{child.Name, child.Args.String()}, " ")),
670755
child.Description,
671756
)
672757
}
@@ -675,8 +760,8 @@ func (c CommandPath) PrintHelp() error {
675760
if len(active.Children) == 0 {
676761
color.New(color.FgWhite, color.Bold).Print("Usage: ")
677762
color.New(color.FgCyan).Print(strings.Join(prefix, " "))
678-
if active.Args != "" {
679-
color.New(color.FgGreen).Print(" " + active.Args)
763+
if len(active.Args) > 0 {
764+
color.New(color.FgGreen).Print(" " + active.Args.String())
680765
}
681766
fmt.Println()
682767
fmt.Println()
@@ -706,7 +791,7 @@ func (c CommandPath) PrintHelp() error {
706791
fmt.Println()
707792
color.New(color.FgWhite, color.Bold).Print("Examples:\n")
708793
for _, example := range active.Examples {
709-
fmt.Println(" " + example)
794+
fmt.Println(" " + example.Content)
710795
}
711796
}
712797
}

0 commit comments

Comments
 (0)