opts.go 816 B

1234567891011121314151617181920212223242526272829
  1. package compose
  2. import (
  3. "github.com/spf13/cobra"
  4. "github.com/spf13/pflag"
  5. )
  6. type ProjectOptions struct {
  7. ConfigPaths []string
  8. Name string
  9. }
  10. func (o *ProjectOptions) AddFlags(flags *pflag.FlagSet) {
  11. flags.StringArrayVarP(&o.ConfigPaths, "file", "f", nil, "Specify an alternate compose file")
  12. flags.StringVarP(&o.Name, "project-name", "n", "", "Specify an alternate project name (default: directory name)")
  13. }
  14. type ProjectFunc func(project *Project, args []string) error
  15. // WithProject wrap a ProjectFunc into a cobra command
  16. func WithProject(options *ProjectOptions, f ProjectFunc) func(cmd *cobra.Command, args []string) error {
  17. return func(cmd *cobra.Command, args []string) error {
  18. project, err := ProjectFromOptions(options)
  19. if err != nil {
  20. return err
  21. }
  22. return f(project, args)
  23. }
  24. }