|
| 1 | +/* |
| 2 | +Copyright 2023 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package cmd |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "github.com/spf13/cobra" |
| 23 | + |
| 24 | + "sigs.k8s.io/bom/pkg/spdx" |
| 25 | +) |
| 26 | + |
| 27 | +func AddToDot(parent *cobra.Command) { |
| 28 | + toDotOpts := &spdx.ToDotOptions{} |
| 29 | + toDotCmd := &cobra.Command{ |
| 30 | + PersistentPreRunE: initLogging, |
| 31 | + Short: "bom document todot -> dump the SPDX document as dotlang.", |
| 32 | + Long: `bom document todot -> dump the SPDX document as dotlang. |
| 33 | +
|
| 34 | +This Subcommand translates the graph like structure of an spdx document into dotlang, |
| 35 | +An abstract grammar used to represent graphs https://graphviz.org/doc/info/lang.html. |
| 36 | +
|
| 37 | +This is printed to stdout but can easily be piped to a file like so. |
| 38 | +
|
| 39 | +bom document todot file.spdx > file.dot |
| 40 | +
|
| 41 | +The output can also be filtered by depth, (--depth), inverse dependencies (--find) |
| 42 | +or subgraph (--subgraph) to aid with visualisation by tools like Graphviz |
| 43 | +
|
| 44 | +bom will try to add useful information to dotlangs tooltip node attribute. |
| 45 | +`, |
| 46 | + Use: "todot SPDX_FILE|URL", |
| 47 | + SilenceUsage: true, |
| 48 | + SilenceErrors: true, |
| 49 | + RunE: func(_ *cobra.Command, args []string) error { |
| 50 | + if len(args) == 0 { |
| 51 | + args = append(args, "") |
| 52 | + } |
| 53 | + doc, err := spdx.OpenDoc(args[0]) |
| 54 | + if err != nil { |
| 55 | + return fmt.Errorf("opening doc: %w", err) |
| 56 | + } |
| 57 | + if toDotOpts.Find != "" { |
| 58 | + doc.FilterReverseDependencies(toDotOpts.Find, toDotOpts.Depth) |
| 59 | + } |
| 60 | + fmt.Println(doc.ToDot(toDotOpts)) |
| 61 | + return nil |
| 62 | + }, |
| 63 | + } |
| 64 | + toDotCmd.PersistentFlags().StringVarP( |
| 65 | + &toDotOpts.Find, |
| 66 | + "find", |
| 67 | + "f", |
| 68 | + "", |
| 69 | + "Find node in DAG", |
| 70 | + ) |
| 71 | + toDotCmd.PersistentFlags().IntVarP( |
| 72 | + &toDotOpts.Depth, |
| 73 | + "depth", |
| 74 | + "d", |
| 75 | + -1, |
| 76 | + "Depth to traverse", |
| 77 | + ) |
| 78 | + toDotCmd.PersistentFlags().StringVarP( |
| 79 | + &toDotOpts.SubGraphRoot, |
| 80 | + "subgraph", |
| 81 | + "s", |
| 82 | + "", |
| 83 | + "SPDXID of the root node for the subgraph", |
| 84 | + ) |
| 85 | + parent.AddCommand(toDotCmd) |
| 86 | +} |
0 commit comments