Skip to content

Commit 6f5fbcc

Browse files
committed
📦 NEW: Configure with file
1 parent 99fcbbc commit 6f5fbcc

File tree

7 files changed

+568
-32
lines changed

7 files changed

+568
-32
lines changed

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
*.json
2+
.env

‎README.md‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,44 +48,53 @@ go build -o GoJelastic
4848
* URL: Your Jelastic API URL
4949
* NODEID: The unique ID of the node/container
5050

51+
### Configure Environment into a file
52+
53+
If you want to use the CLI without passing the token and the URL each time, you can configure it into a file (stored in your home directory in a file named `gojelastic.env`)
54+
55+
56+
```bash
57+
GoJelastic configure --token <TOKEN> --url <URL>
58+
```
59+
5160
### Get Environment Info
5261

5362
Get informations about one environment
5463

5564
```bash
56-
./GoJelastic getEnv --appid <APPID> --token <TOKEN> --url <URL>
65+
GoJelastic getEnv --appid <APPID>
5766
```
5867

5968
### Get Environments Info
6069

6170
Get informations about all environments
6271

6372
```bash
64-
./GoJelastic getEnvs --token <TOKEN> --url <URL>
73+
GoJelastic getEnvs
6574
```
6675

6776
### Start Environment
6877

6978
Start an environment
7079

7180
```bash
72-
./GoJelastic startEnv --appid <APPID> --token <TOKEN> --url <URL>
81+
GoJelastic startEnv --appid <APPID>
7382
```
7483

7584
### Stop Environment
7685

7786
Stop an environment
7887

7988
```bash
80-
./GoJelastic stopEnv --appid <APPID> --token <TOKEN> --url <URL>
89+
GoJelastic stopEnv --appid <APPID>
8190
```
8291

8392
### Redeploy Container by ID
8493

8594
Redeploy a container by ID and target tag
8695

8796
```bash
88-
./GoJelastic redeployEnv --nodeid <NODEID> --tag <TAG> --appid <APPID> --token <TOKEN> --url <URL>
97+
GoJelastic redeployEnv --nodeid <NODEID> --tag <TAG> --appid <APPID>
8998
```
9099

91100
## License

‎cmd/configure.go‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
func init() {
8+
rootCmd.AddCommand(configure)
9+
configure.Flags().String("token", "", "Your Jelastic token")
10+
configure.Flags().String("url", "", "Your Jelastic url")
11+
configure.MarkFlagRequired("token")
12+
configure.MarkFlagRequired("url")
13+
}
14+
15+
var configure = &cobra.Command{
16+
Use: "configure",
17+
Short: "Configure your Jelastic account",
18+
Long: "Configure your Jelastic account",
19+
Run: func(cmd *cobra.Command, args []string) {
20+
writeConfig(cmd.Flags().Lookup("url").Value.String(), cmd.Flags().Lookup("token").Value.String())
21+
},
22+
}

