Esempio n. 1
0
func set(v semver.Version) {
	err := ioutil.WriteFile(file, []byte(v.String()), os.ModeExclusive)
	if err != nil {
		fmt.Printf("Unable to write version file: %v", err)
		os.Exit(3)
	}
	fmt.Printf("New version: %s", v)
}
func main() {
	if len(os.Args) < 2 {
		println("usage: " + os.Args[0] + " <source>")
		os.Exit(1)
	}

	sources := os.Args[1]

	var request models.OutRequest
	err := json.NewDecoder(os.Stdin).Decode(&request)
	if err != nil {
		fatal("reading request", err)
	}

	driver, err := driver.FromSource(request.Source)
	if err != nil {
		fatal("constructing driver", err)
	}

	var newVersion semver.Version
	if request.Params.File != "" {
		versionFile, err := os.Open(filepath.Join(sources, request.Params.File))
		if err != nil {
			fatal("opening version file", err)
		}

		defer versionFile.Close()

		var versionStr string
		_, err = fmt.Fscanf(versionFile, "%s", &versionStr)
		if err != nil {
			fatal("reading version file", err)
		}

		newVersion, err = semver.Parse(versionStr)
		if err != nil {
			fatal("parsing version", err)
		}

		err = driver.Set(newVersion)
		if err != nil {
			fatal("setting version", err)
		}
	} else if request.Params.Bump != "" || request.Params.Pre != "" {
		bump := version.BumpFromParams(request.Params.Bump, request.Params.Pre)

		newVersion, err = driver.Bump(bump)
		if err != nil {
			fatal("bumping version", err)
		}
	} else {
		println("no version bump specified")
		os.Exit(1)
	}

	outVersion := models.Version{
		Number: newVersion.String(),
	}

	json.NewEncoder(os.Stdout).Encode(models.OutResponse{
		Version: outVersion,
		Metadata: models.Metadata{
			{"number", outVersion.Number},
		},
	})
}
		"":      "1.2.3",
		"final": "1.2.3",
		"patch": "1.2.4",
		"minor": "1.3.0",
		"major": "2.0.0",
	} {
		bumpLocal := bump
		resultLocal := result

		Context(fmt.Sprintf("when bumping %s", bumpLocal), func() {
			BeforeEach(func() {
				bumpParam = bumpLocal
			})

			It("bumps to "+resultLocal, func() {
				Ω(version.String()).Should(Equal(resultLocal))
			})
		})
	}

	Context("when bumping to a prerelease", func() {
		BeforeEach(func() {
			preParam = "rc"
		})

		for bump, result := range map[string]string{
			"":      "1.2.3-rc.1",
			"final": "1.2.3-rc.1",
			"patch": "1.2.4-rc.1",
			"minor": "1.3.0-rc.1",
			"major": "2.0.0-rc.1",