🎉 Init sideload panel
This commit is contained in:
89
pkg/cmd/rdc/conn/commands.go
Normal file
89
pkg/cmd/rdc/conn/commands.go
Normal file
@ -0,0 +1,89 @@
|
||||
package conn
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/samber/lo"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
var CliCommands = []*cli.Command{
|
||||
{
|
||||
Name: "list",
|
||||
Aliases: []string{"ls"},
|
||||
Description: "List all connected remote server",
|
||||
Action: func(ctx *cli.Context) error {
|
||||
var servers []CliConnection
|
||||
raw, _ := json.Marshal(viper.Get("servers"))
|
||||
_ = json.Unmarshal(raw, &servers)
|
||||
|
||||
log.Info().Msgf("There are %d server(s) connected in total.", len(servers))
|
||||
for idx, server := range servers {
|
||||
log.Info().Msgf("%d) %s: %s", idx+1, server.ID, server.Url)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "connect",
|
||||
Aliases: []string{"add"},
|
||||
Description: "Connect and save configuration of remote server",
|
||||
ArgsUsage: "<id> <server url> <credential>",
|
||||
Action: func(ctx *cli.Context) error {
|
||||
if ctx.Args().Len() < 3 {
|
||||
return fmt.Errorf("must have three arguments: <id> <server url> <credential>")
|
||||
}
|
||||
|
||||
c := CliConnection{
|
||||
ID: ctx.Args().Get(0),
|
||||
Url: ctx.Args().Get(1),
|
||||
Credential: ctx.Args().Get(2),
|
||||
}
|
||||
|
||||
if err := c.CheckConnectivity(); err != nil {
|
||||
return fmt.Errorf("couldn't connect server: %s", err.Error())
|
||||
} else {
|
||||
var servers []CliConnection
|
||||
raw, _ := json.Marshal(viper.Get("servers"))
|
||||
_ = json.Unmarshal(raw, &servers)
|
||||
viper.Set("servers", append(servers, c))
|
||||
|
||||
if err := viper.WriteConfig(); err != nil {
|
||||
return err
|
||||
} else {
|
||||
log.Info().Msg("Successfully connected a new remote server, enter \"rds ls\" to get more info.")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "disconnect",
|
||||
Aliases: []string{"remove"},
|
||||
Description: "Remove a remote server configuration",
|
||||
ArgsUsage: "<id>",
|
||||
Action: func(ctx *cli.Context) error {
|
||||
if ctx.Args().Len() < 1 {
|
||||
return fmt.Errorf("must have more one arguments: <server url>")
|
||||
}
|
||||
|
||||
var servers []CliConnection
|
||||
raw, _ := json.Marshal(viper.Get("servers"))
|
||||
_ = json.Unmarshal(raw, &servers)
|
||||
viper.Set("servers", lo.Filter(servers, func(item CliConnection, idx int) bool {
|
||||
return item.ID != ctx.Args().Get(0)
|
||||
}))
|
||||
|
||||
if err := viper.WriteConfig(); err != nil {
|
||||
return err
|
||||
} else {
|
||||
log.Info().Msg("Successfully disconnected a remote server, enter \"rds ls\" to get more info.")
|
||||
return nil
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
42
pkg/cmd/rdc/conn/connect.go
Normal file
42
pkg/cmd/rdc/conn/connect.go
Normal file
@ -0,0 +1,42 @@
|
||||
package conn
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
roadsign "code.smartsheep.studio/goatworks/roadsign/pkg"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type CliConnection struct {
|
||||
ID string `json:"id"`
|
||||
Url string `json:"url"`
|
||||
Credential string `json:"credential"`
|
||||
}
|
||||
|
||||
func (v CliConnection) CheckConnectivity() error {
|
||||
client := fiber.Get(v.Url + "/cgi/metadata")
|
||||
client.BasicAuth("RoadSign CLI", v.Credential)
|
||||
|
||||
if status, data, err := client.Bytes(); len(err) > 0 {
|
||||
return fmt.Errorf("couldn't connect to server: %q", err)
|
||||
} else if status != 200 {
|
||||
return fmt.Errorf("server rejected request, may cause by invalid credential")
|
||||
} else {
|
||||
var resp fiber.Map
|
||||
if err := json.Unmarshal(data, &resp); err != nil {
|
||||
return err
|
||||
} else if resp["server"] != "RoadSign" {
|
||||
return fmt.Errorf("remote server isn't roadsign")
|
||||
} else if resp["version"] != roadsign.AppVersion {
|
||||
if strings.Contains(roadsign.AppVersion, "#") {
|
||||
return fmt.Errorf("remote server version mismatch client version, update or downgrade client required")
|
||||
} else {
|
||||
log.Warn().Msg("RoadSign CLI didn't complied with vcs information, compatibility was disabled. To enable it, reinstall cli with -buildvcs flag.")
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
17
pkg/cmd/rdc/conn/directory.go
Normal file
17
pkg/cmd/rdc/conn/directory.go
Normal file
@ -0,0 +1,17 @@
|
||||
package conn
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/samber/lo"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func GetConnection(id string) (CliConnection, bool) {
|
||||
var servers []CliConnection
|
||||
raw, _ := json.Marshal(viper.Get("servers"))
|
||||
_ = json.Unmarshal(raw, &servers)
|
||||
return lo.Find(servers, func(item CliConnection) bool {
|
||||
return item.ID == id
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user