viz.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. Copyright 2023 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "fmt"
  17. "os"
  18. "strings"
  19. "github.com/docker/compose/v2/pkg/api"
  20. "github.com/spf13/cobra"
  21. )
  22. type vizOptions struct {
  23. *ProjectOptions
  24. includeNetworks bool
  25. includePorts bool
  26. includeImageName bool
  27. indentationStr string
  28. }
  29. func vizCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
  30. opts := vizOptions{
  31. ProjectOptions: p,
  32. }
  33. var indentationSize int
  34. var useSpaces bool
  35. cmd := &cobra.Command{
  36. Use: "viz [OPTIONS]",
  37. Short: "EXPERIMENTAL - Generate a graphviz graph from your compose file",
  38. PreRunE: Adapt(func(ctx context.Context, args []string) error {
  39. var err error
  40. opts.indentationStr, err = preferredIndentationStr(indentationSize, useSpaces)
  41. return err
  42. }),
  43. RunE: Adapt(func(ctx context.Context, args []string) error {
  44. return runViz(ctx, backend, &opts)
  45. }),
  46. }
  47. cmd.Flags().BoolVar(&opts.includePorts, "ports", false, "Include service's exposed ports in output graph")
  48. cmd.Flags().BoolVar(&opts.includeNetworks, "networks", false, "Include service's attached networks in output graph")
  49. cmd.Flags().BoolVar(&opts.includeImageName, "image", false, "Include service's image name in output graph")
  50. cmd.Flags().IntVar(&indentationSize, "indentation-size", 1, "Number of tabs or spaces to use for indentation")
  51. cmd.Flags().BoolVar(&useSpaces, "spaces", false, "If given, space character ' ' will be used to indent,\notherwise tab character '\\t' will be used")
  52. return cmd
  53. }
  54. func runViz(ctx context.Context, backend api.Service, opts *vizOptions) error {
  55. _, _ = fmt.Fprintln(os.Stderr, "viz command is EXPERIMENTAL")
  56. project, err := opts.ToProject(nil)
  57. if err != nil {
  58. return err
  59. }
  60. // build graph
  61. graphStr, _ := backend.Viz(ctx, project, api.VizOptions{
  62. IncludeNetworks: opts.includeNetworks,
  63. IncludePorts: opts.includePorts,
  64. IncludeImageName: opts.includeImageName,
  65. Indentation: opts.indentationStr,
  66. })
  67. fmt.Println(graphStr)
  68. return nil
  69. }
  70. // preferredIndentationStr returns a single string given the indentation preference
  71. func preferredIndentationStr(size int, useSpace bool) (string, error) {
  72. if size < 0 {
  73. return "", fmt.Errorf("invalid indentation size: %d", size)
  74. }
  75. indentationStr := "\t"
  76. if useSpace {
  77. indentationStr = " "
  78. }
  79. return strings.Repeat(indentationStr, size), nil
  80. }