Skip to content

Commit

Permalink
Merge pull request #27 from b4b4r07/improve/show
Browse files Browse the repository at this point in the history
Improve show command
  • Loading branch information
b4b4r07 authored Feb 23, 2022
2 parents de5bc07 + 65ece13 commit 563de5c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
19 changes: 19 additions & 0 deletions cmd/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type metaCmd struct {
packages []config.Package
appConfig *config.AppConfig
state *state.State
configs map[string]config.Config

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

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

// Append config to one struct
m.configs[file] = cfg

if cfg.AppConfig != nil {
app = cfg.AppConfig
}
Expand Down Expand Up @@ -270,3 +275,17 @@ func (m metaCmd) GetPackages(resources []state.Resource) []config.Package {
}
return pkgs
}

func (m metaCmd) GetConfig() config.Config {
var all config.Config
for _, config := range m.configs {
if config.AppConfig != nil {
all.AppConfig = config.AppConfig
}
all.GitHub = append(all.GitHub, config.GitHub...)
all.Gist = append(all.Gist, config.Gist...)
all.HTTP = append(all.HTTP, config.HTTP...)
all.Local = append(all.Local, config.Local...)
}
return all
}
45 changes: 42 additions & 3 deletions cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ import (

"github.com/b4b4r07/afx/pkg/helpers/templates"
"github.com/b4b4r07/afx/pkg/printers"
"github.com/goccy/go-yaml"
"github.com/spf13/cobra"
)

type showCmd struct {
metaCmd

opt showOpt
}

type showOpt struct {
output string
}

var (
Expand All @@ -21,13 +28,14 @@ var (

// showExample is examples for show command
showExample = templates.Examples(`
afx show
$ afx show
$ afx show -o json | jq .github
`)
)

// newShowCmd creates a new show command
func (m metaCmd) newShowCmd() *cobra.Command {
c := &showCmd{m}
c := &showCmd{metaCmd: m}

showCmd := &cobra.Command{
Use: "show",
Expand All @@ -39,10 +47,41 @@ func (m metaCmd) newShowCmd() *cobra.Command {
SilenceErrors: true,
Args: cobra.MaximumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
return c.run(args)
b, err := yaml.Marshal(m.GetConfig())
if err != nil {
return err
}
switch c.opt.output {
case "default":
return c.run(args)
case "json":
yb, err := yaml.YAMLToJSON(b)
if err != nil {
return err
}
fmt.Println(string(yb))
case "yaml":
fmt.Println(string(b))
case "path":
for _, pkg := range c.GetPackages(c.state.NoChanges) {
fmt.Println(pkg.GetHome())
}
default:
return fmt.Errorf("%s: not supported output style", c.opt.output)
}
return nil
},
}

flag := showCmd.Flags()
flag.StringVarP(&c.opt.output, "output", "o", "default", "Output style [default,json,yaml,path]")

showCmd.RegisterFlagCompletionFunc("output",
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
out := []string{"default", "json", "yaml", "path"}
return out, cobra.ShellCompDirectiveNoFileComp
})

return showCmd
}

Expand Down

0 comments on commit 563de5c

Please sign in to comment.