-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add describe subcommand under object-store
- Loading branch information
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package objectstore | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/fastly/cli/pkg/cmd" | ||
"github.com/fastly/cli/pkg/config" | ||
fsterr "github.com/fastly/cli/pkg/errors" | ||
"github.com/fastly/cli/pkg/manifest" | ||
"github.com/fastly/cli/pkg/text" | ||
"github.com/fastly/go-fastly/v7/fastly" | ||
) | ||
|
||
// DescribeStoreCommand calls the Fastly API to fetch the value of a key from an object store. | ||
type DescribeStoreCommand struct { | ||
cmd.Base | ||
json bool | ||
manifest manifest.Data | ||
Input fastly.GetObjectStoreInput | ||
} | ||
|
||
// NewDescribeStoreCommand returns a usable command registered under the parent. | ||
func NewDescribeStoreCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *DescribeStoreCommand { | ||
c := DescribeStoreCommand{ | ||
Base: cmd.Base{ | ||
Globals: globals, | ||
}, | ||
manifest: data, | ||
} | ||
c.CmdClause = parent.Command("describe", "Describe an object store").Alias("get") | ||
c.CmdClause.Flag("store-id", "Store ID").Short('s').Required().StringVar(&c.Input.ID) | ||
|
||
// optional | ||
c.RegisterFlagBool(cmd.BoolFlagOpts{ | ||
Name: cmd.FlagJSONName, | ||
Description: cmd.FlagJSONDesc, | ||
Dst: &c.json, | ||
Short: 'j', | ||
}) | ||
|
||
return &c | ||
} | ||
|
||
// Exec invokes the application logic for the command. | ||
func (c *DescribeStoreCommand) Exec(_ io.Reader, out io.Writer) error { | ||
if c.Globals.Verbose() && c.json { | ||
return fsterr.ErrInvalidVerboseJSONCombo | ||
} | ||
|
||
o, err := c.Globals.APIClient.GetObjectStore(&c.Input) | ||
if err != nil { | ||
c.Globals.ErrLog.Add(err) | ||
return err | ||
} | ||
|
||
if c.json { | ||
data, err := json.Marshal(o) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = out.Write(data) | ||
if err != nil { | ||
c.Globals.ErrLog.Add(err) | ||
return fmt.Errorf("error: unable to write data to stdout: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
text.PrintObjectStore(out, "", o) | ||
return nil | ||
} |