-
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.
* Bump go-fastly to v1.7.0 * Add golang-petname dep * Add NullProgress writer * Add UndoStack to pkg/common * Persist user email to global config file * Make `compute init` accept input interactively and add logic to provision service, domain and backend during initialization. * Surpress gosec G307 on file.Close() defers * Add RunIfError method to UndoStack which unwinds the stack if passed an error * Add text.Description() output helper to print term:description groupings. * Refactor compute init to use new helpers
- Loading branch information
Showing
18 changed files
with
656 additions
and
83 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
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,66 @@ | ||
package common | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
) | ||
|
||
// UndoFn is a function with no arguments which returns an error or nil. | ||
type UndoFn func() error | ||
|
||
// UndoStack models a simple undo stack which consumers can use to store undo | ||
// stateful functions, such as a function to teardown API state if something | ||
// goes wrong during procedural commands, for example deleting a Fastly service | ||
// after it's been created. | ||
type UndoStack struct { | ||
states []UndoFn | ||
} | ||
|
||
// NewUndoStack constructs a new UndoStack. | ||
func NewUndoStack() *UndoStack { | ||
s := make([]UndoFn, 0, 1) | ||
stack := &UndoStack{ | ||
states: s, | ||
} | ||
return stack | ||
} | ||
|
||
// Pop method pops last added UndoFn element oof the stack and returns it. | ||
// If stack is empty Pop() returns nil. | ||
func (s *UndoStack) Pop() UndoFn { | ||
n := len(s.states) | ||
if n == 0 { | ||
return nil | ||
} | ||
v := s.states[n-1] | ||
s.states = s.states[:n-1] | ||
return v | ||
} | ||
|
||
// Push method pushes an Undoer element onto the UndoStack. | ||
func (s *UndoStack) Push(elem UndoFn) { | ||
s.states = append(s.states, elem) | ||
} | ||
|
||
// Len method returns the number of elements in the UndoStack. | ||
func (s *UndoStack) Len() int { | ||
return len(s.states) | ||
} | ||
|
||
// RunIfError unwinds the stack if a non-nil error is passed, by serially | ||
// calling each UndoFn function state in FIFO order. If any UndoFn returns an | ||
// error, it gets logged to the provided writer. Should be deferrerd, such as: | ||
// | ||
// undoStack := common.NewUndoStack() | ||
// defer undoStack.RunIfError(w, err) | ||
// | ||
func (s *UndoStack) RunIfError(w io.Writer, err error) { | ||
if err == nil { | ||
return | ||
} | ||
for i := len(s.states) - 1; i >= 0; i-- { | ||
if err := s.states[i](); err != nil { | ||
fmt.Fprintf(w, "%w", err) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.