@@ -8,11 +8,18 @@ import (
8
8
9
9
"github.com/b4b4r07/afx/pkg/helpers/templates"
10
10
"github.com/b4b4r07/afx/pkg/printers"
11
+ "github.com/goccy/go-yaml"
11
12
"github.com/spf13/cobra"
12
13
)
13
14
14
15
type showCmd struct {
15
16
metaCmd
17
+
18
+ opt showOpt
19
+ }
20
+
21
+ type showOpt struct {
22
+ output string
16
23
}
17
24
18
25
var (
@@ -21,13 +28,14 @@ var (
21
28
22
29
// showExample is examples for show command
23
30
showExample = templates .Examples (`
24
- afx show
31
+ $ afx show
32
+ $ afx show -o json | jq .github
25
33
` )
26
34
)
27
35
28
36
// newShowCmd creates a new show command
29
37
func (m metaCmd ) newShowCmd () * cobra.Command {
30
- c := & showCmd {m }
38
+ c := & showCmd {metaCmd : m }
31
39
32
40
showCmd := & cobra.Command {
33
41
Use : "show" ,
@@ -39,10 +47,41 @@ func (m metaCmd) newShowCmd() *cobra.Command {
39
47
SilenceErrors : true ,
40
48
Args : cobra .MaximumNArgs (0 ),
41
49
RunE : func (cmd * cobra.Command , args []string ) error {
42
- return c .run (args )
50
+ b , err := yaml .Marshal (m .GetConfig ())
51
+ if err != nil {
52
+ return err
53
+ }
54
+ switch c .opt .output {
55
+ case "default" :
56
+ return c .run (args )
57
+ case "json" :
58
+ yb , err := yaml .YAMLToJSON (b )
59
+ if err != nil {
60
+ return err
61
+ }
62
+ fmt .Println (string (yb ))
63
+ case "yaml" :
64
+ fmt .Println (string (b ))
65
+ case "path" :
66
+ for _ , pkg := range c .GetPackages (c .state .NoChanges ) {
67
+ fmt .Println (pkg .GetHome ())
68
+ }
69
+ default :
70
+ return fmt .Errorf ("%s: not supported output style" , c .opt .output )
71
+ }
72
+ return nil
43
73
},
44
74
}
45
75
76
+ flag := showCmd .Flags ()
77
+ flag .StringVarP (& c .opt .output , "output" , "o" , "default" , "Output style [default,json,yaml,path]" )
78
+
79
+ showCmd .RegisterFlagCompletionFunc ("output" ,
80
+ func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
81
+ out := []string {"default" , "json" , "yaml" , "path" }
82
+ return out , cobra .ShellCompDirectiveNoFileComp
83
+ })
84
+
46
85
return showCmd
47
86
}
48
87
0 commit comments