Skip to content

Commit a9630ec

Browse files
committed
add dot-env command, extra values
1 parent 955af0e commit a9630ec

30 files changed

Lines changed: 23964 additions & 14564 deletions

File tree

cmd/application.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"fmt"
55
"log"
6+
"time"
67

78
"github.com/spf13/cobra"
89
ybApi "github.com/yottab/proto-api/proto"
@@ -14,6 +15,7 @@ var (
1415
minScale uint64
1516
flagVarImage string
1617
flagVarEndpointType string
18+
flagVarExtra map[string]string
1719
)
1820

1921
func appList(cmd *cobra.Command, args []string) {
@@ -65,6 +67,8 @@ func appRun(cmd *cobra.Command, args []string) {
6567
log.Fatal("no command run")
6668
}
6769
req.Command = args[1:]
70+
//TODO: will be replaced by an interactive aproach
71+
clientTimeout = 60 * time.Minute
6872
client := grpcConnect()
6973
defer client.Close()
7074
output, err := client.V2().AppShell(client.Context(), req)
@@ -112,7 +116,7 @@ func appDestroy(cmd *cobra.Command, args []string) {
112116
}
113117

114118
// ApplicationCreate create application by link of docker image
115-
func ApplicationCreate(appName, image, plan, EndpointType string, port, minScale uint64) (*ybApi.AppStatusRes, error) {
119+
func ApplicationCreate(appName, image, plan, EndpointType string, port, minScale uint64, otherValues map[string]string) (*ybApi.AppStatusRes, error) {
116120
if err := endpointTypeValid(EndpointType); err != nil {
117121
log.Panic(err)
118122
}
@@ -127,6 +131,11 @@ func ApplicationCreate(appName, image, plan, EndpointType string, port, minScale
127131
req.Values["minimum-scale"] = fmt.Sprintf("%d", minScale)
128132
req.Values["image"] = image
129133

134+
//Fill other values
135+
for key, val := range otherValues {
136+
req.Values[key] = val
137+
}
138+
130139
client := grpcConnect()
131140
defer client.Close()
132141
return client.V2().AppCreate(client.Context(), req)
@@ -139,7 +148,8 @@ func appCreate(cmd *cobra.Command, args []string) {
139148
cmd.Flag("plan").Value.String(),
140149
flagVarEndpointType,
141150
flagVarPort,
142-
flagVarMinScale)
151+
flagVarMinScale,
152+
flagVarExtra)
143153
uiCheckErr("Could not Create the Application", err)
144154
uiApplicationStatus(res)
145155
}
@@ -173,6 +183,13 @@ func appUpdate(cmd *cobra.Command, args []string) {
173183
client := grpcConnect()
174184
defer client.Close()
175185

186+
//Fill other values
187+
for key, val := range flagVarExtra {
188+
if val != "" {
189+
req.Values[key] = val
190+
}
191+
}
192+
176193
res, err := client.V2().AppConfigSet(client.Context(), req)
177194
uiCheckErr("Could not Set the Config for Application", err)
178195
uiApplicationStatus(res)
@@ -198,6 +215,15 @@ func appAddEnvironmentVariable(cmd *cobra.Command, args []string) {
198215
uiApplicationStatus(res)
199216
}
200217

218+
func appAddEnvironmentFromDotEnv(cmd *cobra.Command, args []string) {
219+
res, err := ApplicationAddEnvironmentVariable(
220+
getCliRequiredArg(args, 0),
221+
parseDotEnvFile(flagFile))
222+
223+
uiCheckErr("Could not Add the Environment Variable for Application (Dot env file)", err)
224+
uiApplicationStatus(res)
225+
}
226+
201227
func appRemoveEnvironmentVariable(cmd *cobra.Command, args []string) {
202228
req := new(ybApi.UnsetReq)
203229
req.Name = getCliRequiredArg(args, 0)

cmd/command_create_update.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ var (
2323
$: yb create application \
2424
--image="hub.yottab.io/test/dotnetcore:aspnetapp" \
2525
--name="myaspnetapp" \
26-
--port=80
26+
--port=80 \
27+
--extra "key1=val1,key2=va..."
2728
2829
2930
$: yb create application \
@@ -32,6 +33,7 @@ var (
3233
--min-scale=4 \
3334
--debug=true \
3435
--port=8080 \
36+
--extra "key1=val1,key2=va..." \
3537
--endpoint-type=private`}
3638

3739
srvCreateCmd = &cobra.Command{
@@ -89,12 +91,14 @@ var (
8991
Example: `
9092
## The Application Version is updated
9193
$: yb update myadmin \
92-
--image="hub.yottab.io/test/myadmin:v1.2.3"
94+
--image="hub.yottab.io/test/myadmin:v1.2.3" \
95+
--extra "debug=false,..."
9396
9497
9598
## The Application Scale is updated
9699
$: yb update myadmin \
97-
--min-scale=4`}
100+
--min-scale=4 \
101+
--extra "debug=false,..."`}
98102
)
99103

100104
func init() {
@@ -109,6 +113,7 @@ func init() {
109113
// application Create flag:
110114
appCreateCmd.Flags().StringP("plan", "", "default", "name of plan")
111115
appCreateCmd.Flags().StringP("name", "n", "", "a uniquely identifiable name for the application. No other app can already exist with this name.")
116+
appCreateCmd.Flags().StringToStringVarP(&flagVarExtra, "extra", "x", map[string]string{}, "Set extra values that are available for app")
112117
appCreateCmd.Flags().Uint64VarP(&flagVarPort, "port", "p", 0, "port of application")
113118
appCreateCmd.Flags().StringVarP(&flagVarImage, "image", "i", "", "image of application")
114119
appCreateCmd.Flags().StringVarP(&flagVarEndpointType, "endpoint-type", "e", "http", "Accepted values: http|grpc|private")
@@ -130,6 +135,7 @@ func init() {
130135
updateCmd.Flags().Uint64VarP(&flagVarPort, "port", "p", 0, "port of application")
131136
updateCmd.Flags().StringVarP(&flagVarImage, "image", "i", "", "image of application")
132137
updateCmd.Flags().Uint64VarP(&flagVarMinScale, "min-scale", "m", 0, "min scale of application")
138+
updateCmd.Flags().StringToStringP("extra", "x", map[string]string{}, "Set extra values that are available for app")
133139
updateCmd.Flags().StringVarP(&flagVarEndpointType, "endpoint-type", "e", "http", "Accepted values: http|grpc|private")
134140

135141
rootCmd.AddCommand(

cmd/command_enviroment.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@ import (
55
)
66

77
var (
8-
commentShort = "environment variables [Set/Unset] for an Application"
9-
commentSetLong = `Add list of environment variables to an Application.`
10-
commentUnsetLong = `Remove list of environment variables for an Application.`
11-
commentExample = `
8+
commentShort = "environment variables [Set/Unset] for an Application"
9+
commentSetLong = `Add list of environment variables to an Application.`
10+
commentDotEnvLong = `Add list of environment variables in a dotenv file to an Application.`
11+
commentUnsetLong = `Remove list of environment variables for an Application.`
12+
commentExample = `
1213
$: yb environment unset my-admin
1314
-v="key1"
1415
-v="key2"
1516
1617
$: yb environment set my-admin
1718
-v="key1=value1"
18-
-v="key2=value2"`
19+
-v="key2=value2"
20+
21+
$: yb environment from-dotenv my-admin path/to/.env
22+
`
1923
)
2024

2125
var (
@@ -35,6 +39,13 @@ var (
3539
Long: commentSetLong,
3640
Example: commentExample}
3741

42+
dotEnvCmd = &cobra.Command{
43+
Use: "from-dotenv [app.name]",
44+
Run: appAddEnvironmentFromDotEnv,
45+
Short: commentShort,
46+
Long: commentDotEnvLong,
47+
Example: commentExample}
48+
3849
unsetEnvCmd = &cobra.Command{
3950
Use: "unset [app.name]",
4051
Run: appRemoveEnvironmentVariable,
@@ -48,6 +59,10 @@ func init() {
4859
setEnvCmd.Flags().StringArrayVarP(&flagVariableArray, "variable", "v", nil, "Environment Variable of application")
4960
setEnvCmd.MarkFlagRequired("variable")
5061

62+
// app dot env flag:
63+
dotEnvCmd.Flags().StringVarP(&flagFile, "file", "f", "", "Dot env file")
64+
dotEnvCmd.MarkFlagRequired("file")
65+
5166
// app unset flag:
5267
unsetEnvCmd.Flags().StringArrayVarP(&flagVariableArray, "variable", "v", nil, "Environment Variable of application")
5368
unsetEnvCmd.MarkFlagRequired("variable")
@@ -56,5 +71,6 @@ func init() {
5671
rootCmd.AddCommand(envCmd)
5772
envCmd.AddCommand(
5873
setEnvCmd,
74+
dotEnvCmd,
5975
unsetEnvCmd)
6076
}

cmd/utils.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"syscall"
1313
"time"
1414

15+
"github.com/joho/godotenv"
1516
"github.com/spf13/cobra"
1617
"github.com/spf13/viper"
1718
"github.com/yottab/cli/config"
@@ -25,8 +26,18 @@ var (
2526
flagVariableArray = make([]string, 0, 8)
2627
flagIndex int32
2728
flagAppName string
29+
flagFile string
30+
clientTimeout time.Duration = 10 * time.Second
2831
)
2932

33+
func parseDotEnvFile(filename string) map[string]string {
34+
var myEnv map[string]string
35+
myEnv, err := godotenv.Read(filename)
36+
if err != nil {
37+
panic(err)
38+
}
39+
return myEnv
40+
}
3041
func arrayFlagToMap(flags []string) map[string]string {
3142
varMap := make(map[string]string, len(flags))
3243
for _, v := range flags {
@@ -85,7 +96,7 @@ func grpcConnect() ybApi.Client {
8596
return viper.GetString(config.KEY_TOKEN)
8697
}, func() string {
8798
return version
88-
}, nil))
99+
}, nil), clientTimeout)
89100
}
90101

91102
func toTime(t *ybApi.Timestamp) (out string) {

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ require (
77
github.com/fatih/color v1.9.0
88
github.com/go-ini/ini v1.51.1 // indirect
99
github.com/grpc-ecosystem/grpc-gateway v1.14.6 // indirect
10+
github.com/joho/godotenv v1.3.0
1011
github.com/minio/minio-go v6.0.14+incompatible
1112
github.com/mitchellh/go-homedir v1.1.0
1213
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
1314
github.com/rhysd/go-github-selfupdate v1.2.1
14-
github.com/sirupsen/logrus v1.4.2 // indirect
1515
github.com/smartystreets/goconvey v1.6.4 // indirect
1616
github.com/spf13/cobra v0.0.5
1717
github.com/spf13/viper v1.3.2
18-
github.com/yottab/proto-api v3.1.0+incompatible
18+
github.com/yottab/proto-api v3.1.1+incompatible
1919
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586
2020
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e // indirect
2121
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect

0 commit comments

Comments
 (0)