github.com/jacobsoderblom/buffalo@v0.11.0/buffalo/cmd/updater/dep.go (about)

     1  package updater
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"os/exec"
     8  
     9  	"github.com/gobuffalo/envy"
    10  	"github.com/markbates/deplist"
    11  	"github.com/pkg/errors"
    12  	"golang.org/x/sync/errgroup"
    13  )
    14  
    15  type lockToml struct {
    16  	Name     string   `toml:"name"`
    17  	Branch   string   `toml:"branch"`
    18  	Packages []string `toml:"packages"`
    19  	Revision string   `toml:"revision"`
    20  	Version  string   `toml:"version"`
    21  }
    22  
    23  func goGetUpdate(r *Runner) error {
    24  	fmt.Println("~~~ Running go get ~~~")
    25  	ctx, cancel := context.WithCancel(context.Background())
    26  	defer cancel()
    27  	wg, _ := errgroup.WithContext(ctx)
    28  	deps, err := deplist.List()
    29  	if err != nil {
    30  		return errors.WithStack(err)
    31  	}
    32  	for dep := range deps {
    33  		args := []string{"get", "-u"}
    34  		args = append(args, dep)
    35  		cc := exec.Command(envy.Get("GO_BIN", "go"), args...)
    36  		f := func() error {
    37  			cc.Stdin = os.Stdin
    38  			cc.Stderr = os.Stderr
    39  			cc.Stdout = os.Stdout
    40  			return cc.Run()
    41  		}
    42  		wg.Go(f)
    43  	}
    44  	err = wg.Wait()
    45  	if err != nil {
    46  		return errors.Errorf("We encountered the following error trying to install and update the dependencies for this application:\n%s", err)
    47  	}
    48  	return nil
    49  }
    50  
    51  // DepEnsure runs `dep ensure -v` to make sure that any newly changed
    52  // imports are added to dep.
    53  func DepEnsure(r *Runner) error {
    54  	if !r.App.WithDep {
    55  		return goGetUpdate(r)
    56  	}
    57  	fmt.Println("~~~ Running dep ensure ~~~")
    58  	cc := exec.Command("dep", "ensure", "-v")
    59  	cc.Stdin = os.Stdin
    60  	cc.Stderr = os.Stderr
    61  	cc.Stdout = os.Stdout
    62  	if err := cc.Run(); err != nil {
    63  		return errors.WithStack(err)
    64  	}
    65  
    66  	apkg := []string{
    67  		"github.com/gobuffalo/tags@v2.0.0",
    68  		"github.com/gobuffalo/suite@v2.0.0",
    69  	}
    70  	args := []string{"ensure", "-v", "-add"}
    71  
    72  	for _, p := range apkg {
    73  		args = append(args, p)
    74  	}
    75  	cc = exec.Command("dep", args...)
    76  	cc.Stdin = os.Stdin
    77  	cc.Stderr = os.Stderr
    78  	cc.Stdout = os.Stdout
    79  	if err := cc.Run(); err != nil {
    80  		return errors.WithStack(err)
    81  	}
    82  
    83  	upkg := []string{
    84  		"github.com/gobuffalo/buffalo",
    85  		"github.com/gobuffalo/plush",
    86  		"github.com/markbates/inflect",
    87  	}
    88  
    89  	args = []string{"ensure", "-v", "-update"}
    90  	for _, p := range upkg {
    91  		args = append(args, p)
    92  	}
    93  	cc = exec.Command("dep", args...)
    94  	cc.Stdin = os.Stdin
    95  	cc.Stderr = os.Stderr
    96  	cc.Stdout = os.Stdout
    97  	return cc.Run()
    98  }