Skip to content
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
9 changes: 9 additions & 0 deletions cmd/storage/lifecycle/lifecycle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package lifecycle

import "github.com/spf13/cobra"

var Cmd = &cobra.Command{
Use: "lifecycle",
Short: "Object Storage Bucket lifecycle management",
Long: "Object Storage Bucket lifecycle management",
}
50 changes: 50 additions & 0 deletions cmd/storage/lifecycle/lifecycle_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package lifecycle

import (
"fmt"
"strings"

"github.com/spf13/cobra"

exocmd "github.com/exoscale/cli/cmd"
"github.com/exoscale/cli/pkg/storage/sos"
)

type deleteCmd struct {
exocmd.CliCommandSettings `cli-cmd:"-"`

_ bool `cli-cmd:"delete"`

Bucket string `cli-arg:"#" cli-usage:"sos://BUCKET"`

Zone string `cli-short:"z" cli-usage:"zone"`
}

func (c *deleteCmd) CmdAliases() []string { return exocmd.GDeleteAlias }
func (c *deleteCmd) CmdShort() string { return "Delete lifecycle configuration" }
func (c *deleteCmd) CmdLong() string { return "Delete lifecycle configuration" }

func (c *deleteCmd) CmdPreRun(cmd *cobra.Command, args []string) error {
exocmd.CmdSetZoneFlagFromDefault(cmd)
return exocmd.CliCommandDefaultPreRun(c, cmd, args)
}

func (c *deleteCmd) CmdRun(_ *cobra.Command, _ []string) error {
bucket := strings.TrimPrefix(c.Bucket, sos.BucketPrefix)

storage, err := sos.NewStorageClient(
exocmd.GContext,
sos.ClientOptWithZone(c.Zone),
)
if err != nil {
return fmt.Errorf("unable to initialize storage client: %w", err)
}

return storage.DeleteBucketLifecycle(exocmd.GContext, bucket)
}

func init() {
cobra.CheckErr(exocmd.RegisterCLICommand(Cmd, &deleteCmd{
CliCommandSettings: exocmd.DefaultCLICmdSettings(),
}))
}
78 changes: 78 additions & 0 deletions cmd/storage/lifecycle/lifecycle_set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package lifecycle

import (
"encoding/json"
"fmt"
"os"
"strings"

"github.com/spf13/cobra"

exocmd "github.com/exoscale/cli/cmd"
"github.com/exoscale/cli/pkg/storage/sos"
)

type setCmd struct {
exocmd.CliCommandSettings `cli-cmd:"-"`

_ bool `cli-cmd:"set"`

Bucket string `cli-arg:"#" cli-usage:"sos://BUCKET"`
File string `cli-arg:"#" cli-usage:"path/to/lifecycle.json"`

Zone string `cli-short:"z" cli-usage:"zone"`
}

func (c *setCmd) CmdAliases() []string { return nil }
func (c *setCmd) CmdShort() string { return "Set lifecycle configuration" }
func (c *setCmd) CmdLong() string {
return `Set a lifecycle configuration for a bucket.
Example of a valid lifecycle configuration:
{
"Rules": [
{
"Status": "Enabled",
"Expiration": { "Days": 30 },
"Filter": { "Prefix": "" },
"ID": "expire-after-30-days"
}
]
}`
}

func (c *setCmd) CmdPreRun(cmd *cobra.Command, args []string) error {
exocmd.CmdSetZoneFlagFromDefault(cmd)
return exocmd.CliCommandDefaultPreRun(c, cmd, args)
}

func (c *setCmd) CmdRun(_ *cobra.Command, _ []string) error {
bucket := strings.TrimPrefix(c.Bucket, sos.BucketPrefix)

confFile, err := os.Open(c.File)
if err != nil {
return err
}
defer func() { _ = confFile.Close() }()

var configuration sos.BucketLifecycleConf
if err = json.NewDecoder(confFile).Decode(&configuration); err != nil {
return err
}

storage, err := sos.NewStorageClient(
exocmd.GContext,
sos.ClientOptWithZone(c.Zone),
)
if err != nil {
return fmt.Errorf("unable to initialize storage client: %w", err)
}

return storage.PutBucketLifecycle(exocmd.GContext, bucket, configuration.ToS3())
}

func init() {
cobra.CheckErr(exocmd.RegisterCLICommand(Cmd, &setCmd{
CliCommandSettings: exocmd.DefaultCLICmdSettings(),
}))
}
55 changes: 55 additions & 0 deletions cmd/storage/lifecycle/lifecycle_show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package lifecycle

import (
"fmt"
"strings"

"github.com/spf13/cobra"

exocmd "github.com/exoscale/cli/cmd"
"github.com/exoscale/cli/pkg/storage/sos"
)

type showCmd struct {
exocmd.CliCommandSettings `cli-cmd:"-"`

_ bool `cli-cmd:"show"`

Bucket string `cli-arg:"#" cli-usage:"sos://BUCKET"`

Zone string `cli-short:"z" cli-usage:"zone"`
}

func (c *showCmd) CmdAliases() []string { return exocmd.GShowAlias }
func (c *showCmd) CmdShort() string { return "Retrieve lifecycle configuration" }
func (c *showCmd) CmdLong() string { return "Retrieve lifecycle configuration" }

func (c *showCmd) CmdPreRun(cmd *cobra.Command, args []string) error {
exocmd.CmdSetZoneFlagFromDefault(cmd)
return exocmd.CliCommandDefaultPreRun(c, cmd, args)
}

func (c *showCmd) CmdRun(_ *cobra.Command, _ []string) error {
bucket := strings.TrimPrefix(c.Bucket, sos.BucketPrefix)

storage, err := sos.NewStorageClient(
exocmd.GContext,
sos.ClientOptWithZone(c.Zone),
)
if err != nil {
return fmt.Errorf("unable to initialize storage client: %w", err)
}

o, err := storage.GetBucketLifecycle(exocmd.GContext, bucket)
if err != nil {
return err
}

return c.OutputFunc(o, nil)
}

func init() {
cobra.CheckErr(exocmd.RegisterCLICommand(Cmd, &showCmd{
CliCommandSettings: exocmd.DefaultCLICmdSettings(),
}))
}
2 changes: 2 additions & 0 deletions cmd/storage/storage_bucket.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package storage

import (
"github.com/exoscale/cli/cmd/storage/lifecycle"
"github.com/spf13/cobra"
)

func init() {
storageCmd.AddCommand(storageBucketCmd)
storageBucketCmd.AddCommand(lifecycle.Cmd)
}

var storageBucketCmd = &cobra.Command{
Expand Down
21 changes: 0 additions & 21 deletions cmd/storage/storage_bucket_lifecycle.go

This file was deleted.

48 changes: 0 additions & 48 deletions cmd/storage/storage_bucket_lifecycle_delete.go

This file was deleted.

78 changes: 0 additions & 78 deletions cmd/storage/storage_bucket_lifecycle_set.go

This file was deleted.

Loading
Loading