github.com/ldez/gomoddirectives@v0.2.4/cmd/gomoddirectives/gomoddirectives.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"log"
     7  	"os"
     8  	"strings"
     9  
    10  	"github.com/ldez/gomoddirectives"
    11  )
    12  
    13  type flagSlice []string
    14  
    15  func (f flagSlice) String() string {
    16  	return strings.Join(f, ":")
    17  }
    18  
    19  func (f *flagSlice) Set(s string) error {
    20  	*f = append(*f, strings.Split(s, ",")...)
    21  	return nil
    22  }
    23  
    24  type config struct {
    25  	ReplaceAllowList          flagSlice
    26  	ReplaceAllowLocal         bool
    27  	ExcludeForbidden          bool
    28  	RetractAllowNoExplanation bool
    29  }
    30  
    31  func main() {
    32  	cfg := config{}
    33  
    34  	flag.BoolVar(&cfg.ReplaceAllowLocal, "local", false, "Allow local replace directives")
    35  	flag.Var(&cfg.ReplaceAllowList, "list", "List of allowed replace directives")
    36  	flag.BoolVar(&cfg.RetractAllowNoExplanation, "retract-no-explanation", false, "Allow to use retract directives without explanation")
    37  	flag.BoolVar(&cfg.ExcludeForbidden, "exclude", false, "Forbid the use of exclude directives")
    38  
    39  	help := flag.Bool("h", false, "Show this help.")
    40  
    41  	flag.Usage = usage
    42  	flag.Parse()
    43  
    44  	if *help {
    45  		usage()
    46  	}
    47  
    48  	results, err := gomoddirectives.Analyze(gomoddirectives.Options{
    49  		ReplaceAllowList:          cfg.ReplaceAllowList,
    50  		ReplaceAllowLocal:         cfg.ReplaceAllowLocal,
    51  		ExcludeForbidden:          cfg.ExcludeForbidden,
    52  		RetractAllowNoExplanation: cfg.RetractAllowNoExplanation,
    53  	})
    54  	if err != nil {
    55  		log.Fatal(err)
    56  	}
    57  
    58  	for _, e := range results {
    59  		fmt.Println(e)
    60  	}
    61  
    62  	if len(results) > 0 {
    63  		os.Exit(1)
    64  	}
    65  }
    66  
    67  func usage() {
    68  	_, _ = os.Stderr.WriteString(`GoModDirectives
    69  
    70  gomoddirectives [flags]
    71  
    72  Flags:
    73  `)
    74  	flag.PrintDefaults()
    75  	os.Exit(2)
    76  }