working commit

This commit is contained in:
2026-05-29 19:07:59 +02:00
parent 4e2c548e97
commit c1f85e87c9
23 changed files with 543 additions and 342 deletions
+64
View File
@@ -0,0 +1,64 @@
/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*/
package servcmd
import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const (
uuidRegex = `^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`
defaultHostname = "localhost:1025"
)
func NewServiceUtil() *ServiceUtil {
return &ServiceUtil{}
}
type ServiceUtil struct {
getHelloParams GetHelloParams
commonServiceParams CommonServiceParams
}
type CommonServiceParams struct {
Username string
Password string
Hostname string
Timeout uint64
SkipTLSVerify bool
}
func (util *ServiceUtil) Makeservcmds() *cobra.Command {
var subCmd = &cobra.Command{
Use: "service",
Short: "Service operations",
}
const defaultTimeout uint64 = 10
subCmd.PersistentFlags().StringVarP(&util.commonServiceParams.Username, "user", "U", util.commonServiceParams.Username, "Username")
subCmd.PersistentFlags().StringVarP(&util.commonServiceParams.Password, "pass", "P", util.commonServiceParams.Password, "Password")
subCmd.PersistentFlags().StringVarP(&util.commonServiceParams.Hostname, "host", "X", defaultHostname, "Hostname")
subCmd.PersistentFlags().Uint64VarP(&util.commonServiceParams.Timeout, "timeout", "T", defaultTimeout, "Operation timeout")
subCmd.PersistentFlags().BoolVarP(&util.commonServiceParams.SkipTLSVerify, "skipVerify", "S", true, "Skip server certificate verify")
subCmd.MarkFlagsRequiredTogether("user", "pass")
vi := viper.New()
vi.SetEnvPrefix("mproxy")
vi.BindEnv("user")
vi.BindEnv("pass")
util.commonServiceParams.Username = vi.GetString("user")
util.commonServiceParams.Password = vi.GetString("pass")
// GetService
var getservcmd = &cobra.Command{
Use: "get [user:pass@]hostname[:port]",
Short: "Get service hello",
Args: cobra.ExactArgs(1),
Run: util.GetHello,
}
subCmd.AddCommand(getservcmd)
return subCmd
}
+46
View File
@@ -0,0 +1,46 @@
/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*/
package servcmd
import (
"context"
"time"
"github.com/spf13/cobra"
"mproxy/pkg/servcli"
)
// GetHello
type GetHelloParams struct {
Address string
}
func (util *ServiceUtil) GetHello(cmd *cobra.Command, args []string) {
util.getHelloParams.Address = args[0]
res, err := util.getHello(&util.commonServiceParams, &util.getHelloParams)
printResponse(res, err)
}
type GetHelloResult struct{}
func (util *ServiceUtil) getHello(common *CommonServiceParams, params *GetHelloParams) (*GetHelloResult, error) {
var err error
res := &GetHelloResult{}
timeout := time.Duration(common.Timeout) * time.Second
ctx, _ := context.WithTimeout(context.Background(), timeout)
ref, err := servcli.ParsePath(params.Address)
if err != nil {
return res, err
}
ref.SetUserinfo(common.Username, common.Password)
mw := servcli.NewBasicAuthMiddleware(ref.Userinfo())
cli := servcli.NewClient(nil, mw)
err = cli.GetHello(ctx, ref.Raw())
if err != nil {
return res, err
}
return res, err
}
+27
View File
@@ -0,0 +1,27 @@
/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*/
package servcmd
import (
"fmt"
"sigs.k8s.io/yaml"
)
func printResponse(res any, err error) {
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"`
}
resp := Response{}
if err != nil {
resp.Error = true
resp.Message = err.Error()
} else {
resp.Result = res
}
respBytes, _ := yaml.Marshal(resp)
fmt.Printf("---\n%s\n", string(respBytes))
}
+6 -1
View File
@@ -8,6 +8,8 @@ import (
"os"
"path/filepath"
"mproxy/cmd/mproxyctl/servcmd"
"github.com/spf13/cobra"
)
@@ -28,12 +30,15 @@ func (util *Util) Build() error {
execName := filepath.Base(os.Args[0])
rootCmd := &cobra.Command{
Use: execName,
Short: "\nOperation with artefacts: files, images, service accounts and grants",
Short: "\nGet hello from service",
SilenceUsage: true,
}
rootCmd.CompletionOptions.DisableDefaultCmd = true
util.rootCmd = rootCmd
ServiceUtil := servcmd.NewServiceUtil()
rootCmd.AddCommand(ServiceUtil.MakeServiceCmds())
return err
}