viz.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/cli/cli/command"
  20. "github.com/spf13/cobra"
  21. "github.com/docker/compose/v5/pkg/api"
  22. "github.com/docker/compose/v5/pkg/compose"
  23. )
  24. type vizOptions struct {
  25. *ProjectOptions
  26. includeNetworks bool
  27. includePorts bool
  28. includeImageName bool
  29. indentationStr string
  30. }
  31. func vizCommand(p *ProjectOptions, dockerCli command.Cli, backendOptions *BackendOptions) *cobra.Command {
  32. opts := vizOptions{
  33. ProjectOptions: p,
  34. }
  35. var indentationSize int
  36. var useSpaces bool
  37. cmd := &cobra.Command{
  38. Use: "viz [OPTIONS]",
  39. Short: "EXPERIMENTAL - Generate a graphviz graph from your compose file",
  40. PreRunE: Adapt(func(ctx context.Context, args []string) error {
  41. var err error
  42. opts.indentationStr, err = preferredIndentationStr(indentationSize, useSpaces)
  43. return err
  44. }),
  45. RunE: Adapt(func(ctx context.Context, args []string) error {
  46. return runViz(ctx, dockerCli, backendOptions, &opts)
  47. }),
  48. }
  49. cmd.Flags().BoolVar(&opts.includePorts, "ports", false, "Include service's exposed ports in output graph")
  50. cmd.Flags().BoolVar(&opts.includeNetworks, "networks", false, "Include service's attached networks in output graph")
  51. cmd.Flags().BoolVar(&opts.includeImageName, "image", false, "Include service's image name in output graph")
  52. cmd.Flags().IntVar(&indentationSize, "indentation-size", 1, "Number of tabs or spaces to use for indentation")
  53. cmd.Flags().BoolVar(&useSpaces, "spaces", false, "If given, space character ' ' will be used to indent,\notherwise tab character '\\t' will be used")
  54. return cmd
  55. }
  56. func runViz(ctx context.Context, dockerCli command.Cli, backendOptions *BackendOptions, opts *vizOptions) error {
  57. _, _ = fmt.Fprintln(os.Stderr, "viz command is EXPERIMENTAL")
  58. backend, err := compose.NewComposeService(dockerCli, backendOptions.Options...)
  59. if err != nil {
  60. return err
  61. }
  62. project, _, err := opts.ToProject(ctx, dockerCli, backend, nil)
  63. if err != nil {
  64. return err
  65. }
  66. // build graph
  67. graphStr, _ := backend.Viz(ctx, project, api.VizOptions{
  68. IncludeNetworks: opts.includeNetworks,
  69. IncludePorts: opts.includePorts,
  70. IncludeImageName: opts.includeImageName,
  71. Indentation: opts.indentationStr,
  72. })
  73. fmt.Println(graphStr)
  74. return nil
  75. }
  76. // preferredIndentationStr returns a single string given the indentation preference
  77. func preferredIndentationStr(size int, useSpace bool) (string, error) {
  78. if size < 0 {
  79. return "", fmt.Errorf("invalid indentation size: %d", size)
  80. }
  81. indentationStr := "\t"
  82. if useSpace {
  83. indentationStr = " "
  84. }
  85. return strings.Repeat(indentationStr, size), nil
  86. }