Skip to content

Commit 563de5c

Browse files
authored
Merge pull request #27 from b4b4r07/improve/show
Improve show command
2 parents de5bc07 + 65ece13 commit 563de5c

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

cmd/meta.go

+19
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type metaCmd struct {
2626
packages []config.Package
2727
appConfig *config.AppConfig
2828
state *state.State
29+
configs map[string]config.Config
2930

3031
updateMessageChan chan *update.ReleaseInfo
3132
}
@@ -52,6 +53,7 @@ func (m *metaCmd) init() error {
5253

5354
var pkgs []config.Package
5455
app := &config.DefaultAppConfig
56+
m.configs = map[string]config.Config{}
5557
for _, file := range files {
5658
cfg, err := config.Read(file)
5759
if err != nil {
@@ -63,6 +65,9 @@ func (m *metaCmd) init() error {
6365
}
6466
pkgs = append(pkgs, parsed...)
6567

68+
// Append config to one struct
69+
m.configs[file] = cfg
70+
6671
if cfg.AppConfig != nil {
6772
app = cfg.AppConfig
6873
}
@@ -270,3 +275,17 @@ func (m metaCmd) GetPackages(resources []state.Resource) []config.Package {
270275
}
271276
return pkgs
272277
}
278+
279+
func (m metaCmd) GetConfig() config.Config {
280+
var all config.Config
281+
for _, config := range m.configs {
282+
if config.AppConfig != nil {
283+
all.AppConfig = config.AppConfig
284+
}
285+
all.GitHub = append(all.GitHub, config.GitHub...)
286+
all.Gist = append(all.Gist, config.Gist...)
287+
all.HTTP = append(all.HTTP, config.HTTP...)
288+
all.Local = append(all.Local, config.Local...)
289+
}
290+
return all
291+
}

cmd/show.go

+42-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@ import (
88

99
"github.com/b4b4r07/afx/pkg/helpers/templates"
1010
"github.com/b4b4r07/afx/pkg/printers"
11+
"github.com/goccy/go-yaml"
1112
"github.com/spf13/cobra"
1213
)
1314

1415
type showCmd struct {
1516
metaCmd
17+
18+
opt showOpt
19+
}
20+
21+
type showOpt struct {
22+
output string
1623
}
1724

1825
var (
@@ -21,13 +28,14 @@ var (
2128

2229
// showExample is examples for show command
2330
showExample = templates.Examples(`
24-
afx show
31+
$ afx show
32+
$ afx show -o json | jq .github
2533
`)
2634
)
2735

2836
// newShowCmd creates a new show command
2937
func (m metaCmd) newShowCmd() *cobra.Command {
30-
c := &showCmd{m}
38+
c := &showCmd{metaCmd: m}
3139

3240
showCmd := &cobra.Command{
3341
Use: "show",
@@ -39,10 +47,41 @@ func (m metaCmd) newShowCmd() *cobra.Command {
3947
SilenceErrors: true,
4048
Args: cobra.MaximumNArgs(0),
4149
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
4373
},
4474
}
4575

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+
4685
return showCmd
4786
}
4887

0 commit comments

Comments
 (0)