Skip to content

Commit

Permalink
Add --skip-pull and git-plan command
Browse files Browse the repository at this point in the history
- Use `--skip-pull` to forcefully skip the pull step using a git plan
- Use `git-plan` command to do git commands inside the plan directory
  • Loading branch information
emilingerslev committed Oct 9, 2018
1 parent acdb44c commit 0773065
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 12 deletions.
24 changes: 24 additions & 0 deletions cmd/git-plan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cmd

import (
"strings"

"github.com/lunarway/shuttle/pkg/git"
"github.com/spf13/cobra"
)

var (
gitPlanCmd = &cobra.Command{
Use: "git-plan [...git_args]",
Short: "Run a git command for the plan",
Run: func(cmd *cobra.Command, args []string) {
skipGitPlanPulling = true
context := getProjectContext()
git.RunGitPlanCommand(strings.Join(args, " "), context.LocalPlanPath, context.UI)
},
}
)

func init() {
rootCmd.AddCommand(gitPlanCmd)
}
16 changes: 9 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import (
)

var (
projectPath string
uii ui.UI
verboseFlag bool
clean bool
version = "<dev-version>"
commit = "<unspecified-commit>"
projectPath string
uii ui.UI
verboseFlag bool
clean bool
skipGitPlanPulling bool
version = "<dev-version>"
commit = "<unspecified-commit>"
)

// rootCmd represents the base command when called without any subcommands
Expand Down Expand Up @@ -54,6 +55,7 @@ func Execute() {
func init() {
rootCmd.PersistentFlags().StringVarP(&projectPath, "project", "p", ".", "Project path")
rootCmd.PersistentFlags().BoolVarP(&clean, "clean", "c", false, "Start from clean setup")
rootCmd.PersistentFlags().BoolVar(&skipGitPlanPulling, "skip-pull", false, "Skip git plan pulling step")
rootCmd.PersistentFlags().BoolVarP(&verboseFlag, "verbose", "v", false, "Print verbose output")
}

Expand All @@ -66,6 +68,6 @@ func getProjectContext() config.ShuttleProjectContext {

var fullProjectPath = path.Join(dir, projectPath)
var c config.ShuttleProjectContext
c.Setup(fullProjectPath, uii, clean)
c.Setup(fullProjectPath, uii, clean, skipGitPlanPulling)
return c
}
4 changes: 2 additions & 2 deletions pkg/config/shuttleconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type ShuttleProjectContext struct {
}

// Setup the ShuttleProjectContext for a specific path
func (c *ShuttleProjectContext) Setup(projectPath string, uii ui.UI, clean bool) *ShuttleProjectContext {
func (c *ShuttleProjectContext) Setup(projectPath string, uii ui.UI, clean bool, skipGitPlanPulling bool) *ShuttleProjectContext {
c.Config.getConf(projectPath)
c.UI = uii
c.ProjectPath = projectPath
Expand All @@ -47,7 +47,7 @@ func (c *ShuttleProjectContext) Setup(projectPath string, uii ui.UI, clean bool)
os.MkdirAll(c.LocalShuttleDirectoryPath, os.ModePerm)

c.TempDirectoryPath = path.Join(c.LocalShuttleDirectoryPath, "temp")
c.LocalPlanPath = FetchPlan(c.Config.Plan, projectPath, c.LocalShuttleDirectoryPath, uii)
c.LocalPlanPath = FetchPlan(c.Config.Plan, projectPath, c.LocalShuttleDirectoryPath, uii, skipGitPlanPulling)
c.Plan.Load(c.LocalPlanPath)
c.Scripts = make(map[string]ShuttlePlanScript)
for scriptName, script := range c.Plan.Scripts {
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/shuttleplan.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ func (p *ShuttlePlanConfiguration) Load(planPath string) *ShuttlePlanConfigurati
}

// FetchPlan so it exists locally and return path to that plan
func FetchPlan(plan string, projectPath string, localShuttleDirectoryPath string, uii ui.UI) string {
func FetchPlan(plan string, projectPath string, localShuttleDirectoryPath string, uii ui.UI, skipGitPlanPulling bool) string {
switch {
case plan == "":
uii.VerboseLn("Using no plan")
return ""
case git.IsGitPlan(plan):
uii.VerboseLn("Using git plan at '%s'", plan)
return git.GetGitPlan(plan, localShuttleDirectoryPath, uii)
return git.GetGitPlan(plan, localShuttleDirectoryPath, uii, skipGitPlanPulling)
case isMatching("^http://|^https://", plan):
panic("plan not valid: http is not supported yet")
case isFilePath(plan, true):
Expand Down
30 changes: 29 additions & 1 deletion pkg/git/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func IsGitPlan(plan string) bool {
}

// GetGitPlan will pull git repository and return its path
func GetGitPlan(plan string, localShuttleDirectoryPath string, uii ui.UI) string {
func GetGitPlan(plan string, localShuttleDirectoryPath string, uii ui.UI, skipGitPlanPulling bool) string {
parsedGitPlan := parseGitPlan(plan)
planPath := path.Join(localShuttleDirectoryPath, "plan")

Expand All @@ -77,6 +77,11 @@ func GetGitPlan(plan string, localShuttleDirectoryPath string, uii ui.UI) string
}

if fileAvailable(planPath) {
if skipGitPlanPulling {
uii.VerboseLn("Skipping git plan pulling")
return planPath
}

status := getStatus(planPath)

if status.mergeState {
Expand All @@ -88,6 +93,7 @@ func GetGitPlan(plan string, localShuttleDirectoryPath string, uii ui.UI) string
uii.InfoLn("Pulling latest plan changes")
gitCmd("pull origin", planPath, uii)
}
return planPath
} else {
os.MkdirAll(localShuttleDirectoryPath, os.ModePerm)

Expand All @@ -107,6 +113,28 @@ func GetGitPlan(plan string, localShuttleDirectoryPath string, uii ui.UI) string
return planPath
}

func RunGitPlanCommand(command string, plan string, uii ui.UI) {
cmdOptions := go_cmd.Options{
Streaming: true,
}
execCmd := go_cmd.NewCmdOptions(cmdOptions, "sh", "-c", "cd '"+plan+"'; git "+command)
execCmd.Env = os.Environ()
go func() {
for {
select {
case line := <-execCmd.Stdout:
uii.InfoLn(line)
case line := <-execCmd.Stderr:
uii.InfoLn(line)
}
}
}()
status := <-execCmd.Start()
if status.Exit > 0 {
os.Exit(status.Exit)
}
}

func isMatching(r string, content string) bool {
match, err := regexp.MatchString(r, content)
if err != nil {
Expand Down

0 comments on commit 0773065

Please sign in to comment.