github.com/glimps-jbo/go-licenses@v0.0.0-20230908151000-e06d3c113277/main.go (about) 1 // Copyright 2019 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package main 16 17 import ( 18 "flag" 19 "os" 20 "strings" 21 22 "github.com/spf13/cobra" 23 "k8s.io/klog/v2" 24 ) 25 26 var ( 27 rootCmd = &cobra.Command{ 28 Use: "go-licenses", 29 Short: "go-licenses helps you work with licenses of your go project's dependencies.", 30 Long: `go-licenses helps you work with licenses of your go project's dependencies. 31 32 Prerequisites: 33 1. Go v1.16 or later. 34 2. Change directory to your go project. 35 3. Run "go mod download".`, 36 } 37 38 // Flags shared between subcommands 39 includeTests bool 40 ignore []string 41 packageHelp = ` 42 43 Typically, specify the Go package that builds your Go binary. 44 go-licenses expects the same package argument format as "go build". 45 For example: 46 * A rooted import path like "github.com/google/go-licenses" or "github.com/google/go-licenses/licenses". 47 * A relative path that denotes the package in that directory, like "." or "./cmd/some-command". 48 To learn more about Go package argument, run "go help packages".` 49 ) 50 51 func init() { 52 // Change klog default log level to INFO. 53 klog.InitFlags(nil) 54 err := flag.Set("logtostderr", "true") 55 if err != nil { 56 klog.Error(err) 57 os.Exit(1) 58 } 59 err = flag.Set("stderrthreshold", "INFO") 60 if err != nil { 61 klog.Error(err) 62 os.Exit(1) 63 } 64 rootCmd.PersistentFlags().BoolVar(&includeTests, "include_tests", false, "Include packages only imported by testing code.") 65 rootCmd.PersistentFlags().StringSliceVar(&ignore, "ignore", nil, "Package path prefixes to be ignored. Dependencies from the ignored packages are still checked. Can be specified multiple times.") 66 } 67 68 func main() { 69 flag.Parse() 70 rootCmd.PersistentFlags().AddGoFlagSet(flag.CommandLine) 71 rootCmd.SilenceErrors = true // to avoid duplicate error output 72 rootCmd.SilenceUsage = true // to avoid usage/help output on error 73 74 if err := rootCmd.Execute(); err != nil { 75 klog.Exit(err) 76 } 77 } 78 79 // Unvendor removes the "*/vendor/" prefix from the given import path, if present. 80 func unvendor(importPath string) string { 81 if vendorerAndVendoree := strings.SplitN(importPath, "/vendor/", 2); len(vendorerAndVendoree) == 2 { 82 return vendorerAndVendoree[1] 83 } 84 return importPath 85 }