github.com/prysmaticlabs/prysm@v1.4.4/shared/tos/tos.go (about)

     1  package tos
     2  
     3  import (
     4  	"errors"
     5  	"path/filepath"
     6  	"strings"
     7  
     8  	"github.com/logrusorgru/aurora"
     9  	"github.com/prysmaticlabs/prysm/shared/cmd"
    10  	"github.com/prysmaticlabs/prysm/shared/fileutil"
    11  	"github.com/prysmaticlabs/prysm/shared/promptutil"
    12  	"github.com/sirupsen/logrus"
    13  	"github.com/urfave/cli/v2"
    14  )
    15  
    16  const (
    17  	acceptTosFilename   = "tosaccepted"
    18  	acceptTosPromptText = `
    19  Prysmatic Labs Terms of Use
    20  
    21  By downloading, accessing or using the Prysm implementation (“Prysm”), you (referenced herein
    22  as “you” or the “user”) certify that you have read and agreed to the terms and conditions below.
    23  
    24  TERMS AND CONDITIONS: https://github.com/prysmaticlabs/prysm/blob/master/TERMS_OF_SERVICE.md
    25  
    26  
    27  Type "accept" to accept this terms and conditions [accept/decline]:`
    28  	acceptTosPromptErrText = `could not scan text input, if you are trying to run in non-interactive environment, you
    29  can use the --accept-terms-of-use flag after reading the terms and conditions here: 
    30  https://github.com/prysmaticlabs/prysm/blob/master/TERMS_OF_SERVICE.md`
    31  )
    32  
    33  var (
    34  	au  = aurora.NewAurora(true)
    35  	log = logrus.WithField("prefix", "tos")
    36  )
    37  
    38  // VerifyTosAcceptedOrPrompt check if Tos was accepted before or asks to accept.
    39  func VerifyTosAcceptedOrPrompt(ctx *cli.Context) error {
    40  	if ctx.Bool(cmd.E2EConfigFlag.Name) {
    41  		return nil
    42  	}
    43  
    44  	if fileutil.FileExists(filepath.Join(ctx.String(cmd.DataDirFlag.Name), acceptTosFilename)) {
    45  		return nil
    46  	}
    47  	if ctx.Bool(cmd.AcceptTosFlag.Name) {
    48  		saveTosAccepted(ctx)
    49  		return nil
    50  	}
    51  
    52  	input, err := promptutil.DefaultPrompt(au.Bold(acceptTosPromptText).String(), "decline")
    53  	if err != nil {
    54  		return errors.New(acceptTosPromptErrText)
    55  	}
    56  	if !strings.EqualFold(input, "accept") {
    57  		return errors.New("you have to accept Terms and Conditions in order to continue")
    58  	}
    59  
    60  	saveTosAccepted(ctx)
    61  	return nil
    62  }
    63  
    64  // saveTosAccepted creates a file when Tos accepted.
    65  func saveTosAccepted(ctx *cli.Context) {
    66  	dataDir := ctx.String(cmd.DataDirFlag.Name)
    67  	dataDirExists, err := fileutil.HasDir(dataDir)
    68  	if err != nil {
    69  		log.WithError(err).Warnf("error checking directory: %s", dataDir)
    70  	}
    71  	if !dataDirExists {
    72  		if err := fileutil.MkdirAll(dataDir); err != nil {
    73  			log.WithError(err).Warnf("error creating directory: %s", dataDir)
    74  		}
    75  	}
    76  	if err := fileutil.WriteFile(filepath.Join(dataDir, acceptTosFilename), []byte("")); err != nil {
    77  		log.WithError(err).Warnf("error writing %s to file: %s", cmd.AcceptTosFlag.Name,
    78  			filepath.Join(dataDir, acceptTosFilename))
    79  	}
    80  }