viz.go 3.0 KB

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