cmd/route: implement route management commands
This commit is contained in:
parent
0897a63aa7
commit
7ecce5f503
|
@ -2,6 +2,8 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
@ -9,11 +11,14 @@ import (
|
|||
|
||||
"git.xeserv.us/xena/route/database"
|
||||
"git.xeserv.us/xena/route/lib/routecrypto"
|
||||
|
||||
proto "git.xeserv.us/xena/route/proto"
|
||||
"github.com/Xe/ln"
|
||||
"github.com/Xe/uuid"
|
||||
jwtcreds "github.com/Xe/x/tools/svc/credentials/jwt"
|
||||
"github.com/dickeyxxx/netrc"
|
||||
|
||||
"github.com/olekukonko/tablewriter"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials"
|
||||
"gopkg.in/alecthomas/kingpin.v2"
|
||||
)
|
||||
|
||||
|
@ -38,40 +43,40 @@ func userHomeDir() string {
|
|||
|
||||
var (
|
||||
app = kingpin.New("route", "An interface to routed")
|
||||
grpcServer = app.Flag("routed-addr", "routed grpc address").Default("127.0.0.1:12823").String()
|
||||
grpcServer = app.Flag("routed-addr", "routed grpc address").Default("127.0.0.1:7268").String()
|
||||
netrcPath = app.Flag("netrc", "netrc path").Default(filepath.Join(userHomeDir(), ".netrc")).String()
|
||||
|
||||
generateKey = app.Command("generate-key", "generate SSL cert crypto key")
|
||||
|
||||
routes = app.Command("route", "route management")
|
||||
|
||||
routesList = routes.Command("list", "list all routes owned by you")
|
||||
|
||||
routesInspect = routes.Command("inspect", "inspect one route")
|
||||
routesInspectID = routesInspect.Flag("route-id", "route ID to inspect").Required().String()
|
||||
|
||||
routesCreate = routes.Command("create", "create a new route")
|
||||
routesCreateDomain = routesCreate.Flag("domain", "domain for the route (if not given one will be generated for you)").String()
|
||||
|
||||
routesInspect = routes.Command("inspect", "inspect one route")
|
||||
routesInspectDomain = routesInspect.Flag("domain", "domain to inspect").Required().String()
|
||||
|
||||
routesList = routes.Command("list", "list all routes owned by you")
|
||||
|
||||
routesRm = routes.Command("rm", "remove a route")
|
||||
routesRmID = routes.Flag("id", "route ID to remove").Required().String()
|
||||
|
||||
token = app.Command("token", "token management")
|
||||
|
||||
tokenCreate = token.Command("create", "create a new token")
|
||||
tokenCreateScopes = tokenCreate.Flag("scope", "token scopes").Default(allScopes...).Strings()
|
||||
|
||||
tokenGenerate = token.Command("generate-root", "generate a root token")
|
||||
tokenGenerateKey = tokenGenerate.Flag("key", "token crypto key").Required().String()
|
||||
tokenGenerateScopes = tokenGenerate.Flag("scopes", "token scopes").Default(allScopes...).Strings()
|
||||
tokenGenerateUsername = tokenGenerate.Flag("username", "token username").Required().String()
|
||||
tokenGenerateDatabasePath = tokenGenerate.Flag("db", "database file to add the root token to").Required().String()
|
||||
|
||||
tokenList = token.Command("list", "list all tokens belonging to you")
|
||||
tokenListDeactivated = tokenList.Flag("deactivated", "list deactivated tokens?").Default("false").Bool()
|
||||
|
||||
tokenInspect = token.Command("inspect", "inspect a token")
|
||||
tokenInspectID = tokenInspect.Arg("token-id", "token id").Required().String()
|
||||
|
||||
tokenCreate = token.Command("create", "create a new token")
|
||||
tokenCreateScopes = tokenCreate.Flag("scope", "token scopes").Default(allScopes...).Strings()
|
||||
tokenList = token.Command("list", "list all tokens belonging to you")
|
||||
tokenListDeactivated = tokenList.Flag("deactivated", "list deactivated tokens?").Default("false").Bool()
|
||||
|
||||
tokenRm = token.Command("rm", "remove a token")
|
||||
tokenRmHard = tokenRm.Flag("hard", "hard-delete the token instead of deactivating it").Default("false").Bool()
|
||||
|
@ -99,7 +104,7 @@ func main() {
|
|||
ln.Fatal(ln.F{"err": err, "action": "routecrypto.GenerateKey"})
|
||||
}
|
||||
|
||||
fmt.Println(routecrypto.ShowKey(key))
|
||||
fmt.Println("Your key is:", routecrypto.ShowKey(key))
|
||||
case "token generate-root":
|
||||
key, err := routecrypto.ParseKey(*tokenGenerateKey)
|
||||
if err != nil {
|
||||
|
@ -128,4 +133,65 @@ func main() {
|
|||
|
||||
return
|
||||
}
|
||||
|
||||
m := n.Machine(*grpcServer)
|
||||
|
||||
connCreds := credentials.NewTLS(&tls.Config{})
|
||||
creds := jwtcreds.NewFromToken(m.Get("password"))
|
||||
conn, err := grpc.Dial(*grpcServer,
|
||||
grpc.WithTransportCredentials(connCreds),
|
||||
grpc.WithPerRPCCredentials(creds))
|
||||
if err != nil {
|
||||
ln.Fatal(ln.F{"err": err, "action": "grpc.Dial"})
|
||||
}
|
||||
|
||||
rc := proto.NewRoutesClient(conn)
|
||||
tc := proto.NewTokensClient(conn)
|
||||
|
||||
_ = rc
|
||||
_ = tc
|
||||
|
||||
switch cmdline {
|
||||
case "route create":
|
||||
idr, err := rc.Put(context.Background(), &proto.Route{Host: *routesCreateDomain})
|
||||
if err != nil {
|
||||
ln.Fatal(ln.F{"err": err, "action": "rc.Put"})
|
||||
}
|
||||
|
||||
fmt.Println(idr.Id)
|
||||
|
||||
case "route inspect":
|
||||
r, err := rc.Get(context.Background(), &proto.GetRouteRequest{
|
||||
Host: *routesCreateDomain,
|
||||
})
|
||||
if err != nil {
|
||||
ln.Fatal(ln.F{"err": err, "action": "rc.Get"})
|
||||
}
|
||||
|
||||
json.NewEncoder(os.Stdout).Encode(r)
|
||||
fmt.Println()
|
||||
|
||||
return
|
||||
|
||||
case "route list":
|
||||
rts, err := rc.GetAll(context.Background(), &proto.Nil{})
|
||||
if err != nil {
|
||||
ln.Fatal(ln.F{"err": err, "action": "rc.GetAll"})
|
||||
}
|
||||
|
||||
table := tablewriter.NewWriter(os.Stdout)
|
||||
table.SetHeader([]string{"ID", "Host"})
|
||||
|
||||
for _, r := range rts.Routes {
|
||||
table.Append([]string{r.Id, r.Host})
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
case "route rm":
|
||||
_, err := rc.Delete(context.Background(), &proto.Route{Id: *routesRmID})
|
||||
if err != nil {
|
||||
ln.Fatal(ln.F{"err": err, "action": "rc.Delete"})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue