Skip to content

Commit bd5b1b2

Browse files
committed
Linting, add new PHP selector plugin selection support
1 parent 8978ab9 commit bd5b1b2

32 files changed

+516
-357
lines changed

README.md

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ package main
2020

2121
import (
2222
"time"
23-
23+
2424
"github.com/levelzerotechnology/directadmin-go"
2525
)
2626

@@ -42,6 +42,7 @@ func main() {
4242

4343
userCtx.User.Usage = *usage
4444
}
45+
4546
```
4647

4748
From here, you can call user functions via `userCtx`.
@@ -51,22 +52,14 @@ For example, if you wanted to print each of your databases to your terminal:
5152
```go
5253
dbs, err := userCtx.GetDatabases()
5354
if err != nil {
54-
log.Fatalln(err)
55+
log.Fatalln(err)
5556
}
5657

5758
for _, db := range dbs {
58-
fmt.Println(db.Name)
59+
fmt.Println(db.Name)
5960
}
6061
```
6162

62-
## Roadmap
63-
64-
- [ ] Cleanup repo structure (e.g. redis actions being within `admin.go` could go into a dedicated `redis.go` file
65-
perhaps)
66-
- [ ] Explore DA's new API's update versions of old functions (e.g. user config/usage)
67-
- [ ] Implement testing for all functions
68-
- [ ] Reach stable v1.0
69-
7063
## License
7164

7265
BSD licensed. See the [LICENSE](LICENSE) file for details.

admin.go

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package directadmin
22

3-
import "net/http"
3+
import (
4+
"errors"
5+
"net/http"
6+
)
47

58
type (
6-
// Admin inherits Reseller which inherits User
9+
// Admin inherits Reseller which inherits User.
710
Admin struct {
811
Reseller
912
}
@@ -18,6 +21,7 @@ type (
1821
}
1922
)
2023

24+
// ConvertResellerToUser (admin) converts the given reseller to a user account.
2125
func (c *AdminContext) ConvertResellerToUser(username string, reseller string) error {
2226
if _, err := c.makeRequestNew(http.MethodPost, "convert-reseller-to-user", convertAccount{Account: username, Creator: reseller}, nil); err != nil {
2327
return err
@@ -26,6 +30,7 @@ func (c *AdminContext) ConvertResellerToUser(username string, reseller string) e
2630
return nil
2731
}
2832

33+
// ConvertUserToReseller (admin) converts the given user account to a reseller account.
2934
func (c *AdminContext) ConvertUserToReseller(username string) error {
3035
if _, err := c.makeRequestNew(http.MethodPost, "convert-user-to-reseller", convertAccount{Account: username}, nil); err != nil {
3136
return err
@@ -34,6 +39,7 @@ func (c *AdminContext) ConvertUserToReseller(username string) error {
3439
return nil
3540
}
3641

42+
// DisableRedis (admin) disables Redis for the server.
3743
func (c *AdminContext) DisableRedis() error {
3844
var response apiGenericResponseNew
3945

@@ -44,6 +50,7 @@ func (c *AdminContext) DisableRedis() error {
4450
return nil
4551
}
4652

53+
// EnableRedis (admin) enables Redis for the server.
4754
func (c *AdminContext) EnableRedis() error {
4855
var response apiGenericResponseNew
4956

@@ -54,7 +61,7 @@ func (c *AdminContext) EnableRedis() error {
5461
return nil
5562
}
5663

57-
// GetAllUsers (admin) returns an array of all users
64+
// GetAllUsers (admin) returns an array of all users.
5865
func (c *AdminContext) GetAllUsers() ([]string, error) {
5966
var users []string
6067

@@ -65,7 +72,21 @@ func (c *AdminContext) GetAllUsers() ([]string, error) {
6572
return users, nil
6673
}
6774

68-
// GetResellers (admin) returns an array of all resellers
75+
// GetRedisStatus (admin) returns whether Redis is enabled and it's version.
76+
func (c *AdminContext) GetRedisStatus() (bool, string, error) {
77+
var resp struct {
78+
Active bool `json:"active"`
79+
Version string `json:"version"`
80+
}
81+
82+
if _, err := c.makeRequestNew(http.MethodGet, "redis/status", nil, &resp); err != nil {
83+
return false, "", err
84+
}
85+
86+
return resp.Active, resp.Version, nil
87+
}
88+
89+
// GetResellers (admin) returns an array of all resellers.
6990
func (c *AdminContext) GetResellers() ([]string, error) {
7091
var users []string
7192

@@ -76,6 +97,7 @@ func (c *AdminContext) GetResellers() ([]string, error) {
7697
return users, nil
7798
}
7899

100+
// MoveUserToReseller (admin) moves the given user to the given reseller.
79101
func (c *AdminContext) MoveUserToReseller(username string, reseller string) error {
80102
if _, err := c.makeRequestNew(http.MethodPost, "change-user-creator", convertAccount{Account: username, Creator: reseller}, nil); err != nil {
81103
return err
@@ -84,6 +106,7 @@ func (c *AdminContext) MoveUserToReseller(username string, reseller string) erro
84106
return nil
85107
}
86108

109+
// RestartDirectAdmin (admin) restarts the DirectAdmin process on the server.
87110
func (c *AdminContext) RestartDirectAdmin() error {
88111
var response apiGenericResponseNew
89112

@@ -94,6 +117,7 @@ func (c *AdminContext) RestartDirectAdmin() error {
94117
return nil
95118
}
96119

120+
// UpdateDirectAdmin (admin) initiates a DirectAdmin update on the server.
97121
func (c *AdminContext) UpdateDirectAdmin() error {
98122
var response apiGenericResponseNew
99123

@@ -103,3 +127,16 @@ func (c *AdminContext) UpdateDirectAdmin() error {
103127

104128
return nil
105129
}
130+
131+
// UpdateHostname (admin) updates the server's hostname.
132+
func (c *AdminContext) UpdateHostname(hostname string) error {
133+
if hostname == "" {
134+
return errors.New("missing hostname")
135+
}
136+
137+
if _, err := c.makeRequestNew(http.MethodPost, "server-settings/change-hostname", map[string]string{"hostname": hostname}, nil); err != nil {
138+
return err
139+
}
140+
141+
return nil
142+
}

auth.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type (
2626
Created time.Time `json:"created"`
2727
CreatedBy string `json:"createdBy"`
2828
Expires time.Time `json:"expires"`
29-
Id string `json:"id"`
29+
ID string `json:"id"`
3030
RedirectURL string `json:"redirectURL"`
3131
URL string `json:"url"`
3232
}
@@ -52,7 +52,7 @@ func (c *AdminContext) GetLoginHistory() ([]*LoginHistory, error) {
5252
}
5353

5454
if len(loginHistory) == 0 {
55-
return nil, fmt.Errorf("no login history found")
55+
return nil, errors.New("no login history found")
5656
}
5757

5858
return loginHistory, nil
@@ -69,7 +69,7 @@ func (c *UserContext) GetLoginURLs() ([]*LoginKeyURL, error) {
6969
}
7070

7171
// GetMyUsername returns the current user's username. This is particularly useful when logging in as another user, as it
72-
// trims the admin/reseller username automatically
72+
// trims the admin/reseller username automatically.
7373
func (c *UserContext) GetMyUsername() string {
7474
// If the user is logged in via reseller, we need to remove the reseller username from the context's username.
7575
if strings.Contains(c.credentials.username, "|") {
@@ -79,6 +79,7 @@ func (c *UserContext) GetMyUsername() string {
7979
return c.credentials.username
8080
}
8181

82+
// Login checks whether the configured credentials work against the configured API.
8283
func (c *UserContext) Login() error {
8384
var response apiGenericResponse
8485

@@ -94,7 +95,7 @@ func (c *UserContext) Login() error {
9495
}
9596

9697
// LoginAsAdmin verifies the provided credentials against the DA API, then returns an admin-level context.
97-
// The passkey can either be the user's password, or a login key
98+
// The passkey can either be the user's password, or a login key.
9899
func (a *API) LoginAsAdmin(username string, passkey string) (*AdminContext, error) {
99100
userCtx, err := a.login(username, passkey)
100101
if err != nil {
@@ -115,7 +116,7 @@ func (a *API) LoginAsAdmin(username string, passkey string) (*AdminContext, erro
115116
}
116117

117118
// LoginAsReseller verifies the provided credentials against the DA API, then returns a reseller-level context.
118-
// The passkey can either be the user's password, or a login key
119+
// The passkey can either be the user's password, or a login key.
119120
func (a *API) LoginAsReseller(username string, passkey string) (*ResellerContext, error) {
120121
userCtx, err := a.login(username, passkey)
121122
if err != nil {
@@ -134,7 +135,7 @@ func (a *API) LoginAsReseller(username string, passkey string) (*ResellerContext
134135
}
135136

136137
// LoginAsUser verifies the provided credentials against the DA API, then returns a user-level context.
137-
// The passkey can either be the user's password, or a login key
138+
// The passkey can either be the user's password, or a login key.
138139
func (a *API) LoginAsUser(username string, passkey string) (*UserContext, error) {
139140
userCtx, err := a.login(username, passkey)
140141
if err != nil {
@@ -148,14 +149,18 @@ func (a *API) LoginAsUser(username string, passkey string) (*UserContext, error)
148149
return userCtx, nil
149150
}
150151

152+
// LoginAsMyReseller logs the current admin into the given reseller's account.
151153
func (c *AdminContext) LoginAsMyReseller(username string) (*ResellerContext, error) {
152154
return c.api.LoginAsReseller(c.credentials.username+"|"+username, c.credentials.passkey)
153155
}
154156

157+
// LoginAsMyUser logs the current reseller into the given user's account.
155158
func (c *ResellerContext) LoginAsMyUser(username string) (*UserContext, error) {
156159
return c.api.LoginAsUser(c.credentials.username+"|"+username, c.credentials.passkey)
157160
}
158161

162+
// login sets up the user context's cookie jar, verifies that the credentials work against the API, and pulls the user's
163+
// config.
159164
func (a *API) login(username string, passkey string) (*UserContext, error) {
160165
jar, err := cookiejar.New(nil)
161166
if err != nil {

backup.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"net/url"
77
)
88

9-
// CreateBackup (user) creates an account backup for the given domain, and the given items
9+
// CreateBackup (user) creates an account backup for the given domain, and the given items.
1010
func (c *UserContext) CreateBackup(domain string, backupItems ...string) error {
1111
var response apiGenericResponse
1212

@@ -30,7 +30,7 @@ func (c *UserContext) CreateBackup(domain string, backupItems ...string) error {
3030
return nil
3131
}
3232

33-
// CreateBackupAllItems (user) wraps around CreateBackup and provides all available backup items
33+
// CreateBackupAllItems (user) wraps around CreateBackup and provides all available backup items.
3434
func (c *UserContext) CreateBackupAllItems(domain string) error {
3535
return c.CreateBackup(
3636
domain,
@@ -51,7 +51,7 @@ func (c *UserContext) CreateBackupAllItems(domain string) error {
5151
)
5252
}
5353

54-
// GetBackups (user) returns an array of the session user's backups for the given domain
54+
// GetBackups (user) returns an array of the session user's backups for the given domain.
5555
func (c *UserContext) GetBackups(domain string) ([]string, error) {
5656
var backups []string
5757

@@ -62,7 +62,7 @@ func (c *UserContext) GetBackups(domain string) ([]string, error) {
6262
return backups, nil
6363
}
6464

65-
// RestoreBackup (user) restores an account backup for the given domain, and the given items
65+
// RestoreBackup (user) restores an account backup for the given domain, and the given items.
6666
func (c *UserContext) RestoreBackup(domain string, backupFilename string, backupItems ...string) error {
6767
var response apiGenericResponse
6868

@@ -87,7 +87,7 @@ func (c *UserContext) RestoreBackup(domain string, backupFilename string, backup
8787
return nil
8888
}
8989

90-
// RestoreBackupAllItems (user) wraps around RestoreBackup and provides all available backup items
90+
// RestoreBackupAllItems (user) wraps around RestoreBackup and provides all available backup items.
9191
func (c *UserContext) RestoreBackupAllItems(domain string, backupFilename string) error {
9292
return c.RestoreBackup(
9393
domain,

0 commit comments

Comments
 (0)