✨ One-liner 的 CLI #3
@ -1,14 +1,13 @@
|
||||
package administration
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
roadsign "code.smartsheep.studio/goatworks/roadsign/pkg"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func responseConnectivity(c *fiber.Ctx) error {
|
||||
return c.
|
||||
Status(fiber.StatusOK).
|
||||
SendString(fmt.Sprintf("Hello from RoadSign v%s", roadsign.AppVersion))
|
||||
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
||||
"server": "RoadSign",
|
||||
"version": roadsign.AppVersion,
|
||||
})
|
||||
}
|
||||
|
@ -1,25 +0,0 @@
|
||||
package conn
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type CliConnection struct {
|
||||
Url string `json:"url"`
|
||||
Credential string `json:"credential"`
|
||||
}
|
||||
|
||||
func (v CliConnection) GetConnectivity() error {
|
||||
client := fiber.Get(v.Url + "/cgi/connectivity")
|
||||
client.BasicAuth("RoadSign CLI", v.Credential)
|
||||
|
||||
if status, _, err := client.String(); 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")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -22,7 +22,7 @@ var CliCommands = []*cli.Command{
|
||||
|
||||
log.Info().Msgf("There are %d server(s) connected in total.", len(servers))
|
||||
for idx, server := range servers {
|
||||
log.Info().Msgf("%d. %s", idx+1, server.Url)
|
||||
log.Info().Msgf("%d) %s: %s", idx+1, server.ID, server.Url)
|
||||
}
|
||||
|
||||
return nil
|
||||
@ -32,15 +32,16 @@ var CliCommands = []*cli.Command{
|
||||
Name: "connect",
|
||||
Aliases: []string{"add"},
|
||||
Description: "Connect and save configuration of remote server",
|
||||
ArgsUsage: "<server url> <credential>",
|
||||
ArgsUsage: "<id> <server url> <credential>",
|
||||
Action: func(ctx *cli.Context) error {
|
||||
if ctx.Args().Len() < 2 {
|
||||
return fmt.Errorf("must have more two arguments: <server url> <credential>")
|
||||
if ctx.Args().Len() < 3 {
|
||||
return fmt.Errorf("must have three arguments: <id> <server url> <credential>")
|
||||
}
|
||||
|
||||
c := CliConnection{
|
||||
Url: ctx.Args().Get(0),
|
||||
Credential: ctx.Args().Get(1),
|
||||
ID: ctx.Args().Get(0),
|
||||
Url: ctx.Args().Get(1),
|
||||
Credential: ctx.Args().Get(2),
|
||||
}
|
||||
|
||||
if err := c.GetConnectivity(); err != nil {
|
||||
@ -64,7 +65,7 @@ var CliCommands = []*cli.Command{
|
||||
Name: "disconnect",
|
||||
Aliases: []string{"remove"},
|
||||
Description: "Remove a remote server configuration",
|
||||
ArgsUsage: "<server url>",
|
||||
ArgsUsage: "<id>",
|
||||
Action: func(ctx *cli.Context) error {
|
||||
if ctx.Args().Len() < 1 {
|
||||
return fmt.Errorf("must have more one arguments: <server url>")
|
||||
@ -74,7 +75,7 @@ var CliCommands = []*cli.Command{
|
||||
raw, _ := json.Marshal(viper.Get("servers"))
|
||||
_ = json.Unmarshal(raw, &servers)
|
||||
viper.Set("servers", lo.Filter(servers, func(item CliConnection, idx int) bool {
|
||||
return item.Url != ctx.Args().Get(0)
|
||||
return item.ID != ctx.Args().Get(0)
|
||||
}))
|
||||
|
||||
if err := viper.WriteConfig(); err != nil {
|
37
pkg/cmd/rds/conn/connect.go
Normal file
37
pkg/cmd/rds/conn/connect.go
Normal file
@ -0,0 +1,37 @@
|
||||
package conn
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
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) GetConnectivity() error {
|
||||
client := fiber.Get(v.Url + "/cgi/connectivity")
|
||||
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 {
|
||||
log.Warn().Msg("Server connected successfully, but remote server version mismatch than CLI version, some features may buggy or completely unusable.")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
@ -4,7 +4,7 @@ import (
|
||||
"os"
|
||||
|
||||
roadsign "code.smartsheep.studio/goatworks/roadsign/pkg"
|
||||
"code.smartsheep.studio/goatworks/roadsign/pkg/cmd/cli/conn"
|
||||
"code.smartsheep.studio/goatworks/roadsign/pkg/cmd/rds/conn"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/viper"
|
||||
@ -36,6 +36,7 @@ func main() {
|
||||
app := &cli.App{
|
||||
Name: "RoadSign CLI",
|
||||
Version: roadsign.AppVersion,
|
||||
Suggest: true,
|
||||
Commands: append([]*cli.Command{}, conn.CliCommands...),
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
package roadsign
|
||||
|
||||
const (
|
||||
AppVersion = "1.2.0"
|
||||
AppVersion = "1.2.1"
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user