Skip to content

Commit

Permalink
avoid double operation
Browse files Browse the repository at this point in the history
  • Loading branch information
Integralist committed Sep 3, 2021
1 parent 8a59962 commit 4d7eac2
Show file tree
Hide file tree
Showing 21 changed files with 45 additions and 65 deletions.
10 changes: 5 additions & 5 deletions pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,12 @@ type ServiceDetailsOpts struct {

// ServiceDetails returns the Service ID and Service Version.
func ServiceDetails(opts ServiceDetailsOpts) (serviceID string, serviceVersion *fastly.Version, err error) {
serviceID, source := opts.Manifest.ServiceID()

if opts.VerboseMode {
DisplayServiceID(opts.Manifest, opts.Out)
DisplayServiceID(serviceID, source, opts.Out)
}

serviceID, source := opts.Manifest.ServiceID()
if source == manifest.SourceUndefined {
return serviceID, serviceVersion, errors.ErrNoServiceID
}
Expand Down Expand Up @@ -157,10 +158,9 @@ func ServiceDetails(opts ServiceDetailsOpts) (serviceID string, serviceVersion *

// DisplayServiceID acquires the Service ID (if provided) and displays both it
// and its source location.
func DisplayServiceID(d manifest.Data, out io.Writer) {
sid, source := d.ServiceID()
func DisplayServiceID(sid string, s manifest.Source, out io.Writer) {
var via string
switch source {
switch s {
case manifest.SourceFlag:
via = " (via --service-id)"
case manifest.SourceFile:
Expand Down
5 changes: 2 additions & 3 deletions pkg/commands/aclentry/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,10 @@ type CreateCommand struct {

// Exec invokes the application logic for the command.
func (c *CreateCommand) Exec(in io.Reader, out io.Writer) error {
serviceID, source := c.manifest.ServiceID()
if c.Globals.Verbose() {
cmd.DisplayServiceID(c.manifest, out)
cmd.DisplayServiceID(serviceID, source, out)
}

serviceID, source := c.manifest.ServiceID()
if source == manifest.SourceUndefined {
err := errors.ErrNoServiceID
c.Globals.ErrLog.Add(err)
Expand Down
5 changes: 2 additions & 3 deletions pkg/commands/aclentry/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ type DeleteCommand struct {

// Exec invokes the application logic for the command.
func (c *DeleteCommand) Exec(in io.Reader, out io.Writer) error {
serviceID, source := c.manifest.ServiceID()
if c.Globals.Verbose() {
cmd.DisplayServiceID(c.manifest, out)
cmd.DisplayServiceID(serviceID, source, out)
}

serviceID, source := c.manifest.ServiceID()
if source == manifest.SourceUndefined {
err := errors.ErrNoServiceID
c.Globals.ErrLog.Add(err)
Expand Down
5 changes: 2 additions & 3 deletions pkg/commands/aclentry/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ type DescribeCommand struct {

// Exec invokes the application logic for the command.
func (c *DescribeCommand) Exec(in io.Reader, out io.Writer) error {
serviceID, source := c.manifest.ServiceID()
if c.Globals.Verbose() {
cmd.DisplayServiceID(c.manifest, out)
cmd.DisplayServiceID(serviceID, source, out)
}

serviceID, source := c.manifest.ServiceID()
if source == manifest.SourceUndefined {
err := errors.ErrNoServiceID
c.Globals.ErrLog.Add(err)
Expand Down
5 changes: 2 additions & 3 deletions pkg/commands/aclentry/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ type ListCommand struct {

// Exec invokes the application logic for the command.
func (c *ListCommand) Exec(in io.Reader, out io.Writer) error {
serviceID, source := c.manifest.ServiceID()
if c.Globals.Verbose() {
cmd.DisplayServiceID(c.manifest, out)
cmd.DisplayServiceID(serviceID, source, out)
}

serviceID, source := c.manifest.ServiceID()
if source == manifest.SourceUndefined {
err := errors.ErrNoServiceID
c.Globals.ErrLog.Add(err)
Expand Down
5 changes: 2 additions & 3 deletions pkg/commands/aclentry/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,10 @@ type UpdateCommand struct {

// Exec invokes the application logic for the command.
func (c *UpdateCommand) Exec(in io.Reader, out io.Writer) error {
serviceID, source := c.manifest.ServiceID()
if c.Globals.Verbose() {
cmd.DisplayServiceID(c.manifest, out)
cmd.DisplayServiceID(serviceID, source, out)
}

serviceID, source := c.manifest.ServiceID()
if source == manifest.SourceUndefined {
err := errors.ErrNoServiceID
c.Globals.ErrLog.Add(err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/commands/compute/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ func NewDeployCommand(parent cmd.Registerer, client api.HTTPClient, globals *con

// Exec implements the command interface.
func (c *DeployCommand) Exec(in io.Reader, out io.Writer) (err error) {
serviceID, sidSrc := c.Manifest.ServiceID()
if c.Globals.Verbose() {
cmd.DisplayServiceID(c.Manifest, out)
cmd.DisplayServiceID(serviceID, sidSrc, out)
}

// Exit early if no token configured.
Expand Down Expand Up @@ -111,7 +112,6 @@ func (c *DeployCommand) Exec(in io.Reader, out io.Writer) (err error) {
serviceVersion *fastly.Version
)

serviceID, sidSrc := c.Manifest.ServiceID()
if sidSrc == manifest.SourceUndefined {
newService = true
serviceID, serviceVersion, err = manageNoServiceIDFlow(c.AcceptDefaults, in, out, verbose, apiClient, pkgName, errLog, &c.Manifest.File)
Expand Down
5 changes: 2 additions & 3 deletions pkg/commands/edgedictionaryitem/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@ func NewBatchCommand(parent cmd.Registerer, globals *config.Data) *BatchCommand

// 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 c.Globals.Verbose() {
cmd.DisplayServiceID(c.manifest, out)
cmd.DisplayServiceID(serviceID, source, out)
}

serviceID, source := c.manifest.ServiceID()
if source == manifest.SourceUndefined {
return errors.ErrNoServiceID
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/commands/edgedictionaryitem/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ func NewCreateCommand(parent cmd.Registerer, globals *config.Data) *CreateComman

// Exec invokes the application logic for the command.
func (c *CreateCommand) Exec(in io.Reader, out io.Writer) error {
serviceID, source := c.manifest.ServiceID()
if c.Globals.Verbose() {
cmd.DisplayServiceID(c.manifest, out)
cmd.DisplayServiceID(serviceID, source, out)
}

serviceID, source := c.manifest.ServiceID()
if source == manifest.SourceUndefined {
return errors.ErrNoServiceID
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/commands/edgedictionaryitem/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ func NewDeleteCommand(parent cmd.Registerer, globals *config.Data) *DeleteComman

// Exec invokes the application logic for the command.
func (c *DeleteCommand) Exec(in io.Reader, out io.Writer) error {
serviceID, source := c.manifest.ServiceID()
if c.Globals.Verbose() {
cmd.DisplayServiceID(c.manifest, out)
cmd.DisplayServiceID(serviceID, source, out)
}

serviceID, source := c.manifest.ServiceID()
if source == manifest.SourceUndefined {
return errors.ErrNoServiceID
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/commands/edgedictionaryitem/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ func NewDescribeCommand(parent cmd.Registerer, globals *config.Data) *DescribeCo

// Exec invokes the application logic for the command.
func (c *DescribeCommand) Exec(in io.Reader, out io.Writer) error {
serviceID, source := c.manifest.ServiceID()
if c.Globals.Verbose() {
cmd.DisplayServiceID(c.manifest, out)
cmd.DisplayServiceID(serviceID, source, out)
}

serviceID, source := c.manifest.ServiceID()
if source == manifest.SourceUndefined {
return errors.ErrNoServiceID
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/commands/edgedictionaryitem/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ func NewListCommand(parent cmd.Registerer, globals *config.Data) *ListCommand {

// Exec invokes the application logic for the command.
func (c *ListCommand) Exec(in io.Reader, out io.Writer) error {
serviceID, source := c.manifest.ServiceID()
if c.Globals.Verbose() {
cmd.DisplayServiceID(c.manifest, out)
cmd.DisplayServiceID(serviceID, source, out)
}

serviceID, source := c.manifest.ServiceID()
if source == manifest.SourceUndefined {
return errors.ErrNoServiceID
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/commands/edgedictionaryitem/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ func NewUpdateCommand(parent cmd.Registerer, globals *config.Data) *UpdateComman

// Exec invokes the application logic for the command.
func (c *UpdateCommand) Exec(in io.Reader, out io.Writer) error {
serviceID, source := c.manifest.ServiceID()
if c.Globals.Verbose() {
cmd.DisplayServiceID(c.manifest, out)
cmd.DisplayServiceID(serviceID, source, out)
}

serviceID, source := c.manifest.ServiceID()
if source == manifest.SourceUndefined {
return errors.ErrNoServiceID
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/commands/logs/tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,10 @@ func NewTailCommand(parent cmd.Registerer, globals *config.Data) *TailCommand {

// Exec invokes the application logic for the command.
func (c *TailCommand) Exec(in io.Reader, out io.Writer) error {
serviceID, source := c.manifest.ServiceID()
if c.Globals.Verbose() {
cmd.DisplayServiceID(c.manifest, out)
cmd.DisplayServiceID(serviceID, source, out)
}

serviceID, source := c.manifest.ServiceID()
if source == manifest.SourceUndefined {
return errors.ErrNoServiceID
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/commands/purge/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ type RootCommand struct {

// Exec implements the command interface.
func (c *RootCommand) Exec(in io.Reader, out io.Writer) error {
serviceID, source := c.manifest.ServiceID()
if c.Globals.Verbose() {
cmd.DisplayServiceID(c.manifest, out)
cmd.DisplayServiceID(serviceID, source, out)
}

// Exit early if no token configured.
Expand All @@ -61,10 +62,7 @@ func (c *RootCommand) Exec(in io.Reader, out io.Writer) error {
}

// The URL purge API call doesn't require a Service ID.
var serviceID string
var source manifest.Source
if c.url == "" {
serviceID, source = c.manifest.ServiceID()
if source == manifest.SourceUndefined {
return errors.ErrNoServiceID
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/commands/service/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ func NewDeleteCommand(parent cmd.Registerer, globals *config.Data) *DeleteComman

// Exec invokes the application logic for the command.
func (c *DeleteCommand) Exec(in io.Reader, out io.Writer) error {
serviceID, source := c.manifest.ServiceID()
if c.Globals.Verbose() {
cmd.DisplayServiceID(c.manifest, out)
cmd.DisplayServiceID(serviceID, source, out)
}

serviceID, source := c.manifest.ServiceID()
if source == manifest.SourceUndefined {
return errors.ErrNoServiceID
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/commands/service/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ func NewDescribeCommand(parent cmd.Registerer, globals *config.Data) *DescribeCo

// Exec invokes the application logic for the command.
func (c *DescribeCommand) Exec(in io.Reader, out io.Writer) error {
serviceID, source := c.manifest.ServiceID()
if c.Globals.Verbose() {
cmd.DisplayServiceID(c.manifest, out)
cmd.DisplayServiceID(serviceID, source, out)
}

serviceID, source := c.manifest.ServiceID()
if source == manifest.SourceUndefined {
return errors.ErrNoServiceID
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/commands/service/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ func NewUpdateCommand(parent cmd.Registerer, globals *config.Data) *UpdateComman

// Exec invokes the application logic for the command.
func (c *UpdateCommand) Exec(in io.Reader, out io.Writer) error {
serviceID, source := c.manifest.ServiceID()
if c.Globals.Verbose() {
cmd.DisplayServiceID(c.manifest, out)
cmd.DisplayServiceID(serviceID, source, out)
}

serviceID, source := c.manifest.ServiceID()
if source == manifest.SourceUndefined {
return errors.ErrNoServiceID
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/commands/serviceversion/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ func NewListCommand(parent cmd.Registerer, globals *config.Data) *ListCommand {

// Exec invokes the application logic for the command.
func (c *ListCommand) Exec(in io.Reader, out io.Writer) error {
serviceID, source := c.manifest.ServiceID()
if c.Globals.Verbose() {
cmd.DisplayServiceID(c.manifest, out)
cmd.DisplayServiceID(serviceID, source, out)
}

serviceID, source := c.manifest.ServiceID()
if source == manifest.SourceUndefined {
return errors.ErrNoServiceID
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/commands/stats/historical.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ func NewHistoricalCommand(parent cmd.Registerer, globals *config.Data) *Historic

// Exec implements the command interface.
func (c *HistoricalCommand) Exec(in io.Reader, out io.Writer) error {
serviceID, source := c.manifest.ServiceID()
if c.Globals.Verbose() {
cmd.DisplayServiceID(c.manifest, out)
cmd.DisplayServiceID(serviceID, source, out)
}

serviceID, source := c.manifest.ServiceID()
if source == manifest.SourceUndefined {
return errors.ErrNoServiceID
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/commands/stats/realtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ func NewRealtimeCommand(parent cmd.Registerer, globals *config.Data) *RealtimeCo

// Exec implements the command interface.
func (c *RealtimeCommand) Exec(in io.Reader, out io.Writer) error {
serviceID, source := c.manifest.ServiceID()
if c.Globals.Verbose() {
cmd.DisplayServiceID(c.manifest, out)
cmd.DisplayServiceID(serviceID, source, out)
}

serviceID, source := c.manifest.ServiceID()
if source == manifest.SourceUndefined {
return errors.ErrNoServiceID
}
Expand Down

0 comments on commit 4d7eac2

Please sign in to comment.