From 0863995b1e594c79800ac8823ac1200432171f91 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Mon, 4 Aug 2025 00:02:46 +0200 Subject: [PATCH] cli: remove BeforeApply hooks and align command structs --- cmd/cli.go | 35 ------------------------- cmd/image.go | 72 +++++++++++++++++++++++++++++++++++----------------- cmd/main.go | 6 ++--- cmd/notif.go | 16 +++++++++--- cmd/serve.go | 16 ++++++------ 5 files changed, 72 insertions(+), 73 deletions(-) delete mode 100644 cmd/cli.go diff --git a/cmd/cli.go b/cmd/cli.go deleted file mode 100644 index 874a1f6d..00000000 --- a/cmd/cli.go +++ /dev/null @@ -1,35 +0,0 @@ -package main - -import ( - "github.com/crazy-max/diun/v4/pb" - "google.golang.org/grpc" - "google.golang.org/grpc/credentials/insecure" -) - -// CliHandler is a cli interface -type CliHandler interface { - BeforeApply() error -} - -// CliGlobals holds globals cli attributes -type CliGlobals struct { - CliHandler `kong:"-"` - - conn *grpc.ClientConn `kong:"-"` - imageSvc pb.ImageServiceClient `kong:"-"` - notifSvc pb.NotifServiceClient `kong:"-"` - - GRPCAuthority string `kong:"name='grpc-authority',default='127.0.0.1:42286',help='Link to Diun gRPC API.'"` -} - -// BeforeApply is a hook that run cli cmd are executed. -func (s *CliGlobals) BeforeApply() (err error) { - s.conn, err = grpc.NewClient(s.GRPCAuthority, grpc.WithTransportCredentials(insecure.NewCredentials())) - if err != nil { - return err - } - - s.imageSvc = pb.NewImageServiceClient(s.conn) - s.notifSvc = pb.NewNotifServiceClient(s.conn) - return -} diff --git a/cmd/image.go b/cmd/image.go index 7d608d7b..307cc957 100644 --- a/cmd/image.go +++ b/cmd/image.go @@ -14,27 +14,35 @@ import ( units "github.com/docker/go-units" "github.com/jedib0t/go-pretty/v6/table" "github.com/tidwall/pretty" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" "google.golang.org/protobuf/encoding/protojson" ) // ImageCmd holds image command type ImageCmd struct { - List ImageListCmd `kong:"cmd,default='1',help='List images in database.'"` - Inspect ImageInspectCmd `kong:"cmd,help='Display information of an image in database.'"` - Remove ImageRemoveCmd `kong:"cmd,help='Remove an image manifest from database.'"` - Prune ImagePruneCmd `kong:"cmd,help='Remove all manifests from the database.'"` + List ImageListCmd `cmd:"" default:"1" help:"List images in database."` + Inspect ImageInspectCmd `cmd:"" help:"Display information of an image in database."` + Remove ImageRemoveCmd `cmd:"" help:"Remove an image manifest from database."` + Prune ImagePruneCmd `cmd:"" help:"Remove all manifests from the database."` } // ImageListCmd holds image list command type ImageListCmd struct { - CliGlobals - Raw bool `kong:"name='raw',default='false',help='JSON output.'"` + Raw bool `name:"raw" default:"false" help:"JSON output."` + GRPCAuthority string `name:"grpc-authority" default:"127.0.0.1:42286" help:"Link to Diun gRPC server."` } func (s *ImageListCmd) Run(_ *Context) error { - defer s.conn.Close() + conn, err := grpc.NewClient(s.GRPCAuthority, grpc.WithTransportCredentials(insecure.NewCredentials())) + if err != nil { + return err + } + defer conn.Close() - il, err := s.imageSvc.ImageList(context.Background(), &pb.ImageListRequest{}) + imageSvc := pb.NewImageServiceClient(conn) + + il, err := imageSvc.ImageList(context.Background(), &pb.ImageListRequest{}) if err != nil { return err } @@ -68,15 +76,21 @@ func (s *ImageListCmd) Run(_ *Context) error { // ImageInspectCmd holds image inspect command type ImageInspectCmd struct { - CliGlobals - Image string `kong:"name='image',required,help='Image to inspect.'"` - Raw bool `kong:"name='raw',default='false',help='JSON output.'"` + Image string `name:"image" required:"" help:"Image to inspect."` + Raw bool `name:"raw" default:"false" help:"JSON output."` + GRPCAuthority string `name:"grpc-authority" default:"127.0.0.1:42286" help:"Link to Diun gRPC server."` } func (s *ImageInspectCmd) Run(_ *Context) error { - defer s.conn.Close() + conn, err := grpc.NewClient(s.GRPCAuthority, grpc.WithTransportCredentials(insecure.NewCredentials())) + if err != nil { + return err + } + defer conn.Close() - ii, err := s.imageSvc.ImageInspect(context.Background(), &pb.ImageInspectRequest{ + imageSvc := pb.NewImageServiceClient(conn) + + ii, err := imageSvc.ImageInspect(context.Background(), &pb.ImageInspectRequest{ Name: s.Image, }) if err != nil { @@ -107,14 +121,20 @@ func (s *ImageInspectCmd) Run(_ *Context) error { // ImageRemoveCmd holds image remove command type ImageRemoveCmd struct { - CliGlobals - Image string `kong:"name='image',required,help='Image to remove.'"` + Image string `name:"image" required:"" help:"Image to remove."` + GRPCAuthority string `name:"grpc-authority" default:"127.0.0.1:42286" help:"Link to Diun gRPC server."` } func (s *ImageRemoveCmd) Run(_ *Context) error { - defer s.conn.Close() + conn, err := grpc.NewClient(s.GRPCAuthority, grpc.WithTransportCredentials(insecure.NewCredentials())) + if err != nil { + return err + } + defer conn.Close() - removed, err := s.imageSvc.ImageRemove(context.Background(), &pb.ImageRemoveRequest{ + imageSvc := pb.NewImageServiceClient(conn) + + removed, err := imageSvc.ImageRemove(context.Background(), &pb.ImageRemoveRequest{ Name: s.Image, }) if err != nil { @@ -137,10 +157,10 @@ func (s *ImageRemoveCmd) Run(_ *Context) error { // ImagePruneCmd holds image prune command type ImagePruneCmd struct { - CliGlobals - // All bool `kong:"name='all',default='false',help='Remove all manifests from the database.'"` - // Filter string `kong:"name='filter',help='Provide filter values (e.g., until=24h).'"` - Force bool `kong:"name='force',default='false',help='Do not prompt for confirmation.'"` + // All bool `name:"all' default:"false help:"Remove all manifests from the database."` + // Filter string `name:"filter help:"Provide filter values (e.g., until=24h)."` + Force bool `name:"force" default:"false" help:"Do not prompt for confirmation."` + GRPCAuthority string `name:"grpc-authority" default:"127.0.0.1:42286" help:"Link to Diun gRPC server."` } const ( @@ -148,7 +168,13 @@ const ( ) func (s *ImagePruneCmd) Run(_ *Context) error { - defer s.conn.Close() + conn, err := grpc.NewClient(s.GRPCAuthority, grpc.WithTransportCredentials(insecure.NewCredentials())) + if err != nil { + return err + } + defer conn.Close() + + imageSvc := pb.NewImageServiceClient(conn) if !s.Force { var confirmed bool @@ -163,7 +189,7 @@ func (s *ImagePruneCmd) Run(_ *Context) error { } } - removed, err := s.imageSvc.ImagePrune(context.Background(), &pb.ImagePruneRequest{ + removed, err := imageSvc.ImagePrune(context.Background(), &pb.ImagePruneRequest{ //All: s.All, //Filter: s.Filter, }) diff --git a/cmd/main.go b/cmd/main.go index 43c65ad6..4be53e67 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -16,9 +16,9 @@ var ( version = "dev" cli struct { Version kong.VersionFlag - Serve ServeCmd `kong:"cmd,help='Starts Diun server.'"` - Image ImageCmd `kong:"cmd,help='Manage image manifests.'"` - Notif NotifCmd `kong:"cmd,help='Manage notifications.'"` + Serve ServeCmd `cmd:"" help:"Starts Diun server."` + Image ImageCmd `cmd:"" help:"Manage image manifests."` + Notif NotifCmd `cmd:"" help:"Manage notifications."` } ) diff --git a/cmd/notif.go b/cmd/notif.go index 17471186..c5a219a5 100644 --- a/cmd/notif.go +++ b/cmd/notif.go @@ -5,22 +5,30 @@ import ( "fmt" "github.com/crazy-max/diun/v4/pb" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" ) // NotifCmd holds notif command type NotifCmd struct { - Test NotifTestCmd `kong:"cmd,help='Test notification settings.'"` + Test NotifTestCmd `cmd:"" help:"Test notification settings."` } // NotifTestCmd holds notif test command type NotifTestCmd struct { - CliGlobals + GRPCAuthority string `name:"grpc-authority" default:"127.0.0.1:42286" help:"Link to Diun gRPC server."` } func (s *NotifTestCmd) Run(_ *Context) error { - defer s.conn.Close() + conn, err := grpc.NewClient(s.GRPCAuthority, grpc.WithTransportCredentials(insecure.NewCredentials())) + if err != nil { + return err + } + defer conn.Close() - nt, err := s.notifSvc.NotifTest(context.Background(), &pb.NotifTestRequest{}) + notifSvc := pb.NewNotifServiceClient(conn) + + nt, err := notifSvc.NotifTest(context.Background(), &pb.NotifTestRequest{}) if err != nil { return err } diff --git a/cmd/serve.go b/cmd/serve.go index c8bbbdbf..83464573 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -15,14 +15,14 @@ import ( // ServeCmd holds serve command args and flags type ServeCmd struct { - Cfgfile string `kong:"name='config',env='CONFIG',help='Diun configuration file.'"` - ProfilerPath string `kong:"name='profiler-path',env='PROFILER_PATH',help='Base path where profiling files are written.'"` - Profiler string `kong:"name='profiler',env='PROFILER',help='Profiler to use.'"` - LogLevel string `kong:"name='log-level',env='LOG_LEVEL',default='info',help='Set log level.'"` - LogJSON bool `kong:"name='log-json',env='LOG_JSON',default='false',help='Enable JSON logging output.'"` - LogCaller bool `kong:"name='log-caller',env='LOG_CALLER',default='false',help='Add file:line of the caller to log output.'"` - LogNoColor bool `kong:"name='log-nocolor',env='LOG_NOCOLOR',default='false',help='Disables the colorized output.'"` - GRPCAuthority string `kong:"name='grpc-authority',env='GRPC_AUTHORITY',default=':42286',help='Address used to expose the gRPC server.'"` + Cfgfile string `name:"config" env:"CONFIG" help:"Diun configuration file."` + ProfilerPath string `name:"profiler-path" env:"PROFILER_PATH" help:"Base path where profiling files are written."` + Profiler string `name:"profiler" env:"PROFILER" help:"Profiler to use."` + LogLevel string `name:"log-level" env:"LOG_LEVEL" default:"info" help:"Set log level."` + LogJSON bool `name:"log-json" env:"LOG_JSON" default:"false" help:"Enable JSON logging output.'"` + LogCaller bool `name:"log-caller" env:"LOG_CALLER" default:"false" help:"Add file:line of the caller to log output."` + LogNoColor bool `name:"log-nocolor" env:"LOG_NOCOLOR" default:"false" help:"Disables the colorized output."` + GRPCAuthority string `name:"grpc-authority" env:"GRPC_AUTHORITY" default:":42286" help:"Address used to expose the gRPC server."` } func (s *ServeCmd) Run(ctx *Context) error {