env.go 2.66 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*Package cmd is used for building command line commands

Copyright © 2020 Addshore

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cmd

import (
	"fmt"

23
	"gerrit.wikimedia.org/r/mediawiki/tools/cli/internal/util/dotenv"
24
25
26
27
28
	"github.com/spf13/cobra"
)

/*Env top level env command*/
func Env(Short string) *cobra.Command {
Addshore's avatar
Addshore committed
29
	return &cobra.Command{
30
31
32
		Use:   "env",
		Short: Short,
		RunE:  nil,
33
34
35
		PersistentPreRun: func(cmd *cobra.Command, args []string) {
			// Do nothing, but override any other PersistentPreRuns
		},
36
37
38
39
	}
}

/*EnvDelete env delete command*/
Addshore's avatar
Addshore committed
40
41
func EnvDelete(directory func() string) *cobra.Command {
	return &cobra.Command{
42
43
44
45
		Use:   "delete [name]",
		Short: "Deletes an environment variable",
		Args:  cobra.MinimumNArgs(1),
		Run: func(cmd *cobra.Command, args []string) {
46
			dotenv.FileForDirectory(directory()).Delete(args[0])
47
48
49
50
51
		},
	}
}

/*EnvSet env set command*/
Addshore's avatar
Addshore committed
52
53
func EnvSet(directory func() string) *cobra.Command {
	return &cobra.Command{
54
55
56
57
		Use:   "set [name] [value]",
		Short: "Set an environment variable",
		Args:  cobra.MinimumNArgs(2),
		Run: func(cmd *cobra.Command, args []string) {
58
			dotenv.FileForDirectory(directory()).Set(args[0], args[1])
59
60
61
62
63
		},
	}
}

/*EnvGet env get command*/
Addshore's avatar
Addshore committed
64
65
func EnvGet(directory func() string) *cobra.Command {
	return &cobra.Command{
66
67
68
69
		Use:   "get [name]",
		Short: "Get an environment variable",
		Args:  cobra.MinimumNArgs(1),
		Run: func(cmd *cobra.Command, args []string) {
70
			fmt.Println(dotenv.FileForDirectory(directory()).Get(args[0]))
71
72
73
74
75
		},
	}
}

/*EnvList env list command*/
Addshore's avatar
Addshore committed
76
77
func EnvList(directory func() string) *cobra.Command {
	return &cobra.Command{
78
79
80
		Use:   "list",
		Short: "List all environment variables",
		Run: func(cmd *cobra.Command, args []string) {
81
			for name, value := range dotenv.FileForDirectory(directory()).List() {
82
83
84
85
86
87
88
				fmt.Println(name + "=" + value)
			}
		},
	}
}

/*EnvWhere env where command*/
Addshore's avatar
Addshore committed
89
90
func EnvWhere(directory func() string) *cobra.Command {
	return &cobra.Command{
91
92
93
		Use:   "where",
		Short: "Output the location of the .env file",
		Run: func(cmd *cobra.Command, args []string) {
94
			fmt.Println(dotenv.FileForDirectory(directory()).Path())
95
96
		},
	}
Addshore's avatar
Addshore committed
97
}