github.com/tcncloud/wollemi@v0.8.1/main.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  
    10  	"github.com/tcncloud/wollemi/adapters/bazel"
    11  	"github.com/tcncloud/wollemi/adapters/cobra"
    12  	"github.com/tcncloud/wollemi/adapters/filesystem"
    13  	"github.com/tcncloud/wollemi/adapters/golang"
    14  	"github.com/tcncloud/wollemi/adapters/logrus"
    15  	"github.com/tcncloud/wollemi/adapters/please"
    16  	"github.com/tcncloud/wollemi/domain/wollemi"
    17  	"github.com/tcncloud/wollemi/ports/ctl"
    18  	"github.com/tcncloud/wollemi/ports/logging"
    19  )
    20  
    21  func main() {
    22  	app := NewApplication()
    23  
    24  	if err := cobra.Ctl(app).Execute(); err != nil {
    25  		os.Exit(1)
    26  	}
    27  }
    28  
    29  func NewApplication() *Application {
    30  	return &Application{
    31  		logger: logrus.NewLogger(os.Stderr),
    32  	}
    33  }
    34  
    35  type Application struct {
    36  	logger logging.Logger
    37  }
    38  
    39  func (app *Application) Logger() logging.Logger {
    40  	return app.logger
    41  }
    42  
    43  func (app *Application) Wollemi() (ctl.Wollemi, error) {
    44  	wd, err := os.Getwd()
    45  	if err != nil {
    46  		return nil, fmt.Errorf("could not get working directory: %v", err)
    47  	}
    48  
    49  	if path, err := os.Readlink(wd); err == nil {
    50  		wd = path
    51  	}
    52  
    53  	log := app.Logger()
    54  	golang := golang.NewImporter()
    55  	filesystem := filesystem.NewFilesystem(log)
    56  
    57  	root, ok := (func(path string) (string, bool) {
    58  		for ; path != "/"; path = filepath.Dir(path) {
    59  			_, err := filesystem.Stat(filepath.Join(path, ".plzconfig"))
    60  			if err == nil {
    61  				return path, true
    62  			}
    63  		}
    64  
    65  		return "", false
    66  	}(wd))
    67  
    68  	if !ok {
    69  		return nil, fmt.Errorf("could not find root .plzconfig")
    70  	}
    71  
    72  	if err := filesystem.Chdir(root); err != nil {
    73  		return nil, fmt.Errorf("could not chdir: %v", err)
    74  	}
    75  
    76  	var gosrc, gopkg string
    77  
    78  	buf := bytes.NewBuffer(nil)
    79  	if err := filesystem.ReadAll(buf, filepath.Join(root, "go.mod")); err == nil {
    80  		gopkg = golang.ModulePath(buf.Bytes())
    81  	} else if !os.IsNotExist(err) {
    82  		return nil, fmt.Errorf("could not read go.mod: %v", err)
    83  	}
    84  
    85  	if gopkg == "" {
    86  		for _, gopath := range strings.Split(golang.GOPATH(), ":") {
    87  			gosrc = filepath.Join(gopath, "src") + "/"
    88  
    89  			if strings.HasPrefix(root, gosrc) {
    90  				gopkg = strings.TrimPrefix(root, gosrc)
    91  				break
    92  			}
    93  		}
    94  	}
    95  
    96  	if gopkg == "" && gosrc != root {
    97  		gosrc = root
    98  	}
    99  
   100  	pleaseCtl := please.NewCtl()
   101  
   102  	config, err := pleaseCtl.Config(filepath.Join(root, ".plzconfig"))
   103  	if err == nil && config.Go.ImportPath != "" {
   104  		gopkg = config.Go.ImportPath
   105  	}
   106  
   107  	bazel := bazel.NewBuilder(log, pleaseCtl, filesystem)
   108  
   109  	log.WithField("working_directory", wd).
   110  		WithField("project_root", root).
   111  		WithField("go_package", gopkg).
   112  		WithField("go_path", golang.GOPATH()).
   113  		WithField("go_root", golang.GOROOT()).
   114  		Debug("wollemi initialized")
   115  
   116  	return wollemi.New(log, filesystem, golang, bazel, root, wd, gosrc, gopkg), nil
   117  }