Skip to content

Commit

Permalink
add dot-env command, extra values
Browse files Browse the repository at this point in the history
  • Loading branch information
hhio618 committed Jul 4, 2020
1 parent 955af0e commit a9630ec
Show file tree
Hide file tree
Showing 30 changed files with 23,964 additions and 14,564 deletions.
30 changes: 28 additions & 2 deletions cmd/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"log"
"time"

"github.com/spf13/cobra"
ybApi "github.com/yottab/proto-api/proto"
Expand All @@ -14,6 +15,7 @@ var (
minScale uint64
flagVarImage string
flagVarEndpointType string
flagVarExtra map[string]string
)

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

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

//Fill other values
for key, val := range otherValues {
req.Values[key] = val
}

client := grpcConnect()
defer client.Close()
return client.V2().AppCreate(client.Context(), req)
Expand All @@ -139,7 +148,8 @@ func appCreate(cmd *cobra.Command, args []string) {
cmd.Flag("plan").Value.String(),
flagVarEndpointType,
flagVarPort,
flagVarMinScale)
flagVarMinScale,
flagVarExtra)
uiCheckErr("Could not Create the Application", err)
uiApplicationStatus(res)
}
Expand Down Expand Up @@ -173,6 +183,13 @@ func appUpdate(cmd *cobra.Command, args []string) {
client := grpcConnect()
defer client.Close()

//Fill other values
for key, val := range flagVarExtra {
if val != "" {
req.Values[key] = val
}
}

res, err := client.V2().AppConfigSet(client.Context(), req)
uiCheckErr("Could not Set the Config for Application", err)
uiApplicationStatus(res)
Expand All @@ -198,6 +215,15 @@ func appAddEnvironmentVariable(cmd *cobra.Command, args []string) {
uiApplicationStatus(res)
}

func appAddEnvironmentFromDotEnv(cmd *cobra.Command, args []string) {
res, err := ApplicationAddEnvironmentVariable(
getCliRequiredArg(args, 0),
parseDotEnvFile(flagFile))

uiCheckErr("Could not Add the Environment Variable for Application (Dot env file)", err)
uiApplicationStatus(res)
}

func appRemoveEnvironmentVariable(cmd *cobra.Command, args []string) {
req := new(ybApi.UnsetReq)
req.Name = getCliRequiredArg(args, 0)
Expand Down
12 changes: 9 additions & 3 deletions cmd/command_create_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ var (
$: yb create application \
--image="hub.yottab.io/test/dotnetcore:aspnetapp" \
--name="myaspnetapp" \
--port=80
--port=80 \
--extra "key1=val1,key2=va..."
$: yb create application \
Expand All @@ -32,6 +33,7 @@ var (
--min-scale=4 \
--debug=true \
--port=8080 \
--extra "key1=val1,key2=va..." \
--endpoint-type=private`}

srvCreateCmd = &cobra.Command{
Expand Down Expand Up @@ -89,12 +91,14 @@ var (
Example: `
## The Application Version is updated
$: yb update myadmin \
--image="hub.yottab.io/test/myadmin:v1.2.3"
--image="hub.yottab.io/test/myadmin:v1.2.3" \
--extra "debug=false,..."
## The Application Scale is updated
$: yb update myadmin \
--min-scale=4`}
--min-scale=4 \
--extra "debug=false,..."`}
)

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

rootCmd.AddCommand(
Expand Down
26 changes: 21 additions & 5 deletions cmd/command_enviroment.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@ import (
)

var (
commentShort = "environment variables [Set/Unset] for an Application"
commentSetLong = `Add list of environment variables to an Application.`
commentUnsetLong = `Remove list of environment variables for an Application.`
commentExample = `
commentShort = "environment variables [Set/Unset] for an Application"
commentSetLong = `Add list of environment variables to an Application.`
commentDotEnvLong = `Add list of environment variables in a dotenv file to an Application.`
commentUnsetLong = `Remove list of environment variables for an Application.`
commentExample = `
$: yb environment unset my-admin
-v="key1"
-v="key2"
$: yb environment set my-admin
-v="key1=value1"
-v="key2=value2"`
-v="key2=value2"
$: yb environment from-dotenv my-admin path/to/.env
`
)

var (
Expand All @@ -35,6 +39,13 @@ var (
Long: commentSetLong,
Example: commentExample}

dotEnvCmd = &cobra.Command{
Use: "from-dotenv [app.name]",
Run: appAddEnvironmentFromDotEnv,
Short: commentShort,
Long: commentDotEnvLong,
Example: commentExample}

unsetEnvCmd = &cobra.Command{
Use: "unset [app.name]",
Run: appRemoveEnvironmentVariable,
Expand All @@ -48,6 +59,10 @@ func init() {
setEnvCmd.Flags().StringArrayVarP(&flagVariableArray, "variable", "v", nil, "Environment Variable of application")
setEnvCmd.MarkFlagRequired("variable")

// app dot env flag:
dotEnvCmd.Flags().StringVarP(&flagFile, "file", "f", "", "Dot env file")
dotEnvCmd.MarkFlagRequired("file")

// app unset flag:
unsetEnvCmd.Flags().StringArrayVarP(&flagVariableArray, "variable", "v", nil, "Environment Variable of application")
unsetEnvCmd.MarkFlagRequired("variable")
Expand All @@ -56,5 +71,6 @@ func init() {
rootCmd.AddCommand(envCmd)
envCmd.AddCommand(
setEnvCmd,
dotEnvCmd,
unsetEnvCmd)
}
13 changes: 12 additions & 1 deletion cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"syscall"
"time"

"github.com/joho/godotenv"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/yottab/cli/config"
Expand All @@ -25,8 +26,18 @@ var (
flagVariableArray = make([]string, 0, 8)
flagIndex int32
flagAppName string
flagFile string
clientTimeout time.Duration = 10 * time.Second
)

func parseDotEnvFile(filename string) map[string]string {
var myEnv map[string]string
myEnv, err := godotenv.Read(filename)
if err != nil {
panic(err)
}
return myEnv
}
func arrayFlagToMap(flags []string) map[string]string {
varMap := make(map[string]string, len(flags))
for _, v := range flags {
Expand Down Expand Up @@ -85,7 +96,7 @@ func grpcConnect() ybApi.Client {
return viper.GetString(config.KEY_TOKEN)
}, func() string {
return version
}, nil))
}, nil), clientTimeout)
}

func toTime(t *ybApi.Timestamp) (out string) {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ require (
github.com/fatih/color v1.9.0
github.com/go-ini/ini v1.51.1 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.14.6 // indirect
github.com/joho/godotenv v1.3.0
github.com/minio/minio-go v6.0.14+incompatible
github.com/mitchellh/go-homedir v1.1.0
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
github.com/rhysd/go-github-selfupdate v1.2.1
github.com/sirupsen/logrus v1.4.2 // indirect
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.3.2
github.com/yottab/proto-api v3.1.0+incompatible
github.com/yottab/proto-api v3.1.1+incompatible
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e // indirect
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect
Expand Down
Loading

0 comments on commit a9630ec

Please sign in to comment.