cmd

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2025 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var LevelIds = map[zapcore.Level][]string{
	zap.DebugLevel: {"debug"},
	zap.InfoLevel:  {"info"},
	zap.WarnLevel:  {"warn"},
	zap.ErrorLevel: {"error"},
}
View Source
var RootCmd = &cobra.Command{
	Use:   "chasky [command|environ]",
	Short: "Chasky is a tool to generate shell environs for your apps",
	Example: `$ chasky my_app
$ chasky my_app -- echo "I am ${MY_USER_ENV_VAR}"
$ chasky my_app --log-level=debug -- echo "I am ${MY_USER_ENV_VAR}"`,
	Args: cobra.MinimumNArgs(1),
	PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
		log.Init(loglevel, cmd.ErrOrStderr())
		return nil
	},
	RunE: func(cmd *cobra.Command, args []string) error {
		cmd.SilenceUsage = true

		var (
			command         = os.Getenv("SHELL")
			commandArg      []string
			isCustomCommand bool
		)

		if len(args) > 1 {
			if !slices.Contains(os.Args, "--") {
				return errors.New("unknown command")
			}

			if len(args) > 2 {
				command = args[1]
				commandArg = args[2:]
				isCustomCommand = true
			}
		}

		ctx := cmd.Context()
		var envName string

		conf, err := config.Parse(ctx)
		if err != nil {
			return err
		}

		envName = args[0]

		var afterRender = func() {}
		if !isCustomCommand {
			s := spinner.New(spinner.CharSets[26], 200*time.Millisecond)
			s.Prefix = fmt.Sprintf("Generating the environment for %q", envName)
			s.FinalMSG = fmt.Sprintf("Generated environment for %q successfully!\n", envName)
			s.Suffix = "\n"
			s.Start()
			afterRender = s.Stop
		}

		cfg, ok := conf[envName]
		if !ok {
			return fmt.Errorf("unknown environment %s", envName)
		}

		env, err := environ.Render(ctx, cfg.Values)
		if err != nil {
			return fmt.Errorf("rendering environment: %w", err)
		}

		afterRender()

		defer func() {
			if err = env.Close(); err != nil {
				log.Logger.Warn("Failed to close environment", zap.Error(err))
			}
		}()

		envvars := append(env.EnvVars, fmt.Sprintf("CHASKY_ENVNAME=%s", envName))
		c := exec.CommandContext(cmd.Context(), command, commandArg...)
		c.Env = append(envvars, os.Environ()...)
		c.Stderr = os.Stderr
		c.Stdout = os.Stdout
		c.Stdin = os.Stdin

		if err := c.Start(); err != nil {
			return fmt.Errorf("starting environment: %w", err)
		}

		if !isCustomCommand {
			if len(env.WelcomeMsgs) > 0 {
				fmt.Println("")
				for _, msg := range env.WelcomeMsgs {
					fmt.Println(msg)
				}
			}
		}

		return c.Wait()
	},
	PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
		return log.Close()
	},
	SilenceUsage:  false,
	SilenceErrors: true,
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL