mirror of
https://github.com/crazy-max/diun.git
synced 2025-12-21 13:23:09 +01:00
cli: remove BeforeApply hooks and align command structs
This commit is contained in:
35
cmd/cli.go
35
cmd/cli.go
@@ -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
|
|
||||||
}
|
|
||||||
72
cmd/image.go
72
cmd/image.go
@@ -14,27 +14,35 @@ import (
|
|||||||
units "github.com/docker/go-units"
|
units "github.com/docker/go-units"
|
||||||
"github.com/jedib0t/go-pretty/v6/table"
|
"github.com/jedib0t/go-pretty/v6/table"
|
||||||
"github.com/tidwall/pretty"
|
"github.com/tidwall/pretty"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/credentials/insecure"
|
||||||
"google.golang.org/protobuf/encoding/protojson"
|
"google.golang.org/protobuf/encoding/protojson"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ImageCmd holds image command
|
// ImageCmd holds image command
|
||||||
type ImageCmd struct {
|
type ImageCmd struct {
|
||||||
List ImageListCmd `kong:"cmd,default='1',help='List images in database.'"`
|
List ImageListCmd `cmd:"" default:"1" help:"List images in database."`
|
||||||
Inspect ImageInspectCmd `kong:"cmd,help='Display information of an image in database.'"`
|
Inspect ImageInspectCmd `cmd:"" help:"Display information of an image in database."`
|
||||||
Remove ImageRemoveCmd `kong:"cmd,help='Remove an image manifest from database.'"`
|
Remove ImageRemoveCmd `cmd:"" help:"Remove an image manifest from database."`
|
||||||
Prune ImagePruneCmd `kong:"cmd,help='Remove all manifests from the database.'"`
|
Prune ImagePruneCmd `cmd:"" help:"Remove all manifests from the database."`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ImageListCmd holds image list command
|
// ImageListCmd holds image list command
|
||||||
type ImageListCmd struct {
|
type ImageListCmd struct {
|
||||||
CliGlobals
|
Raw bool `name:"raw" default:"false" help:"JSON output."`
|
||||||
Raw bool `kong:"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 {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -68,15 +76,21 @@ func (s *ImageListCmd) Run(_ *Context) error {
|
|||||||
|
|
||||||
// ImageInspectCmd holds image inspect command
|
// ImageInspectCmd holds image inspect command
|
||||||
type ImageInspectCmd struct {
|
type ImageInspectCmd struct {
|
||||||
CliGlobals
|
Image string `name:"image" required:"" help:"Image to inspect."`
|
||||||
Image string `kong:"name='image',required,help='Image to inspect.'"`
|
Raw bool `name:"raw" default:"false" help:"JSON output."`
|
||||||
Raw bool `kong:"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 {
|
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,
|
Name: s.Image,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -107,14 +121,20 @@ func (s *ImageInspectCmd) Run(_ *Context) error {
|
|||||||
|
|
||||||
// ImageRemoveCmd holds image remove command
|
// ImageRemoveCmd holds image remove command
|
||||||
type ImageRemoveCmd struct {
|
type ImageRemoveCmd struct {
|
||||||
CliGlobals
|
Image string `name:"image" required:"" help:"Image to remove."`
|
||||||
Image string `kong:"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 {
|
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,
|
Name: s.Image,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -137,10 +157,10 @@ func (s *ImageRemoveCmd) Run(_ *Context) error {
|
|||||||
|
|
||||||
// ImagePruneCmd holds image prune command
|
// ImagePruneCmd holds image prune command
|
||||||
type ImagePruneCmd struct {
|
type ImagePruneCmd struct {
|
||||||
CliGlobals
|
// All bool `name:"all' default:"false help:"Remove all manifests from the database."`
|
||||||
// All bool `kong:"name='all',default='false',help='Remove all manifests from the database.'"`
|
// Filter string `name:"filter help:"Provide filter values (e.g., until=24h)."`
|
||||||
// Filter string `kong:"name='filter',help='Provide filter values (e.g., until=24h).'"`
|
Force bool `name:"force" default:"false" help:"Do not prompt for confirmation."`
|
||||||
Force bool `kong:"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 (
|
const (
|
||||||
@@ -148,7 +168,13 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (s *ImagePruneCmd) Run(_ *Context) error {
|
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 {
|
if !s.Force {
|
||||||
var confirmed bool
|
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,
|
//All: s.All,
|
||||||
//Filter: s.Filter,
|
//Filter: s.Filter,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -16,9 +16,9 @@ var (
|
|||||||
version = "dev"
|
version = "dev"
|
||||||
cli struct {
|
cli struct {
|
||||||
Version kong.VersionFlag
|
Version kong.VersionFlag
|
||||||
Serve ServeCmd `kong:"cmd,help='Starts Diun server.'"`
|
Serve ServeCmd `cmd:"" help:"Starts Diun server."`
|
||||||
Image ImageCmd `kong:"cmd,help='Manage image manifests.'"`
|
Image ImageCmd `cmd:"" help:"Manage image manifests."`
|
||||||
Notif NotifCmd `kong:"cmd,help='Manage notifications.'"`
|
Notif NotifCmd `cmd:"" help:"Manage notifications."`
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
16
cmd/notif.go
16
cmd/notif.go
@@ -5,22 +5,30 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/crazy-max/diun/v4/pb"
|
"github.com/crazy-max/diun/v4/pb"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/credentials/insecure"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NotifCmd holds notif command
|
// NotifCmd holds notif command
|
||||||
type NotifCmd struct {
|
type NotifCmd struct {
|
||||||
Test NotifTestCmd `kong:"cmd,help='Test notification settings.'"`
|
Test NotifTestCmd `cmd:"" help:"Test notification settings."`
|
||||||
}
|
}
|
||||||
|
|
||||||
// NotifTestCmd holds notif test command
|
// NotifTestCmd holds notif test command
|
||||||
type NotifTestCmd struct {
|
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 {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
16
cmd/serve.go
16
cmd/serve.go
@@ -15,14 +15,14 @@ import (
|
|||||||
|
|
||||||
// ServeCmd holds serve command args and flags
|
// ServeCmd holds serve command args and flags
|
||||||
type ServeCmd struct {
|
type ServeCmd struct {
|
||||||
Cfgfile string `kong:"name='config',env='CONFIG',help='Diun configuration file.'"`
|
Cfgfile string `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.'"`
|
ProfilerPath string `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.'"`
|
Profiler string `name:"profiler" env:"PROFILER" help:"Profiler to use."`
|
||||||
LogLevel string `kong:"name='log-level',env='LOG_LEVEL',default='info',help='Set log level.'"`
|
LogLevel string `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.'"`
|
LogJSON bool `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.'"`
|
LogCaller bool `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.'"`
|
LogNoColor bool `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.'"`
|
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 {
|
func (s *ServeCmd) Run(ctx *Context) error {
|
||||||
|
|||||||
Reference in New Issue
Block a user