Skip to content

Commit

Permalink
Merge pull request #119 from Threagile/added-commandline-options
Browse files Browse the repository at this point in the history
added command line options for remaining config options
  • Loading branch information
joreiche authored Nov 20, 2024
2 parents bab72ae + 0cf0b82 commit 10f262a
Show file tree
Hide file tree
Showing 12 changed files with 508 additions and 191 deletions.
8 changes: 4 additions & 4 deletions internal/threagile/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ func (what *Threagile) initAnalyze() *Threagile {
Short: "Analyze model",
Aliases: []string{"analyze", "analyse", "run", "analyse-model"},
RunE: func(cmd *cobra.Command, args []string) error {
cfg := what.readConfig(cmd, what.buildTimestamp)
what.processArgs(cmd, args)
commands := what.readCommands()
progressReporter := DefaultProgressReporter{Verbose: cfg.GetVerbose()}
progressReporter := DefaultProgressReporter{Verbose: what.config.GetVerbose()}

r, err := model.ReadAndAnalyzeModel(cfg, risks.GetBuiltInRiskRules(), progressReporter)
r, err := model.ReadAndAnalyzeModel(what.config, risks.GetBuiltInRiskRules(), progressReporter)
if err != nil {
return fmt.Errorf("failed to read and analyze model: %w", err)
}

err = report.Generate(cfg, r, commands, risks.GetBuiltInRiskRules(), progressReporter)
err = report.Generate(what.config, r, commands, risks.GetBuiltInRiskRules(), progressReporter)
if err != nil {
return fmt.Errorf("failed to generate reports: %w", err)
}
Expand Down
80 changes: 67 additions & 13 deletions internal/threagile/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ type Config struct {
RiskExcelValue RiskExcelConfig `json:"RiskExcel" yaml:"RiskExcel"`

ServerModeValue bool `json:"ServerMode,omitempty" yaml:"ServerMode"`
DiagramDPIValue int `json:"DiagramDPI,omitempty" yaml:"DiagramDPI"`
ServerPortValue int `json:"ServerPort,omitempty" yaml:"ServerPort"`
DiagramDPIValue int `json:"DiagramDPI,omitempty" yaml:"DiagramDPI"`
GraphvizDPIValue int `json:"GraphvizDPI,omitempty" yaml:"GraphvizDPI"`
MaxGraphvizDPIValue int `json:"MaxGraphvizDPI,omitempty" yaml:"MaxGraphvizDPI"`
BackupHistoryFilesToKeepValue int `json:"BackupHistoryFilesToKeep,omitempty" yaml:"BackupHistoryFilesToKeep"`
Expand All @@ -63,6 +63,16 @@ type Config struct {
KeepDiagramSourceFilesValue bool `json:"KeepDiagramSourceFiles,omitempty" yaml:"KeepDiagramSourceFiles"`
IgnoreOrphanedRiskTrackingValue bool `json:"IgnoreOrphanedRiskTracking,omitempty" yaml:"IgnoreOrphanedRiskTracking"`

SkipDataFlowDiagramValue bool `json:"SkipDataFlowDiagram,omitempty" yaml:"SkipDataFlowDiagram"`
SkipDataAssetDiagramValue bool `json:"SkipDataAssetDiagram,omitempty" yaml:"SkipDataAssetDiagram"`
SkipRisksJSONValue bool `json:"SkipRisksJSON,omitempty" yaml:"SkipRisksJSON"`
SkipTechnicalAssetsJSONValue bool `json:"SkipTechnicalAssetsJSON,omitempty" yaml:"SkipTechnicalAssetsJSON"`
SkipStatsJSONValue bool `json:"SkipStatsJSON,omitempty" yaml:"SkipStatsJSON"`
SkipRisksExcelValue bool `json:"SkipRisksExcel,omitempty" yaml:"SkipRisksExcel"`
SkipTagsExcelValue bool `json:"SkipTagsExcel,omitempty" yaml:"SkipTagsExcel"`
SkipReportPDFValue bool `json:"SkipReportPDF,omitempty" yaml:"SkipReportPDF"`
SkipReportADOCValue bool `json:"SkipReportADOC,omitempty" yaml:"SkipReportADOC"`

AttractivenessValue Attractiveness `json:"Attractiveness" yaml:"Attractiveness"`

ReportConfigurationValue report.ReportConfiguation `json:"ReportConfiguration" yaml:"ReportConfiguration"`
Expand Down Expand Up @@ -103,22 +113,30 @@ type ConfigGetter interface {
GetRiskExcelShrinkColumnsToFit() bool
GetRiskExcelColorText() bool
GetServerMode() bool
GetDiagramDPI() int
GetServerPort() int
GetDiagramDPI() int
GetGraphvizDPI() int
GetMinGraphvizDPI() int
GetMaxGraphvizDPI() int
GetBackupHistoryFilesToKeep() int
GetAddModelTitle() bool
GetKeepDiagramSourceFiles() bool
GetIgnoreOrphanedRiskTracking() bool
GetSkipDataFlowDiagram() bool
GetSkipDataAssetDiagram() bool
GetSkipRisksJSON() bool
GetSkipTechnicalAssetsJSON() bool
GetSkipStatsJSON() bool
GetSkipRisksExcel() bool
GetSkipTagsExcel() bool
GetSkipReportPDF() bool
GetSkipReportADOC() bool
GetAttractiveness() Attractiveness
GetReportConfiguration() report.ReportConfiguation
GetThreagileVersion() string
GetProgressReporter() types.ProgressReporter
GetReportConfigurationHideChapters() map[report.ChaptersToShowHide]bool
}

type ConfigSetter interface {
SetVerbose(verbose bool)
SetInteractive(interactive bool)
Expand All @@ -132,8 +150,8 @@ type ConfigSetter interface {
SetRiskRulePlugins(riskRulePlugins []string)
SetSkipRiskRules(skipRiskRules []string)
SetServerMode(serverMode bool)
SetDiagramDPI(diagramDPI int)
SetServerPort(serverPort int)
SetDiagramDPI(diagramDPI int)
SetIgnoreOrphanedRiskTracking(ignoreOrphanedRiskTracking bool)
}

Expand Down Expand Up @@ -436,7 +454,7 @@ func (c *Config) Merge(config Config, values map[string]any) {
}

case strings.ToLower("ServerMode"):
// not configurable via config file
c.ServerModeValue = config.ServerModeValue

case strings.ToLower("DiagramDPI"):
c.DiagramDPIValue = config.DiagramDPIValue
Expand Down Expand Up @@ -718,14 +736,6 @@ func (c *Config) SetServerMode(serverMode bool) {
c.ServerModeValue = serverMode
}

func (c *Config) GetDiagramDPI() int {
return c.DiagramDPIValue
}

func (c *Config) SetDiagramDPI(diagramDPI int) {
c.DiagramDPIValue = diagramDPI
}

func (c *Config) GetServerPort() int {
return c.ServerPortValue
}
Expand All @@ -734,6 +744,14 @@ func (c *Config) SetServerPort(serverPort int) {
c.ServerPortValue = serverPort
}

func (c *Config) GetDiagramDPI() int {
return c.DiagramDPIValue
}

func (c *Config) SetDiagramDPI(diagramDPI int) {
c.DiagramDPIValue = diagramDPI
}

func (c *Config) GetGraphvizDPI() int {
return c.GraphvizDPIValue
}
Expand Down Expand Up @@ -766,6 +784,42 @@ func (c *Config) SetIgnoreOrphanedRiskTracking(ignoreOrphanedRiskTracking bool)
c.IgnoreOrphanedRiskTrackingValue = ignoreOrphanedRiskTracking
}

func (c *Config) GetSkipDataFlowDiagram() bool {
return c.SkipDataFlowDiagramValue
}

func (c *Config) GetSkipDataAssetDiagram() bool {
return c.SkipDataAssetDiagramValue
}

func (c *Config) GetSkipRisksJSON() bool {
return c.SkipRisksJSONValue
}

func (c *Config) GetSkipTechnicalAssetsJSON() bool {
return c.SkipTechnicalAssetsJSONValue
}

func (c *Config) GetSkipStatsJSON() bool {
return c.SkipStatsJSONValue
}

func (c *Config) GetSkipRisksExcel() bool {
return c.SkipRisksExcelValue
}

func (c *Config) GetSkipTagsExcel() bool {
return c.SkipTagsExcelValue
}

func (c *Config) GetSkipReportPDF() bool {
return c.SkipReportPDFValue
}

func (c *Config) GetSkipReportADOC() bool {
return c.SkipReportADOCValue
}

func (c *Config) GetAttractiveness() Attractiveness {
return c.AttractivenessValue
}
Expand Down
16 changes: 11 additions & 5 deletions internal/threagile/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ func (what *Threagile) initCreate() *Threagile {
Short: "Create example threagile model",
Long: "\n" + Logo + "\n\n" + fmt.Sprintf(VersionText, what.buildTimestamp) + "\n\njust create an example model named threagile-example-model.yaml in the output directory",
RunE: func(cmd *cobra.Command, args []string) error {
what.processArgs(cmd, args)

appDir, err := cmd.Flags().GetString(appDirFlagName)
if err != nil {
cmd.Printf("Unable to read app-dir flag: %v", err)
Expand Down Expand Up @@ -48,23 +50,25 @@ func (what *Threagile) initCreate() *Threagile {
Short: "Create stub threagile model",
Long: "\n" + Logo + "\n\n" + fmt.Sprintf(VersionText, what.buildTimestamp) + "\n\njust create a minimal stub model named threagile-stub-model.yaml in the output directory",
RunE: func(cmd *cobra.Command, args []string) error {
cfg := what.readConfig(cmd, what.buildTimestamp)
what.processArgs(cmd, args)

err := examples.CreateStubModelFile(cfg.GetAppFolder(), cfg.GetOutputFolder(), InputFile)
err := examples.CreateStubModelFile(what.config.GetAppFolder(), what.config.GetOutputFolder(), InputFile)
if err != nil {
cmd.Printf("Unable to copy stub model: %v", err)
return err
}

if !what.flags.interactiveFlag {
if !what.config.GetInteractive() {
cmd.Println(Logo + "\n\n" + fmt.Sprintf(VersionText, what.buildTimestamp))
}
cmd.Printf("A minimal stub model was created named threagile-stub-model.yaml in %q.\n", cfg.GetOutputFolder())
if !what.flags.interactiveFlag {

cmd.Printf("A minimal stub model was created named threagile-stub-model.yaml in %q.\n", what.config.GetOutputFolder())
if !what.config.GetInteractive() {
cmd.Println()
cmd.Println(Examples)
cmd.Println()
}

return nil
},
})
Expand All @@ -74,6 +78,8 @@ func (what *Threagile) initCreate() *Threagile {
Short: "Create editing support",
Long: "\n" + Logo + "\n\n" + fmt.Sprintf(VersionText, what.buildTimestamp) + "\n\njust create some editing support stuff in the output directory",
RunE: func(cmd *cobra.Command, args []string) error {
what.processArgs(cmd, args)

appDir, err := cmd.Flags().GetString(appDirFlagName)
if err != nil {
cmd.Printf("Unable to read app-dir flag: %v", err)
Expand Down
10 changes: 6 additions & 4 deletions internal/threagile/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,21 @@ func (what *Threagile) initExecute() *Threagile {
Short: "Execute model macro",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cfg := what.readConfig(cmd, what.buildTimestamp)
progressReporter := DefaultProgressReporter{Verbose: cfg.GetVerbose()}
what.processArgs(cmd, args)

r, err := model.ReadAndAnalyzeModel(cfg, risks.GetBuiltInRiskRules(), progressReporter)
progressReporter := DefaultProgressReporter{Verbose: what.config.GetVerbose()}

r, err := model.ReadAndAnalyzeModel(what.config, risks.GetBuiltInRiskRules(), progressReporter)
if err != nil {
return fmt.Errorf("unable to read and analyze model: %w", err)
}

macrosId := args[0]
err = macros.ExecuteModelMacro(r.ModelInput, cfg.GetInputFile(), r.ParsedModel, macrosId)
err = macros.ExecuteModelMacro(r.ModelInput, what.config.GetInputFile(), r.ParsedModel, macrosId)
if err != nil {
return fmt.Errorf("unable to execute model macro: %w", err)
}

return nil
},
})
Expand Down
29 changes: 14 additions & 15 deletions internal/threagile/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package threagile

import (
"fmt"
"strings"

"github.com/spf13/cobra"
"github.com/threagile/threagile/pkg/macros"
"github.com/threagile/threagile/pkg/model"
Expand Down Expand Up @@ -50,13 +48,14 @@ func (what *Threagile) initExplainNew() *Threagile {
return what
}

func (what *Threagile) explainRisk(cmd *cobra.Command, _ []string) error {
cfg := what.readConfig(cmd, what.buildTimestamp)
progressReporter := DefaultProgressReporter{Verbose: cfg.GetVerbose()}
func (what *Threagile) explainRisk(cmd *cobra.Command, args []string) error {
what.processArgs(cmd, args)

progressReporter := DefaultProgressReporter{Verbose: what.config.GetVerbose()}

// todo: reuse model if already loaded

result, runError := model.ReadAndAnalyzeModel(cfg, risks.GetBuiltInRiskRules(), progressReporter)
result, runError := model.ReadAndAnalyzeModel(what.config, risks.GetBuiltInRiskRules(), progressReporter)
if runError != nil {
cmd.Printf("Failed to read and analyze model: %v", runError)
return runError
Expand All @@ -68,20 +67,16 @@ func (what *Threagile) explainRisk(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("not implemented yet")
}

func (what *Threagile) explainRules(cmd *cobra.Command, _ []string) error {
func (what *Threagile) explainRules(cmd *cobra.Command, args []string) error {
what.processArgs(cmd, args)

cmd.Println(Logo + "\n\n" + fmt.Sprintf(VersionText, what.buildTimestamp))
cmd.Println("Explanation for risk rules:")
cmd.Println()
cmd.Println("----------------------")
cmd.Println("Custom risk rules:")
cmd.Println("----------------------")
cfg := new(Config).Defaults(what.buildTimestamp)
configError := cfg.Load(what.flags.configFlag)
if configError != nil {
cmd.Printf("WARNING: failed to load config file %q: %v\n", what.flags.configFlag, configError)
}

customRiskRules := model.LoadCustomRiskRules(cfg.GetPluginFolder(), strings.Split(what.flags.customRiskRulesPluginFlag, ","), DefaultProgressReporter{Verbose: what.flags.verboseFlag})
customRiskRules := model.LoadCustomRiskRules(what.config.GetPluginFolder(), what.config.GetRiskRulePlugins(), DefaultProgressReporter{Verbose: what.config.GetVerbose()})
for _, rule := range customRiskRules {
cmd.Printf("%v: %v\n", rule.Category().ID, rule.Category().Description)
}
Expand All @@ -99,6 +94,8 @@ func (what *Threagile) explainRules(cmd *cobra.Command, _ []string) error {
}

func (what *Threagile) explainMacros(cmd *cobra.Command, args []string) {
what.processArgs(cmd, args)

cmd.Println(Logo + "\n\n" + fmt.Sprintf(VersionText, what.buildTimestamp))
cmd.Println("Explanation for the model macros:")
cmd.Println()
Expand All @@ -122,12 +119,14 @@ func (what *Threagile) explainMacros(cmd *cobra.Command, args []string) {
}

func (what *Threagile) explainTypes(cmd *cobra.Command, args []string) {
what.processArgs(cmd, args)

cmd.Println(Logo + "\n\n" + fmt.Sprintf(VersionText, what.buildTimestamp))
fmt.Println("Explanation for the types:")
cmd.Println()
cmd.Println("The following types are available (can be extended for custom rules):")
cmd.Println()
for name, values := range types.GetBuiltinTypeValues(what.readConfig(cmd, what.buildTimestamp)) {
for name, values := range types.GetBuiltinTypeValues(what.config) {
cmd.Println(name)
for _, candidate := range values {
cmd.Printf("\t %v: %v\n", candidate, candidate.Explain())
Expand Down
Loading

0 comments on commit 10f262a

Please sign in to comment.