This repository has been archived by the owner on Apr 2, 2019. It is now read-only.
/
reap.go
107 lines (91 loc) · 2.9 KB
/
reap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package cli
import (
"fmt"
"os"
"os/exec"
log "github.com/Sirupsen/logrus"
"github.com/bitrise-io/go-utils/colorstring"
"github.com/bitrise-io/go-utils/pathutil"
"github.com/bitrise-io/garden/config"
"github.com/codegangsta/cli"
)
// ReapCommandParams ...
type ReapCommandParams struct {
Command string
CommandArgs []string
}
func reapThisPlant(plantID string, gardenMap config.GardenMapModel, cmdParams ReapCommandParams) error {
plant, isFound := gardenMap.Plants[plantID]
if !isFound {
return fmt.Errorf("reapThisPlant: can't find Plant with ID: %s", plantID)
}
expandedPlantPath := plant.ExpandedPath(plantID)
absPlantDirPath, err := pathutil.AbsPath(expandedPlantPath)
if err != nil {
return fmt.Errorf("Failed to get Absolute Path of Plant (path:%s), error: %s", expandedPlantPath, err)
}
cmd := exec.Command(cmdParams.Command, cmdParams.CommandArgs...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// envs
envsToAdd := []string{}
// defaults
envsToAdd = append(envsToAdd, fmt.Sprintf("_GARDEN_PLANT_PATH=%s", absPlantDirPath))
envsToAdd = append(envsToAdd, fmt.Sprintf("_GARDEN_PLANT_ID=%s", plantID))
// Vars
allPlantVars, err := gardenMap.CollectAllVarsForPlant(plantID)
if err != nil {
return fmt.Errorf("reapThisPlant: failed to collect Vars for Plant (id: %s), error: %s", plantID, err)
}
log.Debugf("allPlantVars: %#v", allPlantVars)
for key, val := range allPlantVars {
envsToAdd = append(envsToAdd, fmt.Sprintf("_GARDENVAR_%s=%s", key, val))
}
cmd.Env = append(os.Environ(), envsToAdd...)
return cmd.Run()
}
func reapPlants(plantIDs []string, gardenMap config.GardenMapModel, cmdParams ReapCommandParams) error {
for _, plantID := range plantIDs {
fmt.Println()
log.Infof("🚜 -> Reaping plant: %s", colorstring.Green(plantID))
if err := reapThisPlant(plantID, gardenMap, cmdParams); err != nil {
return fmt.Errorf("Failed to reap plant (id:%s), error: %s", plantID, err)
}
}
return nil
}
func printPlantsReadyForReap(plantsToReapIDs []string) {
fmt.Println()
log.Infof(colorstring.Blue("Plants ready to reap:"))
for _, aPlantID := range plantsToReapIDs {
log.Printf(" * %s", colorstring.Green(aPlantID))
}
fmt.Println()
}
func reap(c *cli.Context) {
log.Infoln("Reap")
args := c.Args()
log.Infof("args: %#v", args)
if len(args) < 1 {
log.Fatalln("No command to execute, can't reap.")
}
cmdParams := ReapCommandParams{
Command: args[0],
}
if len(args) > 1 {
cmdParams.CommandArgs = args[1:]
}
gardenMap, _, err := config.LoadGardenMap("")
if err != nil {
log.Fatalf("Failed to load Garden Map: %s", err)
}
plantsToGrowIDs := gardenMap.FilteredPlantsIDs(WorkWithPlantID, WorkWithZone)
if len(plantsToGrowIDs) < 1 {
log.Fatalln("No plants to grow!")
}
printPlantsReadyForReap(plantsToGrowIDs)
if err := reapPlants(plantsToGrowIDs, gardenMap, cmdParams); err != nil {
log.Fatalf("Failed to grow plants: %s", err)
}
}