Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 25 additions & 26 deletions cmd/licensei/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"log"
"os"

"github.com/spf13/cobra"
Expand All @@ -11,61 +12,59 @@ import (
"github.com/goph/licensei/internal/cmd/licensei"
)

// nolint: gochecknoglobals
var config string

// nolint: gochecknoglobals
var debug bool

// nolint: gochecknoinits
func init() {
cobra.OnInitialize(initConfig)
func newRootCommand(options *licensei.GlobalOptions) *cobra.Command {
cmd := &cobra.Command{
Use: "licensei",
Short: "License master",
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
return initConfig(options)
},
}

rootCmd.PersistentFlags().StringVar(&config, "config", "", "config file (default is $PWD/.licensei.yaml)")
rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "enable debug logging")
cmd.PersistentFlags().StringVar(&options.Config, "config", "", "config file (default is $PWD/.licensei.yaml)")
cmd.PersistentFlags().StringVar(&options.Path, "path", "", "path to project (the current directory is used by default)")
cmd.PersistentFlags().BoolVar(&options.Debug, "debug", false, "enable debug logging")

licensei.AddCommands(rootCmd)
return cmd
}

func initConfig() {
func initConfig(options *licensei.GlobalOptions) error {
viper.AutomaticEnv()

if config != "" {
if options.Config != "" {
// Use config file from the flag.
viper.SetConfigFile(config)
viper.SetConfigFile(options.Config)
} else {
viper.AddConfigPath(".")
viper.SetConfigName(".licensei")
}

if err := viper.ReadInConfig(); err != nil {
fmt.Println("can't read config:", err)
os.Exit(1)
return fmt.Errorf("can't read config: %w", err)
}

logHandler := slog.HandlerOptions{
AddSource: debug,
AddSource: options.Debug,
Level: slog.InfoLevel,
}

if debug {
if options.Debug {
logHandler.Level = slog.DebugLevel
}

logger := slog.New(logHandler.NewTextHandler(os.Stderr))

slog.SetDefault(logger)
}

// nolint: gochecknoglobals
var rootCmd = &cobra.Command{
Use: "licensei",
Short: "License master",
return nil
}

func Execute() {
globalOptions := &licensei.GlobalOptions{}
rootCmd := newRootCommand(globalOptions)
licensei.AddCommands(rootCmd, globalOptions)

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
log.Fatalln(err)
}
}
13 changes: 8 additions & 5 deletions internal/cmd/licensei/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type cacheOptions struct {
githubToken string
}

func NewCacheCommand() *cobra.Command {
func NewCacheCommand(globalOptions *GlobalOptions) *cobra.Command {
var options cacheOptions

cmd := &cobra.Command{
Expand All @@ -25,7 +25,7 @@ func NewCacheCommand() *cobra.Command {
RunE: func(_ *cobra.Command, _ []string) error {
options.githubToken = viper.GetString("github_token")

return runCache(options)
return runCache(globalOptions, options)
},
}

Expand All @@ -36,7 +36,7 @@ func NewCacheCommand() *cobra.Command {
return cmd
}

func runCache(options cacheOptions) error {
func runCache(globalOptions *GlobalOptions, options cacheOptions) error {
logger := slog.Default()

logger.Debug("start cache")
Expand All @@ -46,10 +46,13 @@ func runCache(options cacheOptions) error {

// Invalidate cache data
if options.update {
source := licensei.NewAggregatedDependencySource(logger)
source := licensei.NewAggregatedDependencySource(logger, globalOptions.Path)
dependencies, err = source.Dependencies()
} else {
source := licensei.NewCacheProjectSource(licensei.NewAggregatedDependencySource(logger), logger)
source := licensei.NewCacheProjectSource(
licensei.NewAggregatedDependencySource(logger, globalOptions.Path),
logger,
)
dependencies, err = source.Dependencies()
}
if err != nil {
Expand Down
11 changes: 7 additions & 4 deletions internal/cmd/licensei/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type checkOptions struct {
githubToken string
}

func NewCheckCommand() *cobra.Command {
func NewCheckCommand(globalOptions *GlobalOptions) *cobra.Command {
var options checkOptions

cmd := &cobra.Command{
Expand All @@ -31,13 +31,13 @@ func NewCheckCommand() *cobra.Command {

options.githubToken = viper.GetString("github_token")

return runCheck(options)
return runCheck(globalOptions, options)
},
}

return cmd
}
func runCheck(options checkOptions) error {
func runCheck(globalOptions *GlobalOptions, options checkOptions) error {
logger := slog.Default()

logger.Debug("start check")
Expand All @@ -48,7 +48,10 @@ func runCheck(options checkOptions) error {
return nil
}

source := licensei.NewCacheProjectSource(licensei.NewAggregatedDependencySource(logger), logger)
source := licensei.NewCacheProjectSource(
licensei.NewAggregatedDependencySource(logger, globalOptions.Path),
logger,
)
dependencies, err := source.Dependencies()
if err != nil {
return err
Expand Down
12 changes: 6 additions & 6 deletions internal/cmd/licensei/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
)

// AddCommands adds licensei commands to a Cobra command.
func AddCommands(cmd *cobra.Command) {
func AddCommands(cmd *cobra.Command, globalOptions *GlobalOptions) {
cmd.AddCommand(
NewListCommand(),
NewCheckCommand(),
NewCacheCommand(),
NewHeaderCommand(),
NewStatCommand(),
NewListCommand(globalOptions),
NewCheckCommand(globalOptions),
NewCacheCommand(globalOptions),
NewHeaderCommand(globalOptions),
NewStatCommand(globalOptions),
)
}
18 changes: 11 additions & 7 deletions internal/cmd/licensei/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type headerOptions struct {
authors []string
}

func NewHeaderCommand() *cobra.Command {
func NewHeaderCommand(globalOptions *GlobalOptions) *cobra.Command {
var options headerOptions

cmd := &cobra.Command{
Expand All @@ -29,24 +29,28 @@ func NewHeaderCommand() *cobra.Command {
options.ignoreFiles = viper.GetStringSlice("header.ignoreFiles")
options.authors = viper.GetStringSlice("header.authors")

return runHeader(options)
return runHeader(globalOptions, options)
},
}

return cmd
}

func runHeader(options headerOptions) error {
wd, err := os.Getwd()
if err != nil {
return err
func runHeader(globalOptions *GlobalOptions, options headerOptions) (err error) {
target := globalOptions.Path

if target == "" {
target, err = os.Getwd()
if err != nil {
return err
}
}

violations, err := licensei.HeaderChecker{
IgnorePaths: options.ignorePaths,
IgnoreFiles: options.ignoreFiles,
Authors: options.authors,
}.Check(wd, options.template)
}.Check(target, options.template)
if err != nil {
return err
}
Expand Down
11 changes: 7 additions & 4 deletions internal/cmd/licensei/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type listView interface {
Render(model licensei.ListViewModel) error
}

func NewListCommand() *cobra.Command {
func NewListCommand(globalOptions *GlobalOptions) *cobra.Command {
var options listOptions

cmd := &cobra.Command{
Expand All @@ -30,7 +30,7 @@ func NewListCommand() *cobra.Command {
RunE: func(_ *cobra.Command, _ []string) error {
options.githubToken = viper.GetString("github_token")

return runList(options)
return runList(globalOptions, options)
},
}

Expand All @@ -41,7 +41,7 @@ func NewListCommand() *cobra.Command {
return cmd
}

func runList(options listOptions) error {
func runList(globalOptions *GlobalOptions, options listOptions) error {
logger := slog.Default()

logger.Debug("start list")
Expand All @@ -57,7 +57,10 @@ func runList(options listOptions) error {
return errors.New("unsupported format: " + options.format)
}

source := licensei.NewCacheProjectSource(licensei.NewAggregatedDependencySource(logger), logger)
source := licensei.NewCacheProjectSource(
licensei.NewAggregatedDependencySource(logger, globalOptions.Path),
logger,
)
dependencies, err := source.Dependencies()
if err != nil {
return err
Expand Down
7 changes: 7 additions & 0 deletions internal/cmd/licensei/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package licensei

type GlobalOptions struct {
Config string
Debug bool
Path string
}
11 changes: 7 additions & 4 deletions internal/cmd/licensei/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type statOptions struct {
githubToken string
}

func NewStatCommand() *cobra.Command {
func NewStatCommand(globalOptions *GlobalOptions) *cobra.Command {
var options statOptions

cmd := &cobra.Command{
Expand All @@ -26,19 +26,22 @@ func NewStatCommand() *cobra.Command {
RunE: func(cmd *cobra.Command, _ []string) error {
options.githubToken = viper.GetString("github_token")

return runStat(options, cmd.OutOrStderr())
return runStat(globalOptions, options, cmd.OutOrStderr())
},
}

return cmd
}

func runStat(options statOptions, stdout io.Writer) error {
func runStat(globalOptions *GlobalOptions, options statOptions, stdout io.Writer) error {
logger := slog.Default()

logger.Debug("start stat")

source := licensei.NewCacheProjectSource(licensei.NewAggregatedDependencySource(logger), logger)
source := licensei.NewCacheProjectSource(
licensei.NewAggregatedDependencySource(logger, globalOptions.Path),
logger,
)
dependencies, err := source.Dependencies()
if err != nil {
return err
Expand Down
6 changes: 4 additions & 2 deletions internal/licensei/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func (s *cacheDependencySource) Dependencies() ([]Dependency, error) {
logger.Debug("load cache file")

cacheFile, err := os.Open(".licensei.cache")
defer func() {
_ = cacheFile.Close()
}()

if err == nil {
p, err := ReadCache(cacheFile)
if err != nil {
Expand All @@ -71,8 +75,6 @@ func (s *cacheDependencySource) Dependencies() ([]Dependency, error) {
return nil, err
}

_ = cacheFile.Close()

logger.Debug("load dependencies")

sourceDependencies, err := s.delegatedDependencySource.Dependencies()
Expand Down
6 changes: 3 additions & 3 deletions internal/licensei/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package licensei_test

import (
"bytes"
"io/ioutil"
"os"
"testing"

. "github.com/goph/licensei/internal/licensei"
Expand All @@ -23,7 +23,7 @@ func TestJsonListView_Render(t *testing.T) {
},
}

result, err := ioutil.ReadFile("testdata/list/golden0.json")
result, err := os.ReadFile("testdata/list/golden0.json")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -53,7 +53,7 @@ func TestTableListView_Render(t *testing.T) {
},
}

result, err := ioutil.ReadFile("testdata/list/golden0.table")
result, err := os.ReadFile("testdata/list/golden0.table")
if err != nil {
t.Fatal(err)
}
Expand Down
Loading