🎉 Inits admin cli app
This commit is contained in:
94
admin/main.go
Normal file
94
admin/main.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
var (
|
||||
server string
|
||||
token string
|
||||
)
|
||||
|
||||
func load() error {
|
||||
viper.SetConfigName("config")
|
||||
viper.SetConfigType("toml")
|
||||
viper.AddConfigPath("$HOME/.local/share/keys")
|
||||
err := viper.ReadInConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
server = viper.GetString("server")
|
||||
if server == "" {
|
||||
return errors.New("'server' property is required")
|
||||
}
|
||||
|
||||
token = viper.GetString("token")
|
||||
if token == "" {
|
||||
return errors.New("'token' property is required")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
cmd := &cli.Command{
|
||||
Name: "keys-admin",
|
||||
Usage: "keys project server administration tool",
|
||||
Commands: []*cli.Command{
|
||||
{
|
||||
Name: "version",
|
||||
Usage: "print version and exit",
|
||||
Action: func(context.Context, *cli.Command) error {
|
||||
fmt.Println("version 1.0")
|
||||
return nil
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "clients",
|
||||
Usage: "fetch all clients",
|
||||
Action: func(context.Context, *cli.Command) error {
|
||||
err := load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("%s/admin/clients", server)
|
||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", token)
|
||||
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("%s", body)
|
||||
return nil
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
err := cmd.Run(context.Background(), os.Args)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user