github.com/joey-fossa/fossa-cli@v0.7.34-0.20190708193710-569f1e8679f0/config/config.go (about)

     1  // Package config implements application-level configuration functionality.
     2  //
     3  // It works by loading configuration sources (e.g. CLI flags, configuration
     4  // files, local VCS) and providing functions which compute relevant
     5  // configuration values from these sources.
     6  //
     7  // This design is intended to make how a particular value is computed very
     8  // clear. All values can have their computation strategy modified independently
     9  // of all other values. It should also be easy to determine which source set a
    10  // particular configuration value.
    11  package config
    12  
    13  import (
    14  	"github.com/apex/log"
    15  	"github.com/urfave/cli"
    16  
    17  	"github.com/fossas/fossa-cli/vcs"
    18  )
    19  
    20  var (
    21  	ctx  *cli.Context
    22  	file File = NoFile{}
    23  
    24  	repo vcs.System
    25  )
    26  
    27  // SetContext initializes application-level configuration.
    28  func SetContext(c *cli.Context) error {
    29  	// First, set the CLI flags.
    30  	ctx = c
    31  
    32  	// Second, try to load a configuration file.
    33  	f, fname, err := ReadFile(c)
    34  	if err != nil {
    35  		return err
    36  	}
    37  
    38  	log.WithField("filename", fname).Debug("loaded configuration file")
    39  	file = f
    40  	filename = fname
    41  
    42  	// Third, try to open the local VCS repository.
    43  	vcsType, dir, err := vcs.Nearest(".")
    44  	if err != nil {
    45  		if err == vcs.ErrNoNearestVCS {
    46  			vcsType = vcs.None
    47  			dir = "."
    48  		} else {
    49  			return err
    50  		}
    51  	}
    52  
    53  	var r vcs.System
    54  
    55  	switch vcsType {
    56  	case vcs.Git:
    57  		r, err = vcs.NewGitRepository(dir)
    58  	case vcs.Subversion:
    59  		r, err = vcs.NewSubversionRepository(dir)
    60  	case vcs.Mercurial:
    61  		r, err = vcs.NewMercurialRepository(dir)
    62  	case vcs.None:
    63  		r, err = vcs.NewNoRepository(dir)
    64  	default:
    65  		log.Warnf("FOSSA is unable to extract VCS context from your %s project, which means you will not "+
    66  			"be able to take advantage of VCS history information on fossa.com, but a scan will run.", vcsType)
    67  		r, err = vcs.NewNoRepository(dir)
    68  	}
    69  
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	repo = r
    75  	return nil
    76  }