import sources
This commit is contained in:
1
cmd/certmanagerctl/.gitignore
vendored
Normal file
1
cmd/certmanagerctl/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
lbmanagerctl*
|
||||
187
cmd/certmanagerctl/main.go
Normal file
187
cmd/certmanagerctl/main.go
Normal file
@@ -0,0 +1,187 @@
|
||||
/*
|
||||
* Copyright 2022 Oleg Borodin <borodin@unix7.org>
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
lbapi "certmanager/api/certmanagercontrol"
|
||||
"certmanager/pkg/client"
|
||||
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultHostname = "localhost"
|
||||
rcFilename = ".certmanager.yaml"
|
||||
defaultPort int = client.DefaultGrpcPort
|
||||
|
||||
getStatusCmd = "getStatus"
|
||||
helpCmd = "help"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var err error
|
||||
util := NewUtil()
|
||||
err = util.Exec()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
type Util struct {
|
||||
subCmd string
|
||||
cmdTimeout int64
|
||||
access client.Access
|
||||
cont *lbapi.ControlClient
|
||||
|
||||
getStatusParams lbapi.GetStatusParams
|
||||
}
|
||||
|
||||
func NewUtil() *Util {
|
||||
var util Util
|
||||
util.cmdTimeout = 120
|
||||
util.access = client.Access{
|
||||
Hostname: defaultHostname,
|
||||
Port: defaultPort,
|
||||
Username: "certmanager",
|
||||
Password: "certmanager",
|
||||
}
|
||||
return &util
|
||||
}
|
||||
|
||||
func (util *Util) GetOpt() error {
|
||||
var err error
|
||||
|
||||
homeDir := os.Getenv("HOME")
|
||||
if homeDir == "" {
|
||||
currUsr, err := user.Current()
|
||||
if err == nil {
|
||||
homeDir = currUsr.HomeDir
|
||||
}
|
||||
}
|
||||
if homeDir != "" {
|
||||
confPath := filepath.Join(homeDir, rcFilename)
|
||||
confData, err := os.ReadFile(confPath)
|
||||
if err == nil && len(confData) > 0 {
|
||||
yaml.Unmarshal(confData, &util.access)
|
||||
}
|
||||
}
|
||||
exeName := filepath.Base(os.Args[0])
|
||||
|
||||
flag.Int64Var(&util.cmdTimeout, "timeout", util.cmdTimeout, "command execution timeout")
|
||||
flag.StringVar(&util.access.Hostname, "host", util.access.Hostname, "service hostname")
|
||||
flag.IntVar(&util.access.Port, "port", util.access.Port, "service port")
|
||||
flag.StringVar(&util.access.Username, "user", util.access.Username, "access login")
|
||||
flag.StringVar(&util.access.Password, "pass", util.access.Password, "access password")
|
||||
|
||||
help := func() {
|
||||
fmt.Println("")
|
||||
fmt.Printf("Usage: %s [option] command [command option]\n", exeName)
|
||||
fmt.Printf("\n")
|
||||
fmt.Printf("Command list: help, getStatus\n")
|
||||
fmt.Printf("\n")
|
||||
fmt.Printf("Global options:\n")
|
||||
flag.PrintDefaults()
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
flag.Usage = help
|
||||
flag.Parse()
|
||||
|
||||
args := flag.Args()
|
||||
|
||||
var subCmd string
|
||||
var subArgs []string
|
||||
if len(args) > 0 {
|
||||
subCmd = args[0]
|
||||
subArgs = args[1:]
|
||||
}
|
||||
|
||||
switch subCmd {
|
||||
case helpCmd:
|
||||
help()
|
||||
return errors.New("Unknown command")
|
||||
case getStatusCmd:
|
||||
flagSet := flag.NewFlagSet(getStatusCmd, flag.ExitOnError)
|
||||
|
||||
flagSet.Usage = func() {
|
||||
fmt.Printf("\n")
|
||||
fmt.Printf("Usage: %s [global options] %s [command options]\n", exeName, subCmd)
|
||||
fmt.Printf("\n")
|
||||
fmt.Printf("The command options: none\n")
|
||||
flagSet.PrintDefaults()
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
flagSet.Parse(subArgs)
|
||||
util.subCmd = subCmd
|
||||
|
||||
default:
|
||||
help()
|
||||
return errors.New("Unknown command")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Error bool `json:"error" yaml:"error"`
|
||||
Message string `json:"message,omitempty" yaml:"message,omitempty"`
|
||||
Result any `json:"result,omitempty" yaml:"result,omitempty"`
|
||||
}
|
||||
|
||||
func (util *Util) Exec() error {
|
||||
var err error
|
||||
err = util.GetOpt()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var timeout = time.Duration(util.cmdTimeout) * time.Second
|
||||
ctx, close := context.WithTimeout(context.Background(), timeout)
|
||||
defer close()
|
||||
|
||||
var res any
|
||||
switch util.subCmd {
|
||||
case getStatusCmd:
|
||||
res, err = util.GetStatus(ctx)
|
||||
default:
|
||||
err = errors.New("Unknown cli command")
|
||||
}
|
||||
|
||||
var resp Response
|
||||
if err != nil {
|
||||
resp.Error = true
|
||||
resp.Message = fmt.Sprintf("%v", err)
|
||||
} else {
|
||||
resp.Result = res
|
||||
}
|
||||
respBytes, _ := yaml.Marshal(resp)
|
||||
fmt.Println(string(respBytes))
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (util *Util) GetStatus(ctx context.Context) (*lbapi.GetStatusResult, error) {
|
||||
var err error
|
||||
res := &lbapi.GetStatusResult{}
|
||||
cont, err := client.NewClient(&util.access)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
req := util.getStatusParams
|
||||
res, err = cont.GetStatus(ctx, &req)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user