|
| 1 | +package cf |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "path/filepath" |
| 7 | + "strconv" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/onsi/ginkgo" |
| 11 | + "github.com/onsi/gomega/gexec" |
| 12 | + "gopkg.in/yaml.v2" |
| 13 | +) |
| 14 | + |
| 15 | +type Manifest struct { |
| 16 | + Applications []Application |
| 17 | +} |
| 18 | + |
| 19 | +type Application struct { |
| 20 | + Buildpacks []string `yaml:omitempty` |
| 21 | + Command string `yaml:omitempty` |
| 22 | + Instances int `yaml:omitempty` |
| 23 | + Memory string `yaml:omitempty` |
| 24 | + Name string |
| 25 | + Path string `yaml:omitempty` |
| 26 | + Routes []map[string]string `yaml:omitempty` |
| 27 | +} |
| 28 | + |
| 29 | +var Push = func(appName string, args ...string) *gexec.Session { |
| 30 | + tmpDir, err := ioutil.TempDir("", "") |
| 31 | + if err != nil { |
| 32 | + panic(err) |
| 33 | + } |
| 34 | + |
| 35 | + app := Application{ |
| 36 | + Name: appName, |
| 37 | + } |
| 38 | + |
| 39 | + for i := 0; i < len(args); i += 2 { |
| 40 | + switch args[i] { |
| 41 | + case "-b": |
| 42 | + app.Buildpacks = append(app.Buildpacks, args[i+1]) |
| 43 | + case "-c": |
| 44 | + app.Command = args[i+1] |
| 45 | + case "-d": |
| 46 | + app.Routes = append(app.Routes, map[string]string{"route": fmt.Sprintf("%s.%s", appName, args[i+1])}) |
| 47 | + case "-i": |
| 48 | + instances, err := strconv.Atoi(args[i+1]) |
| 49 | + if err != nil { |
| 50 | + panic(err) |
| 51 | + } |
| 52 | + app.Instances = instances |
| 53 | + case "-m": |
| 54 | + app.Memory = args[i+1] |
| 55 | + case "-p": |
| 56 | + path, err := filepath.Abs(args[i+1]) |
| 57 | + if err != nil { |
| 58 | + panic(err) |
| 59 | + } |
| 60 | + app.Path = path |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + manifest := Manifest{} |
| 65 | + |
| 66 | + manifest.Applications = append(manifest.Applications, app) |
| 67 | + |
| 68 | + manifestText, err := yaml.Marshal(manifest) |
| 69 | + if err != nil { |
| 70 | + panic(err) |
| 71 | + } |
| 72 | + |
| 73 | + manifestPath := filepath.Join(tmpDir, "manifest.yml") |
| 74 | + err = ioutil.WriteFile(manifestPath, manifestText, 0644) |
| 75 | + if err != nil { |
| 76 | + panic(err) |
| 77 | + } |
| 78 | + |
| 79 | + fmt.Fprintf( |
| 80 | + ginkgo.GinkgoWriter, |
| 81 | + "\n[%s]> Generated app manifest:\n%s\n", |
| 82 | + time.Now().UTC().Format("2006-01-02 15:04:05.00 (MST)"), |
| 83 | + string(manifestText), |
| 84 | + ) |
| 85 | + |
| 86 | + return Cf("push", |
| 87 | + "-f", manifestPath, |
| 88 | + ) |
| 89 | +} |
0 commit comments