Files
hamlogger/cmd/hamloggerctl/main.go
Олег Бородин ada2a49a64 initial import of sources
2024-06-18 10:15:22 +02:00

157 lines
3.2 KiB
Go

/*
* Copyright 2022 Oleg Borodin <borodin@unix7.org>
*/
package main
// https://pkg.go.dev/github.com/google/go-containerregistry/pkg/name
import (
"context"
"errors"
"flag"
"fmt"
"os"
"path/filepath"
"time"
"hamlogger/pkg/istcom"
yaml "gopkg.in/yaml.v3"
)
const (
serviceHelloCmd = "serviceHello"
helpCmd = "hello"
)
func main() {
var err error
util := NewUtil()
err = util.Exec()
if err != nil {
os.Exit(1)
}
os.Exit(0)
}
type UtilOpts struct {
Username string `json:"-" yaml:"-"`
Password string `json:"-" yaml:"-"`
Hostname string `json:"hostname,omitempty" yaml:"hostname,omitempty"`
Timeout int `json:"-" yaml:"-"`
}
type Util struct {
subCmd string
opts UtilOpts
}
func NewUtil() *Util {
util := &Util{}
util.opts.Hostname = fmt.Sprintf("localhost:%d", istcom.DefaultPort)
return util
}
func (util *Util) GetOpt() error {
var err error
exeName := filepath.Base(os.Args[0])
util.opts.Timeout = 60
flag.IntVar(&util.opts.Timeout, "timeout", util.opts.Timeout, "operation timeout")
help := func() {
fmt.Println("")
fmt.Printf("Usage: %s [option] command [command option]\n", exeName)
fmt.Printf("\n")
fmt.Printf("Command list: help, serviceHello\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 serviceHelloCmd:
flagSet := flag.NewFlagSet(serviceHelloCmd, flag.ExitOnError)
flagSet.StringVar(&util.opts.Username, "user", util.opts.Username, "access name")
flagSet.StringVar(&util.opts.Password, "pass", util.opts.Password, "access password")
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 {
Command string `json:"command" yaml:"command"`
Options UtilOpts `json:"options" yaml:"options"`
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
}
timeout := time.Duration(util.opts.Timeout) * time.Second
ctx, _ := context.WithTimeout(context.Background(), timeout)
var res any
switch util.subCmd {
case serviceHelloCmd:
res, err = util.ServiceHello(ctx)
default:
err = errors.New("Unknown cli command")
}
resp := Response{
Command: util.subCmd,
Options: util.opts,
}
if err != nil {
resp.Error = true
resp.Message = fmt.Sprintf("%v", err)
//resp.Result = res
} else {
resp.Result = res
}
respBytes, _ := yaml.Marshal(resp)
fmt.Printf("---\n%s\n", string(respBytes))
return err
}