Skip to content

Commit

Permalink
Add more flexible args in show command
Browse files Browse the repository at this point in the history
  • Loading branch information
babarot committed Feb 24, 2022
1 parent 01fe472 commit 1681fd7
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 21 deletions.
37 changes: 33 additions & 4 deletions cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/b4b4r07/afx/pkg/helpers/templates"
"github.com/b4b4r07/afx/pkg/printers"
"github.com/b4b4r07/afx/pkg/state"
"github.com/goccy/go-yaml"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -45,9 +46,14 @@ func (m metaCmd) newShowCmd() *cobra.Command {
DisableFlagsInUseLine: true,
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.MaximumNArgs(0),
ValidArgs: state.Keys(m.state.NoChanges),
Args: cobra.MinimumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
b, err := yaml.Marshal(m.GetConfig())
cfg := m.GetConfig()
if len(args) > 0 {
cfg = cfg.Contains(args...)
}
b, err := yaml.Marshal(cfg)
if err != nil {
return err
}
Expand All @@ -66,6 +72,10 @@ func (m metaCmd) newShowCmd() *cobra.Command {
for _, pkg := range c.GetPackages(c.state.NoChanges) {
fmt.Println(pkg.GetHome())
}
case "name":
for _, pkg := range c.GetPackages(c.state.NoChanges) {
fmt.Println(pkg.GetName())
}
default:
return fmt.Errorf("%s: not supported output style", c.opt.output)
}
Expand All @@ -74,11 +84,11 @@ func (m metaCmd) newShowCmd() *cobra.Command {
}

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

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

Expand All @@ -94,6 +104,17 @@ func (c *showCmd) run(args []string) error {
Type string
Status string
}
type Items []Item

filter := func(items []Item, input string) []Item {
var tmp []Item
for _, item := range items {
if strings.Contains(item.Name, input) {
tmp = append(tmp, item)
}
}
return tmp
}

var items []Item
for _, pkg := range c.state.Additions {
Expand Down Expand Up @@ -125,6 +146,14 @@ func (c *showCmd) run(args []string) error {
})
}

if len(args) > 0 {
var tmp []Item
for _, arg := range args {
tmp = append(tmp, filter(items, arg)...)
}
items = tmp
}

sort.Slice(items, func(i, j int) bool {
return items[i].Name < items[j].Name
})
Expand Down
12 changes: 0 additions & 12 deletions pkg/config/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,6 @@ type Link struct {
To string `yaml:"to"`
}

func (l *Link) MarshalYAML() ([]byte, error) {
type alias Link

return yaml.Marshal(struct {
*alias
To string `yaml:"to"`
}{
alias: (*alias)(l),
To: os.ExpandEnv(l.To),
})
}

func (l *Link) UnmarshalYAML(b []byte) error {
type alias Link

Expand Down
64 changes: 59 additions & 5 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import (
// Config structure for file describing deployment. This includes the module source, inputs
// dependencies, backend etc. One config element is connected to a single deployment
type Config struct {
GitHub []*GitHub `yaml:"github"`
Gist []*Gist `yaml:"gist"`
Local []*Local `yaml:"local"`
HTTP []*HTTP `yaml:"http"`
GitHub []*GitHub `yaml:"github,omitempty"`
Gist []*Gist `yaml:"gist,omitempty"`
Local []*Local `yaml:"local,omitempty"`
HTTP []*HTTP `yaml:"http,omitempty"`

AppConfig *AppConfig `yaml:"config"`
AppConfig *AppConfig `yaml:"config,omitempty"`
}

// AppConfig represents configurations of this application itself
Expand Down Expand Up @@ -252,3 +252,57 @@ func getResource(pkg Package) state.Resource {
Paths: paths,
}
}

func (c Config) Get(args ...string) Config {
var part Config
for _, arg := range args {
for _, github := range c.GitHub {
if github.Name == arg {
part.GitHub = append(part.GitHub, github)
}
}
for _, gist := range c.Gist {
if gist.Name == arg {
part.Gist = append(part.Gist, gist)
}
}
for _, local := range c.Local {
if local.Name == arg {
part.Local = append(part.Local, local)
}
}
for _, http := range c.HTTP {
if http.Name == arg {
part.HTTP = append(part.HTTP, http)
}
}
}
return part
}

func (c Config) Contains(args ...string) Config {
var part Config
for _, arg := range args {
for _, github := range c.GitHub {
if strings.Contains(github.Name, arg) {
part.GitHub = append(part.GitHub, github)
}
}
for _, gist := range c.Gist {
if strings.Contains(gist.Name, arg) {
part.Gist = append(part.Gist, gist)
}
}
for _, local := range c.Local {
if strings.Contains(local.Name, arg) {
part.Local = append(part.Local, local)
}
}
for _, http := range c.HTTP {
if strings.Contains(http.Name, arg) {
part.HTTP = append(part.HTTP, http)
}
}
}
return part
}

0 comments on commit 1681fd7

Please sign in to comment.