Skip to content

Commit

Permalink
Merge pull request #35 from Luzilla/ostor-refactor-cmd
Browse files Browse the repository at this point in the history
Chore(ostor): refactor app setup
  • Loading branch information
till authored Nov 7, 2024
2 parents c6bdd8f + 25b5df4 commit db8f3b5
Show file tree
Hide file tree
Showing 9 changed files with 280 additions and 250 deletions.
168 changes: 10 additions & 158 deletions cmd/ostor/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package main

import (
"context"
"fmt"
"log/slog"
"os"

"github.com/Luzilla/acronis-s3-usage/internal/cmd"
"github.com/Luzilla/acronis-s3-usage/pkg/ostor"
"github.com/urfave/cli/v2"
)

Expand All @@ -22,21 +20,15 @@ func main() {
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug})))

app := &cli.App{
Name: "ostor-client",
Name: "ostor-client",
Authors: []*cli.Author{
{
Name: "Luzilla Capital GmbH",
},
},
HelpName: "a program to interact with the s3 management APIs in ACI and VHI",
Version: fmt.Sprintf("%s (%s, date: %s)", version, commit, date),
Before: func(cCtx *cli.Context) error {
client, err := ostor.New(
cCtx.String("s3-endpoint"),
cCtx.String("s3-system-key-id"),
cCtx.String("s3-system-secret"))
if err != nil {
return err
}

cCtx.Context = context.WithValue(cCtx.Context, cmd.OstorClient, client)
return nil
},
Before: cmd.Before,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "s3-endpoint",
Expand All @@ -55,142 +47,9 @@ func main() {
},
},
Commands: []*cli.Command{
{
Name: "buckets",
Aliases: []string{"b"},
Usage: "list buckets",
Action: cmd.ListBuckets,
Flags: []cli.Flag{
emailFlag(),
},
Subcommands: []*cli.Command{
{
Name: "delete",
Aliases: []string{"d"},
Action: cmd.DeleteBucket,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "bucket",
Required: true,
},
&cli.BoolFlag{
Name: "confirm",
Value: false,
},
},
},
{
Name: "show",
Aliases: []string{"s"},
Action: cmd.ShowBucket,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "bucket",
Required: true,
},
},
},
},
},
{
Name: "stats",
Aliases: []string{"s"},
Usage: "list stats",
Flags: []cli.Flag{
// &cli.TimestampFlag{
// Name: "from",
// Layout: "2006-01-02",
// Timezone: time.UTC,
// Required: false,
// },
},
Action: cmd.ShowStats,
},
{
Name: "users",
Aliases: []string{"u"},
Usage: "manage users",
Action: cmd.Users,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "usage",
Value: false,
},
},
Subcommands: []*cli.Command{
{
Name: "create",
Flags: []cli.Flag{
emailFlag(),
},
Action: cmd.CreateUser,
},
{
Name: "delete",
Flags: []cli.Flag{
emailFlag(),
},
Action: cmd.DeleteUser,
},
{
Name: "lock",
Flags: []cli.Flag{
emailFlag(),
},
Action: cmd.LockUser,
},
{
Name: "unlock",
Flags: []cli.Flag{
emailFlag(),
},
Action: cmd.UnlockUser,
},
{
Name: "show",
Flags: []cli.Flag{
emailFlag(),
},
Action: cmd.ShowUser,
},
{
Name: "create-key",
Flags: []cli.Flag{
emailFlag(),
},
Action: cmd.CreateKey,
},
{
Name: "revoke-key",
Flags: []cli.Flag{
emailFlag(),
&cli.StringFlag{
Name: "key-id",
Required: true,
},
},
Action: cmd.RevokeKey,
},
{
Name: "rotate-key",
Flags: []cli.Flag{
emailFlag(),
&cli.StringFlag{
Name: "key-id",
Required: true,
},
},
Action: cmd.RotateKey,
},
{
Name: "limits",
Flags: []cli.Flag{
emailFlag(),
},
Action: cmd.UserLimits,
},
},
},
cmd.BucketCommand(),
cmd.StatsCommand(),
cmd.UsersCommand(),
},
}

Expand All @@ -199,10 +58,3 @@ func main() {
os.Exit(1)
}
}

func emailFlag() *cli.StringFlag {
return &cli.StringFlag{
Name: "email",
Required: true,
}
}
21 changes: 21 additions & 0 deletions internal/cmd/action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cmd

import (
"context"

"github.com/Luzilla/acronis-s3-usage/pkg/ostor"
"github.com/urfave/cli/v2"
)

func Before(cCtx *cli.Context) error {
client, err := ostor.New(
cCtx.String("s3-endpoint"),
cCtx.String("s3-system-key-id"),
cCtx.String("s3-system-secret"))
if err != nil {
return err
}

cCtx.Context = context.WithValue(cCtx.Context, ostorClient, client)
return nil
}
14 changes: 7 additions & 7 deletions internal/cmd/buckets.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

// this executes the action on 'behalf' of the user by returning the account
// and using the first credential pair to run the delete operations
func DeleteBucket(cCtx *cli.Context) error {
client := cCtx.Context.Value(OstorClient).(*ostor.Ostor)
func deleteBucket(cCtx *cli.Context) error {
client := cCtx.Context.Value(ostorClient).(*ostor.Ostor)

s3, err := s3.NewS3(cCtx.String("s3-endpoint"), cCtx.String("email"), client)
if err != nil {
Expand All @@ -36,8 +36,8 @@ func DeleteBucket(cCtx *cli.Context) error {
return nil
}

func ListBuckets(cCtx *cli.Context) error {
client := cCtx.Context.Value(OstorClient).(*ostor.Ostor)
func listBuckets(cCtx *cli.Context) error {
client := cCtx.Context.Value(ostorClient).(*ostor.Ostor)

buckets, _, err := client.GetBuckets(cCtx.String("email"))
if err != nil {
Expand All @@ -60,10 +60,10 @@ func ListBuckets(cCtx *cli.Context) error {
return nil
}

func ShowBucket(cCtx *cli.Context) error {
ListBuckets(cCtx) // display the filter view first
func showBucket(cCtx *cli.Context) error {
listBuckets(cCtx) // display the filter view first

client := cCtx.Context.Value(OstorClient).(*ostor.Ostor)
client := cCtx.Context.Value(ostorClient).(*ostor.Ostor)

s3, err := s3.NewS3(cCtx.String("s3-endpoint"), cCtx.String("email"), client)
if err != nil {
Expand Down
Loading

0 comments on commit db8f3b5

Please sign in to comment.