Skip to content

Commit c36eae5

Browse files
authored
Merge pull request #5 from yoanbernabeu/v0.1.3
V0.1.3 - New commands and installation simplification
2 parents 8ffac62 + f1dfbbe commit c36eae5

31 files changed

+412
-53
lines changed

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ An Alternative CLI for Jelastic in Go
44

55
## Why rewrite the CLI in Go?
66

7-
The official Jelastic command-line interface (CLI) is written in Java, which can make it heavy and inconvenient to use in continuous integration/continuous deployment (CI/CD) environments.
7+
The [official Jelastic command-line interface (CLI)](https://www.virtuozzo.com/application-platform-docs/cli/) is written in Java, which can make it heavy and inconvenient to use in continuous integration/continuous deployment (CI/CD) environments.
88

99
Rewriting the CLI in Go will make it lighter and more portable, allowing it to be easily incorporated into CI/CD pipelines and used on a variety of systems without the need for additional dependencies.
1010

@@ -16,13 +16,16 @@ This will make it easier for developers to manage and deploy applications on Jel
1616

1717
#### From binary
1818

19-
* Debian/Ubuntu
19+
* Linux/Darwin
2020

2121
```bash
22-
wget https://github.com/yoanbernabeu/GoJelastic/releases/download/v0.1.2/GoJelastic-0.1.2-linux-amd64.tar.gz
23-
tar -xvzf GoJelastic-0.1.2-linux-amd64.tar.gz
24-
sudo mv GoJelastic /usr/local/bin/
25-
sudo chmod +x /usr/local/bin/GoJelastic
22+
# With wget
23+
wget -qO- https://raw.githubusercontent.com/yoanbernabeu/GoJelastic/main/install.sh | bash
24+
```
25+
26+
```bash
27+
# With curl
28+
curl -sL https://raw.githubusercontent.com/yoanbernabeu/GoJelastic/main/install.sh | bash
2629
```
2730

2831
* Other Operating Systems

cmd/jelastic_billing_account.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,19 @@ func init() {
1313

1414
rootCmd.AddGroup(&cobra.Group{ID: "Billing/Account", Title: "Billing/Account"})
1515

16+
rootCmd.AddCommand(getExtendedAccountBillingHistoryByPeriodCmd)
17+
getExtendedAccountBillingHistoryByPeriodCmd.Flags().String("appid", "", "An appid is required")
18+
getExtendedAccountBillingHistoryByPeriodCmd.MarkFlagRequired("appid")
19+
getExtendedAccountBillingHistoryByPeriodCmd.Flags().String("startTime", "", "A startTime is required")
20+
getExtendedAccountBillingHistoryByPeriodCmd.MarkFlagRequired("startTime")
21+
getExtendedAccountBillingHistoryByPeriodCmd.Flags().String("endTime", "", "A endTime is required")
22+
getExtendedAccountBillingHistoryByPeriodCmd.MarkFlagRequired("endTime")
1623
}
1724

1825
var getAccountCmd = &cobra.Command{
1926
Use: "getAccount",
20-
Short: "Gets account by session.",
21-
Long: "Gets account by session.",
27+
Short: "Gets account by session",
28+
Long: "Gets account by session",
2229
GroupID: "Billing/Account",
2330
Run: func(cmd *cobra.Command, args []string) {
2431
token, _ := cmd.Flags().GetString("token")
@@ -31,3 +38,22 @@ var getAccountCmd = &cobra.Command{
3138
fmt.Println(response)
3239
},
3340
}
41+
42+
var getExtendedAccountBillingHistoryByPeriodCmd = &cobra.Command{
43+
Use: "getExtendedAccountBillingHistoryByPeriod",
44+
Short: "Gets extended account billing history by period",
45+
Long: "Gets extended account billing history by period",
46+
GroupID: "Billing/Account",
47+
Run: func(cmd *cobra.Command, args []string) {
48+
token, _ := cmd.Flags().GetString("token")
49+
url, _ := cmd.Flags().GetString("url")
50+
appid, _ := cmd.Flags().GetString("appid")
51+
starttime, _ := cmd.Flags().GetString("startTime")
52+
endtime, _ := cmd.Flags().GetString("endTime")
53+
54+
finalUrl := url + "/billing/account/rest/getextendedaccountbillinghistorybyperiod" + "?session=" + token + "&appid=" + appid + "&startTime=" + starttime + "&endtime=" + endtime
55+
56+
response := makeRequest(finalUrl, "GET", "")
57+
fmt.Println(response)
58+
},
59+
}

cmd/jelastic_environment_control.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ func init() {
4444

4545
var getEnvsCmd = &cobra.Command{
4646
Use: "getEnvs",
47-
Short: "Gets the information about all environments of a user.",
48-
Long: "Gets the information about all environments of a user.",
47+
Short: "Gets the information about all environments of a user",
48+
Long: "Gets the information about all environments of a user",
4949
GroupID: "Environment/Control",
5050
Run: func(cmd *cobra.Command, args []string) {
5151
token, _ := cmd.Flags().GetString("token")
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
func init() {
10+
rootCmd.AddCommand(getRulesCmd)
11+
getRulesCmd.Flags().String("appid", "", "An appid is required")
12+
getRulesCmd.MarkFlagRequired("appid")
13+
14+
rootCmd.AddGroup(&cobra.Group{ID: "Environment/Security", Title: "Environment/Security"})
15+
}
16+
17+
var getRulesCmd = &cobra.Command{
18+
Use: "getRules",
19+
Short: "Provides information about firewall rules for the environment",
20+
Long: "Provides information about firewall rules for the environment",
21+
GroupID: "Environment/Security",
22+
Run: func(cmd *cobra.Command, args []string) {
23+
token, _ := cmd.Flags().GetString("token")
24+
url, _ := cmd.Flags().GetString("url")
25+
appid, _ := cmd.Flags().GetString("appid")
26+
27+
finalUrl := url + "/environment/security/rest/getrules" + "?session=" + token + "&envName=" + appid
28+
29+
response := makeRequest(finalUrl, "GET", "")
30+
fmt.Println(response)
31+
},
32+
}

cmd/jelastic_users_account.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
func init() {
10+
rootCmd.AddCommand(getUserInfoCmd)
11+
rootCmd.AddCommand(recoverPasswordCmd)
12+
recoverPasswordCmd.Flags().String("email", "", "An email is required")
13+
recoverPasswordCmd.MarkFlagRequired("email")
14+
15+
rootCmd.AddGroup(&cobra.Group{ID: "Users/Account", Title: "Users/Account"})
16+
}
17+
18+
var getUserInfoCmd = &cobra.Command{
19+
Use: "getUserInfo",
20+
Short: "Gets information about the user.",
21+
Long: "Gets information about the user.",
22+
GroupID: "Users/Account",
23+
Run: func(cmd *cobra.Command, args []string) {
24+
token, _ := cmd.Flags().GetString("token")
25+
url, _ := cmd.Flags().GetString("url")
26+
27+
finalUrl := url + "/users/account/rest/getuserinfo" + "?session=" + token
28+
29+
response := makeRequest(finalUrl, "GET", "")
30+
fmt.Println(response)
31+
},
32+
}
33+
34+
var recoverPasswordCmd = &cobra.Command{
35+
Use: "recoverPassword",
36+
Short: "Sends an email with the link to reset the account password",
37+
Long: "Sends an email with the link to reset the account password",
38+
GroupID: "Users/Account",
39+
Run: func(cmd *cobra.Command, args []string) {
40+
url, _ := cmd.Flags().GetString("url")
41+
email, _ := cmd.Flags().GetString("email")
42+
43+
finalUrl := url + "/users/account/rest/recoverpassword" + "?email=" + email
44+
45+
response := makeRequest(finalUrl, "GET", "")
46+
fmt.Println(response)
47+
},
48+
}

cmd/root.go

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ Copyright © 2022 Yoan BERNABEU <yoan.bernabeu@gmail.com>
55
package cmd
66

77
import (
8+
"encoding/json"
89
"fmt"
9-
"io"
1010
"net/http"
1111
"os"
1212

@@ -81,31 +81,51 @@ func writeConfig(url string, token string) {
8181
fmt.Println("Config file written or updated")
8282
}
8383

84-
//makeRequest makes a request to the Jelastic API
85-
var makeRequest = func(url string, method string, body string) string {
86-
84+
func makeHTTPRequest(url string, method string, body string) (*http.Response, error) {
8785
httpClient := &http.Client{}
8886
req, err := http.NewRequest(method, url, nil)
89-
9087
if err != nil {
91-
panic(err)
88+
return nil, err
9289
}
9390

9491
resp, err := httpClient.Do(req)
95-
9692
if err != nil {
97-
panic(err)
93+
return nil, err
9894
}
9995

96+
return resp, nil
97+
}
98+
99+
func formatResponse(resp *http.Response) (string, error) {
100100
defer resp.Body.Close()
101101

102-
bodyBytes, err := io.ReadAll(resp.Body)
102+
var data interface{}
103+
err := json.NewDecoder(resp.Body).Decode(&data)
104+
if err != nil {
105+
return "", err
106+
}
107+
108+
jsonBytes, err := json.MarshalIndent(data, "", " ")
109+
if err != nil {
110+
return "", err
111+
}
103112

113+
return string(jsonBytes), nil
114+
}
115+
116+
//makeRequest makes a request to the Jelastic API
117+
func makeRequest(url string, method string, body string) string {
118+
resp, err := makeHTTPRequest(url, method, body)
104119
if err != nil {
105-
panic(err)
120+
fmt.Println(err)
121+
os.Exit(1)
106122
}
107123

108-
bodyString := string(bodyBytes)
124+
formattedResponse, err := formatResponse(resp)
125+
if err != nil {
126+
fmt.Println(err)
127+
os.Exit(1)
128+
}
109129

110-
return bodyString
130+
return formattedResponse
111131
}

docs/documentation/GoJelastic.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,20 @@ An Alternative and not official CLI for Jelastic
2020
* [GoJelastic completion](GoJelastic_completion.md) - Generate the autocompletion script for the specified shell
2121
* [GoJelastic configure](GoJelastic_configure.md) - Configure your CLI
2222
* [GoJelastic documentation](GoJelastic_documentation.md) - Generate documentation
23-
* [GoJelastic getAccount](GoJelastic_getAccount.md) - Gets account by session.
23+
* [GoJelastic getAccount](GoJelastic_getAccount.md) - Gets account by session
2424
* [GoJelastic getContainerEnvVars](GoJelastic_getContainerEnvVars.md) - Gets env vars of container
2525
* [GoJelastic getEnv](GoJelastic_getEnv.md) - Gets the full information about environment
26-
* [GoJelastic getEnvs](GoJelastic_getEnvs.md) - Gets the information about all environments of a user.
26+
* [GoJelastic getEnvs](GoJelastic_getEnvs.md) - Gets the information about all environments of a user
27+
* [GoJelastic getExtendedAccountBillingHistoryByPeriod](GoJelastic_getExtendedAccountBillingHistoryByPeriod.md) - Gets extended account billing history by period
2728
* [GoJelastic getGroups](GoJelastic_getGroups.md) - Get group list
2829
* [GoJelastic getList](GoJelastic_getList.md) - Get list of an environment
2930
* [GoJelastic getNodeGroups](GoJelastic_getNodeGroups.md) - Gets node group's data.
3031
* [GoJelastic getRegions](GoJelastic_getRegions.md) - Gets available regions for the user
32+
* [GoJelastic getRules](GoJelastic_getRules.md) - Provides information about firewall rules for the environment
33+
* [GoJelastic getUserInfo](GoJelastic_getUserInfo.md) - Gets information about the user.
34+
* [GoJelastic recoverPassword](GoJelastic_recoverPassword.md) - Sends an email with the link to reset the account password
3135
* [GoJelastic redeployContainerById](GoJelastic_redeployContainerById.md) - Redeploy a container by id
3236
* [GoJelastic startEnv](GoJelastic_startEnv.md) - Start one environment
3337
* [GoJelastic stopEnv](GoJelastic_stopEnv.md) - Stop one environment
3438

35-
###### Auto generated by spf13/cobra on 24-Dec-2022
39+
###### Auto generated by spf13/cobra on 30-Dec-2022

docs/documentation/GoJelastic_completion.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ See each sub-command's help for details on how to use the generated script.
2929
* [GoJelastic completion powershell](GoJelastic_completion_powershell.md) - Generate the autocompletion script for powershell
3030
* [GoJelastic completion zsh](GoJelastic_completion_zsh.md) - Generate the autocompletion script for zsh
3131

32-
###### Auto generated by spf13/cobra on 24-Dec-2022
32+
###### Auto generated by spf13/cobra on 30-Dec-2022

docs/documentation/GoJelastic_completion_bash.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@ GoJelastic completion bash
4848

4949
* [GoJelastic completion](GoJelastic_completion.md) - Generate the autocompletion script for the specified shell
5050

51-
###### Auto generated by spf13/cobra on 24-Dec-2022
51+
###### Auto generated by spf13/cobra on 30-Dec-2022

docs/documentation/GoJelastic_completion_fish.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ GoJelastic completion fish [flags]
3939

4040
* [GoJelastic completion](GoJelastic_completion.md) - Generate the autocompletion script for the specified shell
4141

42-
###### Auto generated by spf13/cobra on 24-Dec-2022
42+
###### Auto generated by spf13/cobra on 30-Dec-2022

0 commit comments

Comments
 (0)