-
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.
- Loading branch information
Showing
6 changed files
with
240 additions
and
5 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
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 edgedictionaryitem | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
|
||
"github.com/fastly/cli/pkg/common" | ||
"github.com/fastly/cli/pkg/compute/manifest" | ||
"github.com/fastly/cli/pkg/config" | ||
"github.com/fastly/cli/pkg/errors" | ||
"github.com/fastly/cli/pkg/text" | ||
"github.com/fastly/go-fastly/v2/fastly" | ||
) | ||
|
||
// BatchCommand calls the Fastly API to describe a service. | ||
type BatchCommand struct { | ||
common.Base | ||
manifest manifest.Data | ||
Input fastly.BatchModifyDictionaryItemsInput | ||
|
||
file common.OptionalString | ||
} | ||
|
||
// NewBatchCommand returns a usable command registered under the parent. | ||
func NewBatchCommand(parent common.Registerer, globals *config.Data) *BatchCommand { | ||
var c BatchCommand | ||
c.Globals = globals | ||
c.manifest.File.Read(manifest.Filename) | ||
c.CmdClause = parent.Command("batchmodify", "Update multiple items in a Fastly edge dictionary") | ||
c.CmdClause.Flag("service-id", "Service ID").Short('s').StringVar(&c.manifest.Flag.ServiceID) | ||
c.CmdClause.Flag("dictionary-id", "Dictionary ID").Required().StringVar(&c.Input.DictionaryID) | ||
c.CmdClause.Flag("file", "Batch update json file").Required().StringVar(&c.file.Value) | ||
return &c | ||
} | ||
|
||
// Exec invokes the application logic for the command. | ||
func (c *BatchCommand) Exec(in io.Reader, out io.Writer) error { | ||
serviceID, source := c.manifest.ServiceID() | ||
if source == manifest.SourceUndefined { | ||
return errors.ErrNoServiceID | ||
} | ||
c.Input.ServiceID = serviceID | ||
|
||
jsonFile, err := os.Open(c.file.Value) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
jsonBytes, err := ioutil.ReadAll(jsonFile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = json.Unmarshal(jsonBytes, &c.Input) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(c.Input.Items) == 0 { | ||
return fmt.Errorf("item key not found in file %s", c.file.Value) | ||
} | ||
|
||
err = c.Globals.Client.BatchModifyDictionaryItems(&c.Input) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
text.Success(out, "Made %d modifications of Dictionary %s on service %s", len(c.Input.Items), c.Input.DictionaryID, c.Input.ServiceID) | ||
return nil | ||
} |
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