It("returns new versions", func() {
			allVersions := []string{"newest version", "newish version", "oldest version"}
			versions, _ := versions.Since(allVersions, "newish version")

			Expect(versions).To(Equal([]string{"newest version"}))
		})

		Context("when the versions are not ordered", func() {
			var allVersions []string

			BeforeEach(func() {
				allVersions = []string{"aaa", "ddd", "eee", "bbb", "fff", "ccc"}
			})

			It("returns new versions", func() {
				versions, _ := versions.Since(allVersions, "eee")

				Expect(versions).To(Equal([]string{"aaa", "ddd"}))
			})
		})
	})

	Describe("Reverse", func() {
		It("returns reversed ordered versions because concourse expects them that way", func() {
			versions, _ := versions.Reverse([]string{"v201", "v178", "v120", "v200"})

			Expect(versions).To(Equal([]string{"v200", "v120", "v178", "v201"}))
		})
	})
})
Example #2
0
func main() {
	if version == "" {
		version = "dev"
	}

	var input concourse.CheckRequest

	logFile, err := ioutil.TempFile("", "pivnet-resource-check.log")
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Fprintf(logFile, "PivNet Resource version: %s\n", version)

	fmt.Fprintf(os.Stderr, "Logging to %s\n", logFile.Name())

	err = json.NewDecoder(os.Stdin).Decode(&input)
	if err != nil {
		fmt.Fprintf(logFile, "Exiting with error: %v\n", err)
		log.Fatalln(err)
	}

	sanitized := concourse.SanitizedSource(input.Source)
	sanitizer := sanitizer.NewSanitizer(sanitized, logFile)

	l = logger.NewLogger(sanitizer)

	logDir := filepath.Dir(logFile.Name())
	existingLogFiles, err := filepath.Glob(filepath.Join(logDir, "pivnet-resource-check.log*"))
	if err != nil {
		l.Debugf("Exiting with error: %v\n", err)
		log.Fatalln(err)
	}

	for _, f := range existingLogFiles {
		if filepath.Base(f) != filepath.Base(logFile.Name()) {
			l.Debugf("Removing existing log file: %s\n", f)
			err := os.Remove(f)
			if err != nil {
				l.Debugf("Exiting with error: %v\n", err)
				log.Fatalln(err)
			}
		}
	}

	mustBeNonEmpty(input.Source.APIToken, "api_token")
	mustBeNonEmpty(input.Source.ProductSlug, "product_slug")

	l.Debugf("Received input: %+v\n", input)

	clientConfig := pivnet.NewClientConfig{
		URL:       pivnet.URL,
		Token:     input.Source.APIToken,
		UserAgent: fmt.Sprintf("pivnet-resource/%s", version),
	}
	client := pivnet.NewClient(
		clientConfig,
		l,
	)

	l.Debugf("Getting all product versions\n")

	allVersions, err := client.ProductVersions(input.Source.ProductSlug)
	if err != nil {
		l.Debugf("Exiting with error: %v\n", err)
		log.Fatalln(err)
	}

	l.Debugf("All known versions: %+v\n", allVersions)

	newVersions, err := versions.Since(allVersions, input.Version.ProductVersion)
	if err != nil {
		l.Debugf("Exiting with error: %v\n", err)
		log.Fatalln(err)
	}

	l.Debugf("New versions: %+v\n", newVersions)

	reversedVersions, err := versions.Reverse(newVersions)
	if err != nil {
		l.Debugf("Exiting with error: %v\n", err)
		log.Fatalln(err)
	}

	var out concourse.CheckResponse
	for _, v := range reversedVersions {
		out = append(out, concourse.Version{ProductVersion: v})
	}

	if len(out) == 0 {
		out = append(out, concourse.Version{ProductVersion: allVersions[0]})
	}

	l.Debugf("Returning output: %+v\n", out)

	err = json.NewEncoder(os.Stdout).Encode(out)
	if err != nil {
		l.Debugf("Exiting with error: %v\n", err)
		log.Fatalln(err)
	}
}