Home

tmpl @main - refs - log - search -
https://git.jolheiser.com/tmpl.git
Template automation
cmd/env.go - raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package cmd

import (
	"os"

	"go.jolheiser.com/tmpl/env"

	"github.com/rs/zerolog/log"
	"github.com/urfave/cli/v2"
)

var Env = &cli.Command{
	Name:        "env",
	Usage:       "Show tmpl environment variables",
	Description: "Show tmpl environment variables and their configuration",
	Action:      runEnv,
	Subcommands: []*cli.Command{
		{
			Name:        "set",
			Aliases:     []string{"add"},
			Usage:       "Set a tmpl environment variable (stored plaintext)",
			Description: "Set an environment variable that will be loaded specifically when running tmpl (stored plaintext)",
			ArgsUsage:   "[key] [value]",
			Action:      runEnvSet,
		},
		{
			Name:        "unset",
			Aliases:     []string{"delete"},
			Usage:       "Unsets a tmpl environment variable",
			Description: "Unsets an environment variable previously set for tmpl",
			ArgsUsage:   "[key]",
			Action:      runEnvUnset,
		},
	},
}

func runEnv(_ *cli.Context) error {
	// Source
	log.Info().Str("TMPL_SOURCE", os.Getenv("TMPL_SOURCE")).Msg("system")

	// Registry Path
	log.Info().Str("TMPL_REGISTRY", os.Getenv("TMPL_REGISTRY")).Msg("system")

	// Branch
	log.Info().Str("TMPL_BRANCH", os.Getenv("TMPL_BRANCH")).Msg("system")

	// Custom
	e, err := env.Load(registryFlag)
	if err != nil {
		return err
	}
	for key, val := range e {
		log.Info().Str(key, val).Msg("env.json")
	}

	return nil
}

func runEnvSet(ctx *cli.Context) error {
	if ctx.NArg() < 2 {
		return cli.ShowCommandHelp(ctx, ctx.Command.Name)
	}
	e, err := env.Load(registryFlag)
	if err != nil {
		return err
	}
	key, val := ctx.Args().Get(0), ctx.Args().Get(1)
	e[key] = val
	if err := env.Save(registryFlag, e); err != nil {
		return err
	}
	log.Info().Str(key, val).Msg("Successfully saved tmpl environment variable!")
	return nil
}

func runEnvUnset(ctx *cli.Context) error {
	if ctx.NArg() < 1 {
		return cli.ShowCommandHelp(ctx, ctx.Command.Name)
	}
	e, err := env.Load(registryFlag)
	if err != nil {
		return err
	}
	key := ctx.Args().First()
	val := e[key]
	delete(e, key)
	if err := env.Save(registryFlag, e); err != nil {
		return err
	}
	log.Info().Str(key, val).Msg("Successfully unset tmpl environment variable!")
	return nil
}