‎cmd/jelastic.go‎

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,25 @@ import (
1010

1111
func init() {
1212
rootCmd.AddCommand(getEnvsCmd)
13-
getEnvsCmd.Flags().String("token", "", "A token is required")
14-
getEnvsCmd.Flags().String("url", "", "A url is required")
15-
getEnvsCmd.MarkFlagRequired("token")
16-
getEnvsCmd.MarkFlagRequired("url")
1713

1814
rootCmd.AddCommand(getEnvCmd)
19-
getEnvCmd.Flags().String("token", "", "A token is required")
20-
getEnvCmd.Flags().String("url", "", "A url is required")
2115
getEnvCmd.Flags().String("appid", "", "An appid is required")
2216
getEnvCmd.MarkFlagRequired("appid")
23-
getEnvCmd.MarkFlagRequired("token")
24-
getEnvCmd.MarkFlagRequired("url")
2517

2618
rootCmd.AddCommand(startEnvCmd)
27-
startEnvCmd.Flags().String("token", "", "A token is required")
28-
startEnvCmd.Flags().String("url", "", "A url is required")
2919
startEnvCmd.Flags().String("appid", "", "An appid is required")
3020
startEnvCmd.MarkFlagRequired("appid")
31-
startEnvCmd.MarkFlagRequired("token")
32-
startEnvCmd.MarkFlagRequired("url")
3321

3422
rootCmd.AddCommand(stopEnvCmd)
35-
stopEnvCmd.Flags().String("token", "", "A token is required")
36-
stopEnvCmd.Flags().String("url", "", "A url is required")
3723
stopEnvCmd.Flags().String("appid", "", "An appid is required")
3824
stopEnvCmd.MarkFlagRequired("appid")
39-
stopEnvCmd.MarkFlagRequired("token")
40-
stopEnvCmd.MarkFlagRequired("url")
4125

4226
rootCmd.AddCommand(redeployContainerByIdCmd)
43-
redeployContainerByIdCmd.Flags().String("token", "", "A token is required")
44-
redeployContainerByIdCmd.Flags().String("url", "", "A url is required")
4527
redeployContainerByIdCmd.Flags().String("nodeid", "", "An nodeid is required")
4628
redeployContainerByIdCmd.Flags().String("tag", "", "An tag is required")
4729
redeployContainerByIdCmd.Flags().String("appid", "", "An appid is required")
4830
redeployContainerByIdCmd.MarkFlagRequired("nodeid")
4931
redeployContainerByIdCmd.MarkFlagRequired("tag")
50-
redeployContainerByIdCmd.MarkFlagRequired("token")
51-
redeployContainerByIdCmd.MarkFlagRequired("url")
5232
redeployContainerByIdCmd.MarkFlagRequired("appid")
5333
}
5434

‎cmd/root.go‎

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
/*
2-
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
2+
Copyright © 2022 Yoan BERNABEU <yoan.bernabeu@gmail.com>
33
44
*/
55
package cmd
66

77
import (
8+
"fmt"
89
"os"
910

1011
"github.com/spf13/cobra"
12+
"github.com/spf13/viper"
1113
)
1214

1315
// rootCmd represents the base command when called without any subcommands
@@ -30,13 +32,54 @@ func Execute() {
3032
}
3133

3234
func init() {
33-
// Here you will define your flags and configuration settings.
34-
// Cobra supports persistent flags, which, if defined here,
35-
// will be global for your application.
35+
config := initConfig()
3636

37-
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.GoCronify.yaml)")
37+
if config["token"] != nil {
38+
rootCmd.PersistentFlags().String("token", config["token"].(string), "A token is required")
39+
} else {
40+
rootCmd.PersistentFlags().String("token", "", "A token is required")
41+
rootCmd.MarkPersistentFlagRequired("token")
42+
}
43+
44+
if config["url"] != nil {
45+
rootCmd.PersistentFlags().String("url", config["url"].(string), "A url is required")
46+
} else {
47+
rootCmd.PersistentFlags().String("url", "", "A url is required")
48+
rootCmd.MarkPersistentFlagRequired("url")
49+
}
3850

39-
// Cobra also supports local flags, which will only run
40-
// when this action is called directly.
4151
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
4252
}
53+
54+
// initConfig reads in config file and ENV variables if set.
55+
func initConfig() map[string]interface{} {
56+
// Don't forget to read config either from cfgFile or from home directory!
57+
viper.SetConfigType("env")
58+
viper.SetConfigName("gojelastic.env") // name of config file (without extension)
59+
viper.AddConfigPath("$HOME") // adding home directory as first search path
60+
viper.AutomaticEnv() // read in environment variables that match
61+
62+
// If a config file is found, read it in.
63+
if err := viper.ReadInConfig(); err == nil {
64+
fmt.Println("Using config file:", viper.ConfigFileUsed())
65+
} else {
66+
fmt.Println("No config file found")
67+
}
68+
69+
return viper.AllSettings()
70+
}
71+
72+
//writeConfig writes config file and ENV variables if set.
73+
func writeConfig(url string, token string) {
74+
viper.SetConfigType("env")
75+
viper.SetConfigName("gojelastic") // name of config file (without extension)
76+
viper.AddConfigPath("$HOME") // adding home directory as first search path
77+
viper.AutomaticEnv() // read in environment variables that match
78+
79+
viper.Set("url", url)
80+
viper.Set("token", token)
81+
82+
viper.WriteConfig()
83+
84+
fmt.Println("Config file written or updated")
85+
}

‎go.mod‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@ go 1.18
55
require github.com/spf13/cobra v1.6.1
66

77
require (
8+
github.com/fsnotify/fsnotify v1.6.0 // indirect
9+
github.com/hashicorp/hcl v1.0.0 // indirect
810
github.com/inconshreveable/mousetrap v1.0.1 // indirect
11+
github.com/magiconair/properties v1.8.6 // indirect
12+
github.com/mitchellh/mapstructure v1.5.0 // indirect
13+
github.com/pelletier/go-toml v1.9.5 // indirect
14+
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
15+
github.com/spf13/afero v1.9.2 // indirect
16+
github.com/spf13/cast v1.5.0 // indirect
17+
github.com/spf13/jwalterweatherman v1.1.0 // indirect
918
github.com/spf13/pflag v1.0.5 // indirect
19+
github.com/spf13/viper v1.14.0 // indirect
20+
github.com/subosito/gotenv v1.4.1 // indirect
21+
golang.org/x/sys v0.0.0-20220908164124-27713097b956 // indirect
22+
golang.org/x/text v0.4.0 // indirect
23+
gopkg.in/ini.v1 v1.67.0 // indirect
24+
gopkg.in/yaml.v2 v2.4.0 // indirect
25+
gopkg.in/yaml.v3 v3.0.1 // indirect
1026
)

0 commit comments

Comments
 (0)