-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLI: Refactor the temp directories in the commands (#180)
* Add yaml processor * Fix mod files * Pass pr by value to preview function * Add new subcommand for target repos * Add a cmd utils package * Move temp directory helpers to the cmd utils * Deprecate the console error functions * Use the cmd utils to exit on error * Fix how the deferred function is handling cleanup * Add command to prepare GBM release (#158) * Add base gbm prepare command * Add CloneGBM function to git pkg * Add git submodule utility functions * Update gbm.go * Add Submodule to git client * Update gbm.go steps * Add UpdateReleaseNotes util * Add Bundle exec command * Add GBM PR body template * Add steps to preview and create Gutenberg Mobile PR * Add code comments to gbm prepare command * Call CreateGbmPR from gbm release subcommand * Add shell package for shell based commands inludes git, npm, and bundler * Update gb.go to use the new shell commands * Update gbm release package * Add SetUpstreamTo and AddRemote git interfaces * Add remote and set upstream to GBM prepare command * Place the success message after verifying the PR was created * Add prepare cmd utils * Update SetUpstreamTo signature * Reorder some events and clean up logging messages * Add a cmd utils package * Move temp directory helpers to the cmd utils * Deprecate the console error functions * Use the cmd utils to exit on error * Fix how the deferred function is handling cleanup * Add git submodule utility functions * Add Submodule to git client * Call CreateGbmPR from gbm release subcommand * Update gb.go to use the new shell commands * Update exit on prepare gbm command --------- Co-authored-by: jhnstn <[email protected]> Co-authored-by: Jason Johnston <[email protected]> * Make shell command interfaces public Also rename factory functions to add clarity * Work in progress * add dir to the try exec command * Update yq test to handle dots in values * Update integration package to return the pr * Fill in the create pr flows to the integrate cmd * Update integrate to use the new exit flow * Get Android PR working * Add keep flag to keep the temp directory around after the command * Add cleaner factory function * Add rake to the shell package * get integrate working for ios * Add workspace package for managing the temp directories * update commands to use the new workspace package * Refactor workspace into an interface --------- Co-authored-by: Derek Blank <[email protected]>
- Loading branch information
1 parent
a3e5efd
commit c7d178f
Showing
10 changed files
with
157 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
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,92 @@ | ||
package workspace | ||
|
||
import ( | ||
"os" | ||
"os/signal" | ||
"path" | ||
|
||
"github.com/wordpress-mobile/gbm-cli/pkg/console" | ||
) | ||
|
||
type Workspace interface { | ||
Cleanup() | ||
Dir() string | ||
Keep() | ||
create() error | ||
setCleaner() | ||
} | ||
|
||
type workspace struct { | ||
dir string | ||
keep bool | ||
prefix string | ||
cleaner func() | ||
disabled bool | ||
} | ||
|
||
func NewWorkspace() (Workspace, error) { | ||
w := &workspace{prefix: "gbm-"} | ||
if _, noWorkspace := os.LookupEnv("GBM_NO_WORKSPACE"); noWorkspace { | ||
console.Info("GBM_NO_WORKSPACE is set, not creating a workspace directory") | ||
w.disabled = true | ||
} | ||
if err := w.create(); err != nil { | ||
return nil, err | ||
} | ||
w.setCleaner() | ||
return w, nil | ||
} | ||
|
||
func (w *workspace) create() error { | ||
// if we're disabled, don't create a temp directory | ||
if w.disabled { | ||
return nil | ||
} | ||
tempDir, err := os.MkdirTemp("", w.prefix) | ||
if err != nil { | ||
return err | ||
} | ||
w.dir = tempDir | ||
return nil | ||
} | ||
|
||
func (w *workspace) setCleaner() { | ||
w.cleaner = func() { | ||
if w.disabled || w.dir == "" { | ||
return | ||
} | ||
|
||
if w.keep { | ||
console.Info("Keeping temporary directory %s", w.dir) | ||
return | ||
} | ||
|
||
if err := os.RemoveAll(w.dir); err != nil { | ||
console.Error(err) | ||
} | ||
} | ||
// register a listener for ^C, call the cleanup function | ||
go func() { | ||
sigchan := make(chan os.Signal, 1) | ||
signal.Notify(sigchan, os.Interrupt) | ||
<-sigchan // wait for ^C | ||
w.cleaner() | ||
os.Exit(1) | ||
}() | ||
} | ||
|
||
func (w *workspace) Dir() string { | ||
// if the workspace is disabled, return the current directory | ||
if w.disabled { | ||
return path.Base(".") | ||
} | ||
return w.dir | ||
} | ||
|
||
func (w *workspace) Keep() { | ||
w.keep = true | ||
} | ||
|
||
func (w *workspace) Cleanup() { | ||
w.cleaner() | ||
} |
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