github.com/goliatone/go-envset@v0.7.0/cmd/envset/environment/environment.go (about)

     1  package environment
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  
     7  	"github.com/goliatone/go-envset/pkg/config"
     8  	"github.com/goliatone/go-envset/pkg/envset"
     9  	"github.com/goliatone/go-envset/pkg/exec"
    10  	"github.com/urfave/cli/v2"
    11  )
    12  
    13  var excludeFromRestart bool
    14  
    15  // GetCommand export command
    16  func GetCommand(env string, ecmd exec.ExecCmd, cnf *config.Config) *cli.Command {
    17  
    18  	excludeFromRestart = cnf.RestartForEnv(env)
    19  
    20  	return &cli.Command{
    21  		Name:        env,
    22  		Usage:       fmt.Sprintf("load \"%s\" environment in current shell session", env),
    23  		UsageText:   fmt.Sprintf("envset %s [options] -- [command] [arguments...]", env),
    24  		Description: "This will load the environment and execute the provided command",
    25  		Category:    "environments",
    26  		Flags: []cli.Flag{
    27  			&cli.BoolFlag{
    28  				Name:  "isolated",
    29  				Usage: "if true we run shell with only variables defined",
    30  				Value: cnf.Isolated, //call with --isolated=false to show all
    31  			},
    32  			&cli.BoolFlag{
    33  				Name:  "expand",
    34  				Usage: "if true we use expand environment variables",
    35  				Value: cnf.Expand,
    36  			},
    37  			&cli.StringFlag{
    38  				Name:  "env-file",
    39  				Usage: "file name with environment definition",
    40  				Value: cnf.Filename,
    41  			},
    42  			&cli.StringSliceFlag{
    43  				Name:    "required",
    44  				Aliases: []string{"R"},
    45  				Usage:   "list of key names that are required to run",
    46  			},
    47  			&cli.StringFlag{
    48  				Name:    "export-env-name",
    49  				Aliases: []string{"N"},
    50  				Usage:   "name of exported variable with current environment name",
    51  				Value:   cnf.ExportEnvName,
    52  			},
    53  			&cli.StringSliceFlag{
    54  				Name:    "inherit",
    55  				Aliases: []string{"I"},
    56  				Usage:   "list of env vars to inherit from shell",
    57  			},
    58  			&cli.BoolFlag{
    59  				Name:  "restart",
    60  				Usage: "re-execute command when it exit is error code",
    61  				Value: excludeFromRestart,
    62  			},
    63  			&cli.BoolFlag{
    64  				Name:  "forever",
    65  				Usage: "forever re-execute command when it exit is error code",
    66  				Value: cnf.RestartForever,
    67  			},
    68  			&cli.IntFlag{
    69  				Name:    "max-restarts",
    70  				Aliases: []string{"max-restart"},
    71  				Usage:   "times to restart failed command",
    72  				Value:   cnf.MaxRestarts,
    73  			},
    74  		},
    75  		Action: func(c *cli.Context) error {
    76  			//TODO: we want to support .env.local => [local]
    77  			env := c.Command.Name
    78  
    79  			required := c.StringSlice("required")
    80  			required = cnf.MergeRequired(env, required)
    81  
    82  			max := c.Int("max-restarts")
    83  			restart := c.Bool("restart")
    84  			if c.Bool("forever") && !excludeFromRestart {
    85  				max = math.MaxInt
    86  				restart = true
    87  			}
    88  
    89  			o := envset.RunOptions{
    90  				Cmd:                 ecmd.Cmd,
    91  				Args:                ecmd.Args,
    92  				Isolated:            c.Bool("isolated"),
    93  				Expand:              c.Bool("expand"),
    94  				Filename:            c.String("env-file"),
    95  				CommentSectionNames: cnf.CommentSectionNames.Keys,
    96  				Required:            required,
    97  				Inherit:             c.StringSlice("inherit"),
    98  				ExportEnvName:       c.String("export-env-name"),
    99  				Restart:             restart,
   100  				MaxRestarts:         max,
   101  			}
   102  
   103  			if ecmd.Cmd == "" {
   104  				return envset.Print(env, o)
   105  			}
   106  
   107  			return envset.Run(env, o)
   108  		},
   109  	}
   110  }