Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore(ostor): refactor app setup #35

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Check warning on line 1 in cmd/ostor/main.go

View workflow job for this annotation

GitHub Actions / revive

should have a package comment

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