github.com/CycloneDX/sbom-utility@v0.16.0/main.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package main 19 20 import ( 21 "fmt" 22 "os" 23 "path/filepath" 24 "runtime" 25 26 "github.com/CycloneDX/sbom-utility/cmd" 27 "github.com/CycloneDX/sbom-utility/log" 28 "github.com/CycloneDX/sbom-utility/schema" 29 "github.com/CycloneDX/sbom-utility/utils" 30 ) 31 32 // Struct used to hold tagged (release) build information 33 // Which is displayed by the `version` command. 34 // These values can be overwritten by `go build ${LDFLAGS}` 35 // for example, LDFLAGS=-ldflags "-X main.Version=${VERSION} 36 var ( 37 // public 38 Project = "sbom-utility" 39 Binary = "unset" 40 Version = "x.y.z" 41 Logger *log.MiniLogger 42 43 // Default configurations 44 DefaultLogLevel = log.INFO 45 ) 46 47 func init() { 48 // Create logger at the earliest 49 // NOTE: This logger will not apply to `go test` as package "main" will not be loaded 50 Logger = log.NewLogger(DefaultLogLevel) 51 52 // Check for log-related flags (anywhere) and apply to logger 53 // as early as possible (before customary Cobra flag formalization) 54 // NOTE: the last log-level flag found, in order of appearance "wins" 55 // Set default log level and turn "quiet mode" off 56 Logger.InitLogLevelAndModeFromFlags() 57 58 // Emit log level used from this point forward 59 Logger.Tracef("Logger (%T) created: with Level=`%v`", Logger, Logger.GetLevelName()) 60 61 // Provide access to project logger to other modules 62 cmd.ProjectLogger = Logger 63 schema.ProjectLogger = Logger 64 65 // Copy program package vars into command flags 66 utils.GlobalFlags.Project = Project 67 utils.GlobalFlags.Binary = Binary 68 utils.GlobalFlags.Version = Version 69 70 // Capture working directory 71 utils.GlobalFlags.WorkingDir, _ = os.Getwd() 72 73 // Set the executable directory path 74 execNameWithPath, err := os.Executable() 75 if err == nil { 76 utils.GlobalFlags.ExecDir = filepath.Dir(execNameWithPath) 77 } 78 } 79 80 func printWelcome() { 81 if !Logger.QuietModeOn() { 82 goos := fmt.Sprintf("(%s/%s)", runtime.GOOS, runtime.GOARCH) 83 echo := fmt.Sprintf("Welcome to the %s! Version `%s` (%s) %s\n", Project, Version, Binary, goos) 84 // Logger will only print the welcome if log level requested indicates INFO level (or higher) 85 Logger.DumpString(echo) 86 // Show intent to not check for error returns as there is no recovery 87 _, _ = Logger.DumpSeparator('=', len(echo)-1) 88 } 89 } 90 91 func main() { 92 Logger.Enter() 93 defer Logger.Exit() 94 printWelcome() 95 // Use Cobra convention and execute top-level command 96 cmd.Execute() 97 }