github.com/CycloneDX/sbom-utility@v0.16.0/utils/flags.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  /*
     3   * Licensed to the Apache Software Foundation (ASF) under one or more
     4   * contributor license agreements.  See the NOTICE file distributed with
     5   * this work for additional information regarding copyright ownership.
     6   * The ASF licenses this file to You under the Apache License, Version 2.0
     7   * (the "License"); you may not use this file except in compliance with
     8   * the License.  You may obtain a copy of the License at
     9   *
    10   *     http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing, software
    13   * distributed under the License is distributed on an "AS IS" BASIS,
    14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15   * See the License for the specific language governing permissions and
    16   * limitations under the License.
    17   */
    18  
    19  package utils
    20  
    21  // Globals
    22  var GlobalFlags CommandFlags
    23  
    24  type CommandFlags struct {
    25  	// Not flags, but "main" package var copies
    26  	Project    string
    27  	Binary     string
    28  	Version    string
    29  	WorkingDir string
    30  	ExecDir    string
    31  
    32  	// Configurations
    33  	ConfigSchemaFile           string
    34  	ConfigCustomValidationFile string
    35  	ConfigLicensePolicyFile    string
    36  
    37  	// persistent flags (common to all commands)
    38  	PersistentFlags PersistentCommandFlags
    39  
    40  	// Command-specific flags
    41  	ComponentFlags          ComponentCommandFlags
    42  	CustomValidationOptions CustomValidationFlags
    43  	DiffFlags               DiffCommandFlags
    44  	LicenseFlags            LicenseCommandFlags
    45  	PatchFlags              PatchCommandFlags
    46  	ResourceFlags           ResourceCommandFlags
    47  	SchemaFlags             SchemaCommandFlags
    48  	StatsFlags              StatsCommandFlags
    49  	TrimFlags               TrimCommandFlags
    50  	ValidateFlags           ValidateCommandFlags
    51  	VulnerabilityFlags      VulnerabilityCommandFlags
    52  
    53  	// Misc flags
    54  	LogOutputIndentCallstack bool // Log indent
    55  }
    56  
    57  // NOTE: These flags are shared by both the list and policy subcommands
    58  type PersistentCommandFlags struct {
    59  	Quiet           bool // suppresses all non-essential (informational) output from a command. Overrides any other log-level commands.
    60  	Trace           bool // trace logging
    61  	Debug           bool // debug logging
    62  	InputFile       string
    63  	OutputFile      string // TODO: TODO: Note: not used by `validate` command, which emits a warning if supplied
    64  	OutputFormat    string // e.g., "txt", "csv"", "md" (markdown) (normalized to lowercase)
    65  	OutputIndent    uint8
    66  	OutputNormalize bool
    67  }
    68  
    69  func (persistentFlags PersistentCommandFlags) GetOutputIndentInt() int {
    70  	return int(persistentFlags.OutputIndent)
    71  }
    72  
    73  // Common flags for all "list" (report) commands (e.g., license list, vulnerability list, etc.)
    74  type ReportCommandFlags struct {
    75  	Summary      bool
    76  	ListLineWrap bool // NOTE: SHOULD ONLY be used for "text" output; currently only used by "license policy" command.
    77  }
    78  
    79  type LicenseCommandFlags struct {
    80  	ReportCommandFlags
    81  }
    82  
    83  type ValidateCommandFlags struct {
    84  	SchemaVariant        string
    85  	ForcedJsonSchemaFile string
    86  	CustomValidation     bool // Uses custom validation flags if "true"; defaults to config. "custom.json"
    87  	// validation error result (output) processing
    88  	MaxNumErrors              int
    89  	MaxErrorDescriptionLength int
    90  	ColorizeErrorOutput       bool
    91  	ShowErrorValue            bool
    92  }
    93  
    94  type VulnerabilityCommandFlags struct {
    95  	ReportCommandFlags
    96  }
    97  
    98  type DiffCommandFlags struct {
    99  	Colorize    bool
   100  	RevisedFile string
   101  }
   102  
   103  type ResourceCommandFlags struct {
   104  	ReportCommandFlags // NOTE: currently unused; as # columns grows, please implement
   105  	ResourceType       string
   106  }
   107  
   108  type ComponentCommandFlags struct {
   109  	ReportCommandFlags
   110  	Types []string
   111  }
   112  
   113  func NewResourceCommandFlags(resourceType string) ResourceCommandFlags {
   114  	return ResourceCommandFlags{
   115  		ResourceType: resourceType,
   116  	}
   117  }
   118  
   119  type SchemaCommandFlags struct {
   120  	ReportCommandFlags // NOTE: currently unused; as # columns grows, please implement
   121  }
   122  
   123  // TODO: unused/unimplemented
   124  type StatsCommandFlags struct {
   125  }
   126  
   127  // TODO: write a "parse" method for the struct (i.e., from "raw" to slice)
   128  type TrimCommandFlags struct {
   129  	RawKeys   string
   130  	RawPaths  string
   131  	Keys      []string
   132  	FromPaths []string
   133  }
   134  
   135  type PatchCommandFlags struct {
   136  	PatchFile    string
   137  	OutputFormat string // NOTE: needed to overcome Cobra limitation when diff. commands have diff. default values
   138  }
   139  
   140  type CustomValidationFlags struct {
   141  	Composition bool
   142  	License     bool
   143  	Properties  bool
   144  }
   145  
   146  // format and output the MyFlags struct as a string using Go's Stringer interface
   147  func (flags *CommandFlags) String() string {
   148  	buffer, _ := EncodeAnyToDefaultIndentedJSONStr(flags)
   149  	return buffer.String()
   150  }