cli: remove BeforeApply hooks and align command structs

This commit is contained in:
CrazyMax
2025-08-04 00:02:46 +02:00
parent 1d67f0703c
commit 0863995b1e
5 changed files with 72 additions and 73 deletions

View File

@@ -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
}

View File

@@ -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,
})

View File

@@ -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."`
}
)

View File

@@ -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
}

View File

@@ -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 {