|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/gopher-fleece/gleece/v2/core/pipeline" |
| 9 | + "github.com/gopher-fleece/gleece/v2/definitions" |
| 10 | + "github.com/gopher-fleece/gleece/v2/infrastructure/logger" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +type DumpFormat string |
| 15 | + |
| 16 | +const ( |
| 17 | + DumpFormatDot DumpFormat = "dot" |
| 18 | + DumpFormatPlain DumpFormat = "plain" |
| 19 | +) |
| 20 | + |
| 21 | +var ( |
| 22 | + gleeceConfigPath string |
| 23 | + dumpOutput string |
| 24 | + dumpFormat string |
| 25 | +) |
| 26 | + |
| 27 | +// dumpCmd is a command used to dump information such as controllers, models, graph representations etc. |
| 28 | +var dumpCmd = &cobra.Command{ |
| 29 | + Use: "dump", |
| 30 | + Short: "Dumps information about a Gleece project", |
| 31 | + Long: `The dump command returns information on the given Gleece project. |
| 32 | +This information is obtained by running the project through the analysis facilities`, |
| 33 | +} |
| 34 | + |
| 35 | +var graphCmd = &cobra.Command{ |
| 36 | + Use: "graph", |
| 37 | + Short: "Dumps the primary symbolic graph to a textual representation", |
| 38 | + Long: `The 'graph' command can be used to dump a full representation of Gleece primary symbolic graph. |
| 39 | +This graph depict all entities and their relations and is most useful as means to visualize entity relations and dependencies`, |
| 40 | + PreRunE: func(cmd *cobra.Command, args []string) error { |
| 41 | + switch dumpFormat { |
| 42 | + case string(DumpFormatDot), string(DumpFormatPlain), "": |
| 43 | + default: |
| 44 | + return fmt.Errorf("invalid --format: '%s' (want dot|plain)", dumpFormat) |
| 45 | + } |
| 46 | + return nil |
| 47 | + }, |
| 48 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 49 | + err := dumpGraph(cmd) |
| 50 | + if err != nil { |
| 51 | + logger.Fatal("Failed to dump graph - %v", err) |
| 52 | + } |
| 53 | + return err |
| 54 | + }, |
| 55 | +} |
| 56 | + |
| 57 | +func initDumpCommandHierarchy() { |
| 58 | + graphCmd.Flags().StringVarP( |
| 59 | + &dumpFormat, |
| 60 | + "format", |
| 61 | + "f", |
| 62 | + "dot", |
| 63 | + "-f=dot", |
| 64 | + ) |
| 65 | + |
| 66 | + dumpCmd.PersistentFlags().StringVarP( |
| 67 | + &dumpOutput, |
| 68 | + "output", |
| 69 | + "o", |
| 70 | + "", |
| 71 | + "-o \"/target-directory/dump.dot\"", |
| 72 | + ) |
| 73 | + |
| 74 | + dumpCmd.PersistentFlags().StringVarP( |
| 75 | + &gleeceConfigPath, |
| 76 | + "config", |
| 77 | + "c", |
| 78 | + "./gleece.config.json", |
| 79 | + "-c \"/project-directory/gleece.config.json\"", |
| 80 | + ) |
| 81 | + |
| 82 | + dumpCmd.AddCommand(graphCmd) |
| 83 | +} |
| 84 | + |
| 85 | +func resetDumpCommand() { |
| 86 | + gleeceConfigPath = "" |
| 87 | + dumpOutput = "" |
| 88 | + dumpFormat = "" |
| 89 | +} |
| 90 | + |
| 91 | +func loadGleeceConfig(cmd *cobra.Command) (*definitions.GleeceConfig, error) { |
| 92 | + gleeceConfigPath, err := cmd.Flags().GetString("config") |
| 93 | + if err != nil { |
| 94 | + logger.Error("Failed to read Gleece config location from CLI - %v", err) |
| 95 | + return nil, fmt.Errorf("failed to read Gleece config location from CLI - %v", err) |
| 96 | + } |
| 97 | + |
| 98 | + trimmedPath := strings.Trim(gleeceConfigPath, "\"") |
| 99 | + if trimmedPath == "" { |
| 100 | + trimmedPath = "./gleece.config.json" |
| 101 | + logger.Info("Gleece config path was no specified. Defaulting to '%s'", trimmedPath) |
| 102 | + } |
| 103 | + |
| 104 | + gleeceConfig, err := LoadGleeceConfig(trimmedPath) |
| 105 | + if err != nil { |
| 106 | + logger.Error("Failed to load Gleece config from '%s' - %v", trimmedPath, err) |
| 107 | + return nil, fmt.Errorf("failed to load Gleece config from '%s' - %v", trimmedPath, err) |
| 108 | + } |
| 109 | + |
| 110 | + return gleeceConfig, nil |
| 111 | + |
| 112 | +} |
| 113 | + |
| 114 | +func getPipeline(cmd *cobra.Command) (*pipeline.GleecePipeline, error) { |
| 115 | + gleeceConfig, err := loadGleeceConfig(cmd) |
| 116 | + if err != nil { |
| 117 | + return nil, err |
| 118 | + } |
| 119 | + |
| 120 | + pipe, err := pipeline.NewGleecePipeline(gleeceConfig) |
| 121 | + if err != nil { |
| 122 | + logger.Error("Failed to construct the analysis pipeline - %v", err) |
| 123 | + return nil, fmt.Errorf("failed to construct the analysis pipeline - %v", err) |
| 124 | + } |
| 125 | + |
| 126 | + return &pipe, nil |
| 127 | +} |
| 128 | + |
| 129 | +func dumpGraph(cmd *cobra.Command) error { |
| 130 | + logger.Info("Dumping graph to '%s' format", dumpFormat) |
| 131 | + pipe, err := getPipeline(cmd) |
| 132 | + if err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + |
| 136 | + err = pipe.GenerateGraph() |
| 137 | + if err != nil { |
| 138 | + logger.Error("Failed to construct a symbol graph for the project - %v", err) |
| 139 | + return fmt.Errorf("failed to construct a symbol graph for the project - %v", err) |
| 140 | + } |
| 141 | + |
| 142 | + var text string |
| 143 | + switch dumpFormat { |
| 144 | + case string(DumpFormatDot): |
| 145 | + text = pipe.Graph().ToDot(nil) |
| 146 | + case string(DumpFormatPlain): |
| 147 | + text = pipe.Graph().String() |
| 148 | + default: |
| 149 | + logger.Debug("Dump format not specified - defaulting to DOT") |
| 150 | + text = pipe.Graph().ToDot(nil) |
| 151 | + } |
| 152 | + |
| 153 | + if dumpOutput == "" { |
| 154 | + logger.Debug("Output not specified - using stdout") |
| 155 | + cmd.Println(string(text)) |
| 156 | + return nil |
| 157 | + } |
| 158 | + |
| 159 | + logger.Debug("Writing %d bytes to '%s'", len(text), dumpOutput) |
| 160 | + if err = os.WriteFile(dumpOutput, []byte(text), 0o644); err != nil { |
| 161 | + logger.Error("Failed to write to '%s' - %v", dumpOutput, err) |
| 162 | + return fmt.Errorf("failed to write to '%s' - %v", dumpOutput, err) |
| 163 | + } |
| 164 | + |
| 165 | + logger.Info("Graph successfully written to '%s'", dumpOutput) |
| 166 | + return nil |
| 167 | +} |
0 commit comments