File promu-0.17.0.obscpio of Package golang-github-prometheus-promu

07070100000000000041ED0000000000000000000000026623999300000000000000000000000000000000000000000000001700000000promu-0.17.0/.circleci07070100000001000081A4000000000000000000000001662399930000077A000000000000000000000000000000000000002200000000promu-0.17.0/.circleci/config.yml---
version: 2.1

executors:
  # Whenever the Go version is updated here, .promu.yml should also be updated.
  golang:
    docker:
    - image: cimg/go:1.22

jobs:
  test:
    executor: golang

    steps:
    - setup_remote_docker
    - checkout
    - run: go build -o promu-intermediate
    - run: make check_license style unused lint test build PROMU=./promu-intermediate
    - store_artifacts:
        path: promu
        destination: /build/promu
    - run: rm -v promu promu-intermediate
    - run: git diff --exit-code

  build:
    executor: golang
    environment:
        PROMU: ./promu-intermediate
    working_directory: /home/circleci/.go_workspace/src/github.com/prometheus/promu
    parallelism: 5

    steps:
    - setup_remote_docker
    - checkout
    - run: go build -o promu-intermediate
    - run: ./promu-intermediate crossbuild -v --parallelism $CIRCLE_NODE_TOTAL --parallelism-thread $CIRCLE_NODE_INDEX
    - persist_to_workspace:
        root: .
        paths:
        - .build
    - store_artifacts:
        path: .build
        destination: /build

  release_tags:
    executor: golang
    environment:
        PROMU: ./promu-intermediate

    steps:
    - checkout
    - attach_workspace:
        at: .
    - run: go build -o promu-intermediate
    - run: ./promu-intermediate crossbuild tarballs
    - run: ./promu-intermediate checksum .tarballs
    - run: ./promu-intermediate release .tarballs
    - store_artifacts:
        path: .tarballs
        destination: releases

workflows:
  version: 2
  promu:
    jobs:
    - test:
        filters:
          tags:
            only: /.*/
    - build:
        filters:
          tags:
            only: /.*/
    - release_tags:
        context: org-context
        requires:
        - test
        - build
        filters:
          tags:
            only: /^v[0-9]+(\.[0-9]+){2}(-.+|[^-.]*)$/
          branches:
            ignore: /.*/
07070100000002000041ED0000000000000000000000026623999300000000000000000000000000000000000000000000001500000000promu-0.17.0/.github07070100000003000081A4000000000000000000000001662399930000006E000000000000000000000000000000000000002400000000promu-0.17.0/.github/dependabot.ymlversion: 2
updates:
  - package-ecosystem: "gomod"
    directory: "/"
    schedule:
      interval: "monthly"
07070100000004000041ED0000000000000000000000026623999300000000000000000000000000000000000000000000001F00000000promu-0.17.0/.github/workflows07070100000005000081A400000000000000000000000166239993000004CB000000000000000000000000000000000000003100000000promu-0.17.0/.github/workflows/golangci-lint.yml---
# This action is synced from https://github.com/prometheus/prometheus
name: golangci-lint
on:
  push:
    paths:
      - "go.sum"
      - "go.mod"
      - "**.go"
      - "scripts/errcheck_excludes.txt"
      - ".github/workflows/golangci-lint.yml"
      - ".golangci.yml"
  pull_request:

permissions:  # added using https://github.com/step-security/secure-repo
  contents: read

jobs:
  golangci:
    permissions:
      contents: read  # for actions/checkout to fetch code
      pull-requests: read  # for golangci/golangci-lint-action to fetch pull requests
    name: lint
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
      - name: install Go
        uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
        with:
          go-version: 1.22.x
      - name: Install snmp_exporter/generator dependencies
        run: sudo apt-get update && sudo apt-get -y install libsnmp-dev
        if: github.repository == 'prometheus/snmp_exporter'
      - name: Lint
        uses: golangci/golangci-lint-action@3cfe3a4abbb849e10058ce4af15d205b6da42804 # v4.0.0
        with:
          version: v1.56.2
07070100000006000081A4000000000000000000000001662399930000002B000000000000000000000000000000000000001800000000promu-0.17.0/.gitignore/promu
.build
.tarballs
testoutput
/vendor
07070100000007000081A400000000000000000000000166239993000001ED000000000000000000000000000000000000001B00000000promu-0.17.0/.golangci.ymlissues:
  max-issues-per-linter: 0
  max-same-issues: 0

linters:
  disable:
    - errcheck
    - unused
  enable:
    - errorlint
    - gocritic
    - gofumpt
    - goimports
    - govet
    - revive
    - staticcheck

linters-settings:
  goimports:
    local-prefixes: github.com/prometheus/promu
  revive:
    rules:
      # https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#unused-parameter
      - name: unused-parameter
        severity: warning
        disabled: true
07070100000008000081A400000000000000000000000166239993000002BC000000000000000000000000000000000000001800000000promu-0.17.0/.promu.ymlgo:
    # Whenever the Go version is updated here,
    # .circle/config.yml should also be updated.
    version: 1.22
repository:
    path: github.com/prometheus/promu
build:
    tags:
      all: [netgo, static_build]
      windows: [static_build]
    ldflags: |
        -s
        -X github.com/prometheus/common/version.Version={{.Version}}
        -X github.com/prometheus/common/version.Revision={{.Revision}}
        -X github.com/prometheus/common/version.Branch={{.Branch}}
        -X github.com/prometheus/common/version.BuildUser={{user}}@{{host}}
        -X github.com/prometheus/common/version.BuildDate={{date "20060102-15:04:05"}}
tarball:
    files:
        - LICENSE
        - NOTICE

07070100000009000081A40000000000000000000000016623999300000C17000000000000000000000000000000000000001A00000000promu-0.17.0/CHANGELOG.md## 0.17.0 / 2024-04-14

* [FEATURE] Add codesign utility function #284

## 0.16.0 / 2024-04-02

* [FEATURE] Do not discover user/host for reproducible builds #267
* [BUGFIX] Fix example/prometheus build error #274

## 0.15.0 / 2023-07-04

* [CHANGE] Use unmarshalstrict #240
* [CHANGE] Add linux/riscv64 to default platforms #254

## 0.14.0 / 2022-12-09

* [FEATURE] Add the ability to override tags per GOOS

## 0.13.0 / 2021-11-06

* [ENHANCEMENT] Add windows/arm64 platform #225

## 0.12.0 / 2021-04-12

* [CHANGE] Unified CGO crossbuild image #219

## 0.11.1 / 2021-03-20

* [BUGFIX] Deduplicate platforms when two regexes match the same platform #214
* [BUGFIX] Regexes are evaluated against all archs, and don't stop at the first match #214

## 0.11.0 / 2021-03-20

* [FEATURE] Add the ability to run parallel build threads independently #212

## 0.10.0 / 2021-03-17

* [FEATURE] Add parallel crossbuilds #208

## 0.9.0 / 2021-03-16

Note: promu crossbuild --platform flag is now a regexp. To
      use multiple options, the flag can be repeated.

* [CHANGE] Use regexp for crossbuild platforms #204

## 0.8.1 / 2021-03-12

This release is cut to publish `darwin/arm64` and `illumos/amd64` binaries of
promu.

* [ENHANCEMENT] Promu is now built from its default branch instead of a released
  binary #205

## 0.8.0 / 2021-03-11

Note: The default build now requires Go 1.16.

* [FEATURE] Add `darwin/arm64` and `illumos/amd64` to the default build #201

## 0.7.0 / 2020-11-03

* [FEATURE] Produce ZIP archives for Windows releases #195

## 0.6.1 / 2020-09-06

* [BUGFIX] Fix cgo builds on illumos by avoiding static linking #192

## 0.6.0 / 2020-09-02

* [CHANGE] Remove default build of darwin/386 #187
* [FEATURE] Add 'check changelog' command. #168
* [FEATURE] Support remotes other than "origin". #174
* [ENHANCEMNT] Improved error handling when parsing CHANGELOG. #161
* [ENANCEMENT] Support arm64 on BSDs. #186

## 0.5.0 / 2019-06-21

* [CHANGE] Remove --broken from git describe. #145
* [FEATURE] Add support for aix/ppc64. #151
* [ENHANCEMENT] cmd/release: add --timeout option. #142
* [ENHANCEMENT] cmd/release: create release in GitHub if none exists. #148

## 0.4.0 / 2019-05-03

* [FEATURE] Fallback to `git describe` output if no VERSION. #130
* [BUGFIX] cmd/tarball: restore --prefix flag. #133
* [BUGFIX] cmd/release: don't leak credentials in case of error. #136

## 0.3.0 / 2019-02-18

* [FEATURE] Make extldflags extensible by configuration. #125
* [ENHANCEMENT] Avoid bind-mounting to allow building with a remote docker engine #95

## 0.2.0 / 2018-11-07

* [FEATURE] Adding changes to support s390x
* [FEATURE] Add option to disable static linking
* [FEATURE] Add support for 32bit MIPS.
* [FEATURE] Added check_licenses Command to Promu
* [ENHANCEMENT] Allow to customize nested options via env variables
* [ENHANCEMENT] Bump Go version to 1.11
* [ENHANCEMENT] Add warning if promu info is unable to determine repo info
* [BUGFIX] Fix build on SmartOS by not setting gcc's -static flag
* [BUGFIX] Fix git repository url parsing

## 0.1.0 / 2017-09-22

Initial release
0707010000000A000081A40000000000000000000000016623999300000098000000000000000000000000000000000000002000000000promu-0.17.0/CODE_OF_CONDUCT.md# Prometheus Community Code of Conduct

Prometheus follows the [CNCF Code of Conduct](https://github.com/cncf/foundation/blob/main/code-of-conduct.md).
0707010000000B000081A40000000000000000000000016623999300000230000000000000000000000000000000000000001D00000000promu-0.17.0/CONTRIBUTING.md# Contributing

Prometheus uses GitHub to manage reviews of pull requests.

* If you have a trivial fix or improvement, go ahead and create a pull request,
  addressing (with `@...`) the maintainer of this repository (see
  [MAINTAINERS.md](MAINTAINERS.md)) in the description of the pull request.

* If you plan to do something more involved, first discuss your ideas
  on our [mailing list](https://groups.google.com/forum/?fromgroups#!forum/prometheus-developers).
  This will avoid unnecessary work and surely give you and us a good deal
  of inspiration.
0707010000000C000081A40000000000000000000000016623999300002C5D000000000000000000000000000000000000001500000000promu-0.17.0/LICENSE                                 Apache License
                           Version 2.0, January 2004
                        http://www.apache.org/licenses/

   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION

   1. Definitions.

      "License" shall mean the terms and conditions for use, reproduction,
      and distribution as defined by Sections 1 through 9 of this document.

      "Licensor" shall mean the copyright owner or entity authorized by
      the copyright owner that is granting the License.

      "Legal Entity" shall mean the union of the acting entity and all
      other entities that control, are controlled by, or are under common
      control with that entity. For the purposes of this definition,
      "control" means (i) the power, direct or indirect, to cause the
      direction or management of such entity, whether by contract or
      otherwise, or (ii) ownership of fifty percent (50%) or more of the
      outstanding shares, or (iii) beneficial ownership of such entity.

      "You" (or "Your") shall mean an individual or Legal Entity
      exercising permissions granted by this License.

      "Source" form shall mean the preferred form for making modifications,
      including but not limited to software source code, documentation
      source, and configuration files.

      "Object" form shall mean any form resulting from mechanical
      transformation or translation of a Source form, including but
      not limited to compiled object code, generated documentation,
      and conversions to other media types.

      "Work" shall mean the work of authorship, whether in Source or
      Object form, made available under the License, as indicated by a
      copyright notice that is included in or attached to the work
      (an example is provided in the Appendix below).

      "Derivative Works" shall mean any work, whether in Source or Object
      form, that is based on (or derived from) the Work and for which the
      editorial revisions, annotations, elaborations, or other modifications
      represent, as a whole, an original work of authorship. For the purposes
      of this License, Derivative Works shall not include works that remain
      separable from, or merely link (or bind by name) to the interfaces of,
      the Work and Derivative Works thereof.

      "Contribution" shall mean any work of authorship, including
      the original version of the Work and any modifications or additions
      to that Work or Derivative Works thereof, that is intentionally
      submitted to Licensor for inclusion in the Work by the copyright owner
      or by an individual or Legal Entity authorized to submit on behalf of
      the copyright owner. For the purposes of this definition, "submitted"
      means any form of electronic, verbal, or written communication sent
      to the Licensor or its representatives, including but not limited to
      communication on electronic mailing lists, source code control systems,
      and issue tracking systems that are managed by, or on behalf of, the
      Licensor for the purpose of discussing and improving the Work, but
      excluding communication that is conspicuously marked or otherwise
      designated in writing by the copyright owner as "Not a Contribution."

      "Contributor" shall mean Licensor and any individual or Legal Entity
      on behalf of whom a Contribution has been received by Licensor and
      subsequently incorporated within the Work.

   2. Grant of Copyright License. Subject to the terms and conditions of
      this License, each Contributor hereby grants to You a perpetual,
      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
      copyright license to reproduce, prepare Derivative Works of,
      publicly display, publicly perform, sublicense, and distribute the
      Work and such Derivative Works in Source or Object form.

   3. Grant of Patent License. Subject to the terms and conditions of
      this License, each Contributor hereby grants to You a perpetual,
      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
      (except as stated in this section) patent license to make, have made,
      use, offer to sell, sell, import, and otherwise transfer the Work,
      where such license applies only to those patent claims licensable
      by such Contributor that are necessarily infringed by their
      Contribution(s) alone or by combination of their Contribution(s)
      with the Work to which such Contribution(s) was submitted. If You
      institute patent litigation against any entity (including a
      cross-claim or counterclaim in a lawsuit) alleging that the Work
      or a Contribution incorporated within the Work constitutes direct
      or contributory patent infringement, then any patent licenses
      granted to You under this License for that Work shall terminate
      as of the date such litigation is filed.

   4. Redistribution. You may reproduce and distribute copies of the
      Work or Derivative Works thereof in any medium, with or without
      modifications, and in Source or Object form, provided that You
      meet the following conditions:

      (a) You must give any other recipients of the Work or
          Derivative Works a copy of this License; and

      (b) You must cause any modified files to carry prominent notices
          stating that You changed the files; and

      (c) You must retain, in the Source form of any Derivative Works
          that You distribute, all copyright, patent, trademark, and
          attribution notices from the Source form of the Work,
          excluding those notices that do not pertain to any part of
          the Derivative Works; and

      (d) If the Work includes a "NOTICE" text file as part of its
          distribution, then any Derivative Works that You distribute must
          include a readable copy of the attribution notices contained
          within such NOTICE file, excluding those notices that do not
          pertain to any part of the Derivative Works, in at least one
          of the following places: within a NOTICE text file distributed
          as part of the Derivative Works; within the Source form or
          documentation, if provided along with the Derivative Works; or,
          within a display generated by the Derivative Works, if and
          wherever such third-party notices normally appear. The contents
          of the NOTICE file are for informational purposes only and
          do not modify the License. You may add Your own attribution
          notices within Derivative Works that You distribute, alongside
          or as an addendum to the NOTICE text from the Work, provided
          that such additional attribution notices cannot be construed
          as modifying the License.

      You may add Your own copyright statement to Your modifications and
      may provide additional or different license terms and conditions
      for use, reproduction, or distribution of Your modifications, or
      for any such Derivative Works as a whole, provided Your use,
      reproduction, and distribution of the Work otherwise complies with
      the conditions stated in this License.

   5. Submission of Contributions. Unless You explicitly state otherwise,
      any Contribution intentionally submitted for inclusion in the Work
      by You to the Licensor shall be under the terms and conditions of
      this License, without any additional terms or conditions.
      Notwithstanding the above, nothing herein shall supersede or modify
      the terms of any separate license agreement you may have executed
      with Licensor regarding such Contributions.

   6. Trademarks. This License does not grant permission to use the trade
      names, trademarks, service marks, or product names of the Licensor,
      except as required for reasonable and customary use in describing the
      origin of the Work and reproducing the content of the NOTICE file.

   7. Disclaimer of Warranty. Unless required by applicable law or
      agreed to in writing, Licensor provides the Work (and each
      Contributor provides its Contributions) on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
      implied, including, without limitation, any warranties or conditions
      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
      PARTICULAR PURPOSE. You are solely responsible for determining the
      appropriateness of using or redistributing the Work and assume any
      risks associated with Your exercise of permissions under this License.

   8. Limitation of Liability. In no event and under no legal theory,
      whether in tort (including negligence), contract, or otherwise,
      unless required by applicable law (such as deliberate and grossly
      negligent acts) or agreed to in writing, shall any Contributor be
      liable to You for damages, including any direct, indirect, special,
      incidental, or consequential damages of any character arising as a
      result of this License or out of the use or inability to use the
      Work (including but not limited to damages for loss of goodwill,
      work stoppage, computer failure or malfunction, or any and all
      other commercial damages or losses), even if such Contributor
      has been advised of the possibility of such damages.

   9. Accepting Warranty or Additional Liability. While redistributing
      the Work or Derivative Works thereof, You may choose to offer,
      and charge a fee for, acceptance of support, warranty, indemnity,
      or other liability obligations and/or rights consistent with this
      License. However, in accepting such obligations, You may act only
      on Your own behalf and on Your sole responsibility, not on behalf
      of any other Contributor, and only if You agree to indemnify,
      defend, and hold each Contributor harmless for any liability
      incurred by, or claims asserted against, such Contributor by reason
      of your accepting any such warranty or additional liability.

   END OF TERMS AND CONDITIONS

   APPENDIX: How to apply the Apache License to your work.

      To apply the Apache License to your work, attach the following
      boilerplate notice, with the fields enclosed by brackets "[]"
      replaced with your own identifying information. (Don't include
      the brackets!)  The text should be enclosed in the appropriate
      comment syntax for the file format. We also recommend that a
      file or class name and description of purpose be included on the
      same "printed page" as the copyright notice for easier
      identification within third-party archives.

   Copyright [yyyy] [name of copyright owner]

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
0707010000000D000081A4000000000000000000000001662399930000002C000000000000000000000000000000000000001C00000000promu-0.17.0/MAINTAINERS.md* Steve Durrheimer <s.durrheimer@gmail.com>
0707010000000E000081A4000000000000000000000001662399930000034E000000000000000000000000000000000000001600000000promu-0.17.0/Makefile# Copyright 2018 The Prometheus Authors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

include Makefile.common

.PHONY: build
build:
	@echo ">> installing promu"
	GO111MODULE=$(GO111MODULE) GOOS= GOARCH= $(GO) install github.com/prometheus/promu
	@echo ">> rebuilding binaries using promu"
	GO111MODULE=$(GO111MODULE) $(PROMU) build --prefix $(PREFIX)
0707010000000F000081A400000000000000000000000166239993000023CD000000000000000000000000000000000000001D00000000promu-0.17.0/Makefile.common# Copyright 2018 The Prometheus Authors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


# A common Makefile that includes rules to be reused in different prometheus projects.
# !!! Open PRs only against the prometheus/prometheus/Makefile.common repository!

# Example usage :
# Create the main Makefile in the root project directory.
# include Makefile.common
# customTarget:
# 	@echo ">> Running customTarget"
#

# Ensure GOBIN is not set during build so that promu is installed to the correct path
unexport GOBIN

GO           ?= go
GOFMT        ?= $(GO)fmt
FIRST_GOPATH := $(firstword $(subst :, ,$(shell $(GO) env GOPATH)))
GOOPTS       ?=
GOHOSTOS     ?= $(shell $(GO) env GOHOSTOS)
GOHOSTARCH   ?= $(shell $(GO) env GOHOSTARCH)

GO_VERSION        ?= $(shell $(GO) version)
GO_VERSION_NUMBER ?= $(word 3, $(GO_VERSION))
PRE_GO_111        ?= $(shell echo $(GO_VERSION_NUMBER) | grep -E 'go1\.(10|[0-9])\.')

PROMU        := $(FIRST_GOPATH)/bin/promu
pkgs          = ./...

ifeq (arm, $(GOHOSTARCH))
	GOHOSTARM ?= $(shell GOARM= $(GO) env GOARM)
	GO_BUILD_PLATFORM ?= $(GOHOSTOS)-$(GOHOSTARCH)v$(GOHOSTARM)
else
	GO_BUILD_PLATFORM ?= $(GOHOSTOS)-$(GOHOSTARCH)
endif

GOTEST := $(GO) test
GOTEST_DIR :=
ifneq ($(CIRCLE_JOB),)
ifneq ($(shell command -v gotestsum 2> /dev/null),)
	GOTEST_DIR := test-results
	GOTEST := gotestsum --junitfile $(GOTEST_DIR)/unit-tests.xml --
endif
endif

PROMU_VERSION ?= 0.15.0
PROMU_URL     := https://github.com/prometheus/promu/releases/download/v$(PROMU_VERSION)/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM).tar.gz

SKIP_GOLANGCI_LINT :=
GOLANGCI_LINT :=
GOLANGCI_LINT_OPTS ?=
GOLANGCI_LINT_VERSION ?= v1.56.2
# golangci-lint only supports linux, darwin and windows platforms on i386/amd64/arm64.
# windows isn't included here because of the path separator being different.
ifeq ($(GOHOSTOS),$(filter $(GOHOSTOS),linux darwin))
	ifeq ($(GOHOSTARCH),$(filter $(GOHOSTARCH),amd64 i386 arm64))
		# If we're in CI and there is an Actions file, that means the linter
		# is being run in Actions, so we don't need to run it here.
		ifneq (,$(SKIP_GOLANGCI_LINT))
			GOLANGCI_LINT :=
		else ifeq (,$(CIRCLE_JOB))
			GOLANGCI_LINT := $(FIRST_GOPATH)/bin/golangci-lint
		else ifeq (,$(wildcard .github/workflows/golangci-lint.yml))
			GOLANGCI_LINT := $(FIRST_GOPATH)/bin/golangci-lint
		endif
	endif
endif

PREFIX                  ?= $(shell pwd)
BIN_DIR                 ?= $(shell pwd)
DOCKER_IMAGE_TAG        ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD))
DOCKERFILE_PATH         ?= ./Dockerfile
DOCKERBUILD_CONTEXT     ?= ./
DOCKER_REPO             ?= prom

DOCKER_ARCHS            ?= amd64

BUILD_DOCKER_ARCHS = $(addprefix common-docker-,$(DOCKER_ARCHS))
PUBLISH_DOCKER_ARCHS = $(addprefix common-docker-publish-,$(DOCKER_ARCHS))
TAG_DOCKER_ARCHS = $(addprefix common-docker-tag-latest-,$(DOCKER_ARCHS))

SANITIZED_DOCKER_IMAGE_TAG := $(subst +,-,$(DOCKER_IMAGE_TAG))

ifeq ($(GOHOSTARCH),amd64)
        ifeq ($(GOHOSTOS),$(filter $(GOHOSTOS),linux freebsd darwin windows))
                # Only supported on amd64
                test-flags := -race
        endif
endif

# This rule is used to forward a target like "build" to "common-build".  This
# allows a new "build" target to be defined in a Makefile which includes this
# one and override "common-build" without override warnings.
%: common-% ;

.PHONY: common-all
common-all: precheck style check_license lint yamllint unused build test

.PHONY: common-style
common-style:
	@echo ">> checking code style"
	@fmtRes=$$($(GOFMT) -d $$(find . -path ./vendor -prune -o -name '*.go' -print)); \
	if [ -n "$${fmtRes}" ]; then \
		echo "gofmt checking failed!"; echo "$${fmtRes}"; echo; \
		echo "Please ensure you are using $$($(GO) version) for formatting code."; \
		exit 1; \
	fi

.PHONY: common-check_license
common-check_license:
	@echo ">> checking license header"
	@licRes=$$(for file in $$(find . -type f -iname '*.go' ! -path './vendor/*') ; do \
               awk 'NR<=3' $$file | grep -Eq "(Copyright|generated|GENERATED)" || echo $$file; \
       done); \
       if [ -n "$${licRes}" ]; then \
               echo "license header checking failed:"; echo "$${licRes}"; \
               exit 1; \
       fi

.PHONY: common-deps
common-deps:
	@echo ">> getting dependencies"
	$(GO) mod download

.PHONY: update-go-deps
update-go-deps:
	@echo ">> updating Go dependencies"
	@for m in $$($(GO) list -mod=readonly -m -f '{{ if and (not .Indirect) (not .Main)}}{{.Path}}{{end}}' all); do \
		$(GO) get -d $$m; \
	done
	$(GO) mod tidy

.PHONY: common-test-short
common-test-short: $(GOTEST_DIR)
	@echo ">> running short tests"
	$(GOTEST) -short $(GOOPTS) $(pkgs)

.PHONY: common-test
common-test: $(GOTEST_DIR)
	@echo ">> running all tests"
	$(GOTEST) $(test-flags) $(GOOPTS) $(pkgs)

$(GOTEST_DIR):
	@mkdir -p $@

.PHONY: common-format
common-format:
	@echo ">> formatting code"
	$(GO) fmt $(pkgs)

.PHONY: common-vet
common-vet:
	@echo ">> vetting code"
	$(GO) vet $(GOOPTS) $(pkgs)

.PHONY: common-lint
common-lint: $(GOLANGCI_LINT)
ifdef GOLANGCI_LINT
	@echo ">> running golangci-lint"
	$(GOLANGCI_LINT) run $(GOLANGCI_LINT_OPTS) $(pkgs)
endif

.PHONY: common-lint-fix
common-lint-fix: $(GOLANGCI_LINT)
ifdef GOLANGCI_LINT
	@echo ">> running golangci-lint fix"
	$(GOLANGCI_LINT) run --fix $(GOLANGCI_LINT_OPTS) $(pkgs)
endif

.PHONY: common-yamllint
common-yamllint:
	@echo ">> running yamllint on all YAML files in the repository"
ifeq (, $(shell command -v yamllint 2> /dev/null))
	@echo "yamllint not installed so skipping"
else
	yamllint .
endif

# For backward-compatibility.
.PHONY: common-staticcheck
common-staticcheck: lint

.PHONY: common-unused
common-unused:
	@echo ">> running check for unused/missing packages in go.mod"
	$(GO) mod tidy
	@git diff --exit-code -- go.sum go.mod

.PHONY: common-build
common-build: promu
	@echo ">> building binaries"
	$(PROMU) build --prefix $(PREFIX) $(PROMU_BINARIES)

.PHONY: common-tarball
common-tarball: promu
	@echo ">> building release tarball"
	$(PROMU) tarball --prefix $(PREFIX) $(BIN_DIR)

.PHONY: common-docker-repo-name
common-docker-repo-name:
	@echo "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)"

.PHONY: common-docker $(BUILD_DOCKER_ARCHS)
common-docker: $(BUILD_DOCKER_ARCHS)
$(BUILD_DOCKER_ARCHS): common-docker-%:
	docker build -t "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:$(SANITIZED_DOCKER_IMAGE_TAG)" \
		-f $(DOCKERFILE_PATH) \
		--build-arg ARCH="$*" \
		--build-arg OS="linux" \
		$(DOCKERBUILD_CONTEXT)

.PHONY: common-docker-publish $(PUBLISH_DOCKER_ARCHS)
common-docker-publish: $(PUBLISH_DOCKER_ARCHS)
$(PUBLISH_DOCKER_ARCHS): common-docker-publish-%:
	docker push "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:$(SANITIZED_DOCKER_IMAGE_TAG)"

DOCKER_MAJOR_VERSION_TAG = $(firstword $(subst ., ,$(shell cat VERSION)))
.PHONY: common-docker-tag-latest $(TAG_DOCKER_ARCHS)
common-docker-tag-latest: $(TAG_DOCKER_ARCHS)
$(TAG_DOCKER_ARCHS): common-docker-tag-latest-%:
	docker tag "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:$(SANITIZED_DOCKER_IMAGE_TAG)" "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:latest"
	docker tag "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:$(SANITIZED_DOCKER_IMAGE_TAG)" "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:v$(DOCKER_MAJOR_VERSION_TAG)"

.PHONY: common-docker-manifest
common-docker-manifest:
	DOCKER_CLI_EXPERIMENTAL=enabled docker manifest create -a "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):$(SANITIZED_DOCKER_IMAGE_TAG)" $(foreach ARCH,$(DOCKER_ARCHS),$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$(ARCH):$(SANITIZED_DOCKER_IMAGE_TAG))
	DOCKER_CLI_EXPERIMENTAL=enabled docker manifest push "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):$(SANITIZED_DOCKER_IMAGE_TAG)"

.PHONY: promu
promu: $(PROMU)

$(PROMU):
	$(eval PROMU_TMP := $(shell mktemp -d))
	curl -s -L $(PROMU_URL) | tar -xvzf - -C $(PROMU_TMP)
	mkdir -p $(FIRST_GOPATH)/bin
	cp $(PROMU_TMP)/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM)/promu $(FIRST_GOPATH)/bin/promu
	rm -r $(PROMU_TMP)

.PHONY: proto
proto:
	@echo ">> generating code from proto files"
	@./scripts/genproto.sh

ifdef GOLANGCI_LINT
$(GOLANGCI_LINT):
	mkdir -p $(FIRST_GOPATH)/bin
	curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/$(GOLANGCI_LINT_VERSION)/install.sh \
		| sed -e '/install -d/d' \
		| sh -s -- -b $(FIRST_GOPATH)/bin $(GOLANGCI_LINT_VERSION)
endif

.PHONY: precheck
precheck::

define PRECHECK_COMMAND_template =
precheck:: $(1)_precheck

PRECHECK_COMMAND_$(1) ?= $(1) $$(strip $$(PRECHECK_OPTIONS_$(1)))
.PHONY: $(1)_precheck
$(1)_precheck:
	@if ! $$(PRECHECK_COMMAND_$(1)) 1>/dev/null 2>&1; then \
		echo "Execution of '$$(PRECHECK_COMMAND_$(1))' command failed. Is $(1) installed?"; \
		exit 1; \
	fi
endef
07070100000010000081A400000000000000000000000166239993000000E9000000000000000000000000000000000000001400000000promu-0.17.0/NOTICEPrometheus Utility Tool
Copyright 2013-2016 The Prometheus Authors


The following components were used in this product as inspiration:

Glu
https://github.com/gliderlabs/glu
Copyright 2015 Glider Labs
Licensed under the MIT License
07070100000011000081A400000000000000000000000166239993000007B3000000000000000000000000000000000000001700000000promu-0.17.0/README.md# Prometheus Utility Tool [![CircleCI](https://circleci.com/gh/prometheus/promu/tree/master.svg?style=shield)][circleci]

## Usage

```help
usage: promu [<flags>] <command> [<args> ...]

promu is the utility tool for building and releasing Prometheus projects

Flags:
  -h, --[no-]help            Show context-sensitive help (also try --help-long and --help-man).
  -c, --config=".promu.yml"  Path to config file
  -v, --[no-]verbose         Verbose output

Commands:
help [<command>...]
    Show help.

build [<flags>] [<binary-names>...]
    Build a Go project

check licenses [<flags>] [<location>...]
    Inspect source files for each file in a given directory

check changelog [<flags>]
    Check that CHANGELOG.md follows the guidelines

checksum [<location>...]
    Calculate the SHA256 checksum for each file in the given location

codesign <path>
    Code sign the darwin binary using rcodesign.

crossbuild [<flags>] [<tarballs>]
    Crossbuild a Go project using Golang builder Docker images

info
    Print info about current project and exit

release [<flags>] [<location>...]
    Upload all release files to the Github release

tarball [<flags>] [<location>...]
    Create a tarball from the built Go project

version [<flags>]
    Print the version and exit
```

## `.promu.yml` config file

See documentation example [here](doc/examples/prometheus/.promu.yml)

## Compatibility

* Go 1.14+

## More information

* This tool is part of our reflection about [Prometheus component Builds](https://docs.google.com/document/d/1Ql-f_aThl-2eB5v3QdKV_zgBdetLLbdxxChpy-TnWSE)
* All of the core developers are accessible via the [Prometheus Developers Mailinglist](https://groups.google.com/forum/?fromgroups#!forum/prometheus-developers) and the `#prometheus` channel on `irc.freenode.net`.

## Contributing

Refer to [CONTRIBUTING.md](CONTRIBUTING.md)

## License

Apache License 2.0, see [LICENSE](LICENSE).

[circleci]: https://circleci.com/gh/prometheus/promu
07070100000012000081A400000000000000000000000166239993000000AC000000000000000000000000000000000000001900000000promu-0.17.0/SECURITY.md# Reporting a security issue

The Prometheus security policy, including how to report vulnerabilities, can be
found here:

<https://prometheus.io/docs/operating/security/>
07070100000013000081A40000000000000000000000016623999300000007000000000000000000000000000000000000001500000000promu-0.17.0/VERSION0.17.0
07070100000014000041ED0000000000000000000000026623999300000000000000000000000000000000000000000000001100000000promu-0.17.0/cmd07070100000015000081A4000000000000000000000001662399930000192F000000000000000000000000000000000000001A00000000promu-0.17.0/cmd/build.go// Copyright © 2016 Prometheus Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
	"bytes"
	"fmt"
	"log"
	"os"
	"path"
	"strconv"
	"strings"
	"text/template"
	"time"

	kingpin "github.com/alecthomas/kingpin/v2"

	"github.com/prometheus/promu/pkg/repository"
	"github.com/prometheus/promu/util/sh"
)

const (
	sourceDateEpoch = "SOURCE_DATE_EPOCH"
)

var (
	buildcmd        = app.Command("build", "Build a Go project")
	buildCgoFlagSet bool
	buildCgoFlag    = buildcmd.Flag("cgo", "Enable CGO").
			PreAction(func(c *kingpin.ParseContext) error {
			buildCgoFlagSet = true
			return nil
		}).Bool()
	prefixFlagSet bool
	prefixFlag    = buildcmd.Flag("prefix", "Specific dir to store binaries (default is .)").
			PreAction(func(c *kingpin.ParseContext) error {
			prefixFlagSet = true
			return nil
		}).String()
	binariesArg = buildcmd.Arg("binary-names", "Comma separated list of binaries to build").Default("all").Strings()
)

// Check if binary names passed to build command are in the config.
// Returns an array of Binary to build, or error.
func validateBinaryNames(binaryNames []string, cfgBinaries []Binary) ([]Binary, error) {
	var binaries []Binary

OUTER:
	for _, binaryName := range binaryNames {
		for _, binary := range cfgBinaries {
			if binaryName == binary.Name {
				binaries = append(binaries, binary)
				continue OUTER
			}
		}
		return nil, fmt.Errorf("binary %s not found in config", binaryName)
	}
	return binaries, nil
}

func buildBinary(ext string, prefix string, ldflags string, tags []string, binary Binary) {
	info("Building binary: " + binary.Name)
	binaryName := fmt.Sprintf("%s%s", binary.Name, ext)
	fmt.Printf(" >   %s\n", binaryName)

	repoPath := config.Repository.Path
	flags := config.Build.Flags

	params := []string{
		"build",
		"-o", path.Join(prefix, binaryName),
		"-ldflags", ldflags,
	}

	params = append(params, sh.SplitParameters(flags)...)
	if len(tags) > 0 {
		params = append(params, "-tags", strings.Join(tags, ","))
	}
	params = append(params, path.Join(repoPath, binary.Path))
	info("Building binary: " + "go " + strings.Join(params, " "))
	if err := sh.RunCommand("go", params...); err != nil {
		fatal(fmt.Errorf("command failed: %s: %w", strings.Join(params, " "), err))
	}
}

func buildAll(ext string, prefix string, ldflags string, tags []string, binaries []Binary) {
	for _, binary := range binaries {
		buildBinary(ext, prefix, ldflags, tags, binary)
	}
}

func runBuild(binariesString string) {
	// Check required configuration
	if len(strings.TrimSpace(config.Repository.Path)) == 0 {
		log.Fatalf("missing required '%s' configuration", "repository.path")
	}
	if buildCgoFlagSet {
		config.Go.CGo = *buildCgoFlag
	}
	if prefixFlagSet {
		config.Build.Prefix = *prefixFlag
	}

	var (
		cgo    = config.Go.CGo
		prefix = config.Build.Prefix

		ext      string
		binaries = config.Build.Binaries
		ldflags  string
	)

	if goos == "windows" {
		ext = ".exe"
	}

	ldflags = getLdflags(projInfo)

	os.Setenv("CGO_ENABLED", "0")
	if cgo {
		os.Setenv("CGO_ENABLED", "1")
	}
	defer os.Unsetenv("CGO_ENABLED")

	if binariesString == "all" {
		buildAll(ext, prefix, ldflags, getTags(config.Build.Tags), binaries)
		return
	}

	binariesArray := strings.Split(binariesString, ",")
	binariesToBuild, err := validateBinaryNames(binariesArray, binaries)
	if err != nil {
		fatal(fmt.Errorf("validation of given binary names for build command failed: %w", err))
	}

	for _, binary := range binariesToBuild {
		buildBinary(ext, prefix, ldflags, getTags(config.Build.Tags), binary)
	}
}

func getLdflags(info repository.Info) string {
	var ldflags []string

	if len(strings.TrimSpace(config.Build.LDFlags)) > 0 {
		buildDate := getBuildDate()
		var (
			tmplOutput = new(bytes.Buffer)
			fnMap      = template.FuncMap{
				"date":     buildDate.UTC().Format,
				"host":     HostFunc,
				"repoPath": RepoPathFunc,
				"user":     UserFunc,
			}
			ldflagsTmpl = config.Build.LDFlags
		)

		tmpl, err := template.New("ldflags").Funcs(fnMap).Parse(ldflagsTmpl)
		if err != nil {
			fatal(fmt.Errorf("Failed to parse ldflags text/template: %w", err))
		}

		if err := tmpl.Execute(tmplOutput, info); err != nil {
			fatal(fmt.Errorf("Failed to execute ldflags text/template: %w", err))
		}

		ldflags = append(ldflags, strings.Split(tmplOutput.String(), "\n")...)
	} else {
		ldflags = append(ldflags, fmt.Sprintf("-X main.Version=%s", info.Version))
	}

	extLDFlags := config.Build.ExtLDFlags
	if config.Build.Static && goos != "darwin" && goos != "solaris" && goos != "illumos" && !stringInSlice("-static", extLDFlags) {
		extLDFlags = append(extLDFlags, "-static")
	}

	if len(extLDFlags) > 0 {
		ldflags = append(ldflags, fmt.Sprintf("-extldflags '%s'", strings.Join(extLDFlags, " ")))
	}

	return strings.Join(ldflags, " ")
}

func getBuildDate() time.Time {
	var buildDate time.Time

	sourceDate := os.Getenv(sourceDateEpoch)
	if sourceDate == "" {
		buildDate = time.Now()
	} else {
		unixBuildDate, err := strconv.ParseInt(sourceDate, 10, 64)
		if err != nil {
			fatal(fmt.Errorf("Failed to parse %s: %w", sourceDateEpoch, err))
		} else {
			buildDate = time.Unix(unixBuildDate, 0)
		}
	}
	return buildDate
}

func HostFunc() string {
	if isReproducibleBuild() {
		return "reproducible"
	}
	hostname, err := os.Hostname()
	if err != nil {
		return "unknown-host"
	}
	return hostname
}

// UserFunc returns the current username.
func UserFunc() (interface{}, error) {
	if isReproducibleBuild() {
		return "reproducible", nil
	}
	// os/user.Current() doesn't always work without CGO
	return shellOutput("whoami"), nil
}

func isReproducibleBuild() bool {
	return os.Getenv(sourceDateEpoch) != ""
}

// RepoPathFunc returns the repository path.
func RepoPathFunc() interface{} {
	return config.Repository.Path
}

func getTags(allTags map[string][]string) []string {
	if tags, ok := allTags[envOr("GOOS", goos)]; ok {
		return tags
	}
	return allTags["all"]
}
07070100000016000081A40000000000000000000000016623999300001002000000000000000000000000000000000000001A00000000promu-0.17.0/cmd/check.go// Copyright © 2017 Prometheus Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
	"bufio"
	"fmt"
	"os"
	"path/filepath"
	"strings"

	"github.com/prometheus/promu/pkg/changelog"
)

var (
	// validHeaderStrings is a slice of strings that must exist in a header.
	validHeaderStrings = []string{"copyright", "generated"}

	checkcmd         = app.Command("check", "Check the resources for validity")
	checkLicensescmd = checkcmd.Command("licenses", "Inspect source files for each file in a given directory")
	sourceExtensions = checkLicensescmd.Flag("extensions", "Comma separated list of valid source code extensions (default is .go)").
				Default(".go").Strings()
	headerLength = checkLicensescmd.Flag("length", "The number of lines to read from the head of the file").
			Short('n').Default("10").Int()
	checkLicLocation = checkLicensescmd.Arg("location", "Directory path to check licenses").
				Default(".").Strings()

	checkChangelogcmd  = checkcmd.Command("changelog", "Check that CHANGELOG.md follows the guidelines")
	checkChangelogPath = checkChangelogcmd.Flag("location", "Path to CHANGELOG.md").
				Default("CHANGELOG.md").String()
	checkChangelogVersion = checkChangelogcmd.Flag("version", "Version to check (defaults to the current version)").
				Default("").String()
)

func runCheckLicenses(path string, n int, extensions []string) {
	path = fmt.Sprintf("%s%c", filepath.Clean(path), filepath.Separator)

	filesMissingHeaders, err := checkLicenses(path, n, extensions)
	if err != nil {
		fatal(fmt.Errorf("Failed to check files for license header: %w", err))
	}

	for _, file := range filesMissingHeaders {
		fmt.Println(file)
	}
}

func checkLicenses(path string, n int, extensions []string) ([]string, error) {
	var missingHeaders []string
	walkFunc := func(filepath string, f os.FileInfo, err error) error {
		if err != nil {
			return err
		}

		if f.IsDir() {
			return nil
		}

		if strings.HasPrefix(filepath, "vendor/") {
			return nil
		}

		if !suffixInSlice(f.Name(), extensions) {
			return nil
		}

		file, err := os.Open(filepath)
		if err != nil {
			return err
		}

		defer file.Close()

		pass := false
		scanner := bufio.NewScanner(file)
		for i := 0; i < n; i++ {
			scanner.Scan()

			if err = scanner.Err(); err != nil {
				return err
			}

			if stringContainedInSlice(strings.ToLower(scanner.Text()), validHeaderStrings) {
				pass = true
			}
		}

		if !pass {
			missingHeaders = append(missingHeaders, filepath)
		}

		return nil
	}

	err := filepath.Walk(path, walkFunc)
	if err != nil {
		return nil, err
	}

	return missingHeaders, nil
}

func stringContainedInSlice(needle string, haystack []string) bool {
	exists := false
	for _, h := range haystack {
		if strings.Contains(needle, h) {
			exists = true
			break
		}
	}

	return exists
}

func suffixInSlice(needle string, haystack []string) bool {
	exists := false
	for _, h := range haystack {
		if strings.HasSuffix(needle, h) {
			exists = true
			break
		}
	}

	return exists
}

func runCheckChangelog(path string, version string) error {
	if version == "" {
		_, err := projInfo.ToSemver()
		if err != nil {
			return fmt.Errorf("invalid semver version: %w", err)
		}

		version = projInfo.Version
	}

	f, err := os.Open(path)
	if err != nil {
		return err
	}
	defer f.Close()

	entry, err := changelog.ReadEntry(f, version)
	if err != nil {
		return fmt.Errorf("%s: %w", path, err)
	}

	// Check that the changes are ordered correctly.
	err = entry.Changes.Sorted()
	if err != nil {
		return fmt.Errorf("invalid changelog entry: %w", err)
	}

	return nil
}
07070100000017000081A4000000000000000000000001662399930000098C000000000000000000000000000000000000001D00000000promu-0.17.0/cmd/checksum.go// Copyright © 2017 Prometheus Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
	"crypto/sha256"
	"fmt"
	"io"
	"os"
	"path/filepath"
	"strings"
)

const (
	checksumsFilename = "sha256sums.txt"
)

var (
	checksumcmd      = app.Command("checksum", "Calculate the SHA256 checksum for each file in the given location")
	checksumLocation = checksumcmd.Arg("location", "Location to checksum").Default(".").Strings()
)

func runChecksum(path string) {
	checksums, err := calculateSHA256s(path)
	if err != nil {
		fatal(fmt.Errorf("Failed to calculate checksums: %w", err))
	}

	file, err := os.Create(filepath.Join(path, checksumsFilename))
	if err != nil {
		fatal(fmt.Errorf("Failed to create checksums file: %w", err))
	}
	defer file.Close()
	for _, c := range checksums {
		if _, err := fmt.Fprintf(file, "%x  %s\n", c.checksum, c.filename); err != nil {
			fatal(fmt.Errorf("Failed to write to checksums file: %w", err))
		}
	}
}

type checksumSHA256 struct {
	filename string
	checksum []byte
}

// calculateSHA256s calculates the sha256 checksum for each file in the given
// path and returns a checksumSHA256 type in the order returned of
// filepath.Walk.
func calculateSHA256s(path string) ([]checksumSHA256, error) {
	var checksums []checksumSHA256
	path = fmt.Sprintf("%s%c", filepath.Clean(path), filepath.Separator)
	calculateSHA256 := func(filepath string, f os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if f.IsDir() {
			return nil
		}

		file, err := os.Open(filepath)
		if err != nil {
			return err
		}
		defer file.Close()

		hash := sha256.New()
		if _, err = io.Copy(hash, file); err != nil {
			return err
		}
		checksums = append(checksums, checksumSHA256{
			filename: strings.TrimPrefix(filepath, path),
			checksum: hash.Sum(nil),
		})

		return nil
	}
	if err := filepath.Walk(path, calculateSHA256); err != nil {
		return nil, err
	}
	return checksums, nil
}
07070100000018000081A40000000000000000000000016623999300000527000000000000000000000000000000000000002200000000promu-0.17.0/cmd/checksum_test.go// Copyright © 2017 Prometheus Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
	"crypto/sha256"
	"os"
	"path/filepath"
	"reflect"
	"testing"
)

func TestCalculateSHA256s(t *testing.T) {
	dir, err := os.MkdirTemp("", "promu")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(dir)

	var (
		filename = "testfile"
		location = filepath.Join(dir, filename)
		content  = []byte("temporary file's content")
		checksum = sha256.Sum256(content)
	)
	if err = os.WriteFile(location, content, 0o666); err != nil {
		t.Fatal(err)
	}

	got, err := calculateSHA256s(dir)
	if err != nil {
		t.Fatal(err)
	}
	want := []checksumSHA256{
		{
			filename: filename,
			checksum: checksum[:],
		},
	}
	if !reflect.DeepEqual(want, got) {
		t.Errorf("want checksums %+v, got %+v", want, got)
	}
}
07070100000019000081A400000000000000000000000166239993000006F0000000000000000000000000000000000000001D00000000promu-0.17.0/cmd/codesign.go// Copyright © 2024 Prometheus Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
	"fmt"
	"path/filepath"

	"github.com/prometheus/promu/util/sh"
)

var (
	codesigncmd = app.Command("codesign", "Code sign the darwin binary using rcodesign.")
	binaryPath  = codesigncmd.Arg("path", "Absolute path to binary to be signed").Required().String()
)

func runCodeSign(binaryPath string) {
	codeSignGoBinary(binaryPath)
}

func codeSignGoBinary(binaryPath string) {
	var (
		goVersion              = config.Go.Version
		dockerMainBuilderImage = fmt.Sprintf("%s:%s-main", dockerBuilderImageName, goVersion)
		mountPath              = fmt.Sprintf("/%s", filepath.Base(binaryPath))
		mountPathConcat        = fmt.Sprintf("%s:%s", binaryPath, mountPath)
	)
	fmt.Printf("> using rcodesign to sign the binary file at path %s\n", binaryPath)

	// Example:
	// docker run --entrypoint "rcodesign" --rm -v "/path/to/darwin-arm64/node_exporter:/node_exporter"
	// quay.io/prometheus/golang-builder:1.21-main sign /node_exporter
	err := sh.RunCommand("docker", "run", "--entrypoint",
		"rcodesign", "--rm", "-v", mountPathConcat,
		dockerMainBuilderImage, "sign", mountPath)
	if err != nil {
		fmt.Printf("Couldn't sign the binary as intended: %s", err)
	}
}
0707010000001A000081A40000000000000000000000016623999300001B89000000000000000000000000000000000000001F00000000promu-0.17.0/cmd/crossbuild.go// Copyright © 2016 Prometheus Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
	"fmt"
	"log"
	"os"
	"regexp"
	"sort"
	"strconv"
	"strings"
	"sync"
	"time"

	kingpin "github.com/alecthomas/kingpin/v2"
	"go.uber.org/atomic"

	"github.com/prometheus/promu/util/sh"
)

var (
	dockerBuilderImageName = "quay.io/prometheus/golang-builder"

	defaultPlatforms = []string{
		"aix/ppc64",
		"darwin/amd64",
		"darwin/arm64",
		"dragonfly/amd64",
		"freebsd/386",
		"freebsd/amd64",
		"freebsd/arm64",
		"freebsd/armv6",
		"freebsd/armv7",
		"illumos/amd64",
		"linux/386",
		"linux/amd64",
		"linux/arm64",
		"linux/armv5",
		"linux/armv6",
		"linux/armv7",
		"linux/mips",
		"linux/mips64",
		"linux/mips64le",
		"linux/mipsle",
		"linux/ppc64",
		"linux/ppc64le",
		"linux/riscv64",
		"linux/s390x",
		"netbsd/386",
		"netbsd/amd64",
		"netbsd/arm64",
		"netbsd/armv6",
		"netbsd/armv7",
		"openbsd/386",
		"openbsd/amd64",
		"openbsd/arm64",
		"openbsd/armv7",
		"windows/386",
		"windows/amd64",
		"windows/arm64",
	}
)

var (
	crossbuildcmd        = app.Command("crossbuild", "Crossbuild a Go project using Golang builder Docker images")
	crossBuildCgoFlagSet bool
	crossBuildCgoFlag    = crossbuildcmd.Flag("cgo", "Enable CGO using several docker images with different crossbuild toolchains.").
				PreAction(func(c *kingpin.ParseContext) error {
			crossBuildCgoFlagSet = true
			return nil
		}).Default("false").Bool()
	parallelFlag       = crossbuildcmd.Flag("parallelism", "How many builds to run in parallel").Default("1").Int()
	parallelThreadFlag = crossbuildcmd.Flag("parallelism-thread", "Index of the parallel build").Default("-1").Int()
	goFlagSet          bool
	goFlag             = crossbuildcmd.Flag("go", "Golang builder version to use (e.g. 1.11)").
				PreAction(func(c *kingpin.ParseContext) error {
			goFlagSet = true
			return nil
		}).String()
	platformsFlagSet bool
	platformsFlag    = crossbuildcmd.Flag("platforms", "Regexp match platforms to build, may be used multiple times.").Short('p').
				PreAction(func(c *kingpin.ParseContext) error {
			platformsFlagSet = true
			return nil
		}).Strings()
	// kingpin doesn't currently support using the crossbuild command and the
	// crossbuild tarball subcommand at the same time, so we treat the
	// tarball subcommand as an optional arg
	tarballsSubcommand = crossbuildcmd.Arg("tarballs", "Optionally pass the string \"tarballs\" from cross-built binaries").String()
)

func runCrossbuild() {
	// Check required configuration
	if len(strings.TrimSpace(config.Repository.Path)) == 0 {
		log.Fatalf("missing required '%s' configuration", "repository.path")
	}
	if *tarballsSubcommand == "tarballs" {
		runCrossbuildTarballs()
		return
	}

	if crossBuildCgoFlagSet {
		config.Go.CGo = *crossBuildCgoFlag
	}
	if goFlagSet {
		config.Go.Version = *goFlag
	}
	if platformsFlagSet {
		config.Crossbuild.Platforms = *platformsFlag
	}

	var (
		allPlatforms     []string
		unknownPlatforms []string

		cgo       = config.Go.CGo
		goVersion = config.Go.Version
		repoPath  = config.Repository.Path
		platforms = config.Crossbuild.Platforms

		dockerBaseBuilderImage = fmt.Sprintf("%s:%s-base", dockerBuilderImageName, goVersion)
		dockerMainBuilderImage = fmt.Sprintf("%s:%s-main", dockerBuilderImageName, goVersion)
	)

	var filteredPlatforms []string
	for _, platform := range platforms {
		p := regexp.MustCompile(platform)
		if filteredPlatforms = inSliceRE(p, defaultPlatforms); len(filteredPlatforms) > 0 {
			allPlatforms = append(allPlatforms, filteredPlatforms...)
		} else {
			unknownPlatforms = append(unknownPlatforms, platform)
		}
	}

	// Remove duplicates, e.g. if linux/arm and linux/arm64 is specified, there
	// would be linux/arm64 twice in the platforms without this.
	allPlatforms = removeDuplicates(allPlatforms)

	if len(unknownPlatforms) > 0 {
		warn(fmt.Errorf("unknown/unhandled platforms: %s", unknownPlatforms))
	}

	if !cgo {
		// In non-CGO, use the `base` image without any crossbuild toolchain.
		pg := &platformGroup{"base", dockerBaseBuilderImage, allPlatforms}
		if err := pg.Build(repoPath); err != nil {
			fatal(fmt.Errorf("The %s builder docker image exited unexpectedly: %w", pg.Name, err))
		}
	} else {
		// In CGO, use the `main` image with crossbuild toolchain.
		pg := &platformGroup{"main", dockerMainBuilderImage, allPlatforms}
		if err := pg.Build(repoPath); err != nil {
			fatal(fmt.Errorf("The %s builder docker image exited unexpectedly: %w", pg.Name, err))
		}
	}
}

type platformGroup struct {
	Name        string
	DockerImage string
	Platforms   []string
}

func (pg platformGroup) Build(repoPath string) error {
	if *parallelThreadFlag != -1 {
		return pg.buildThread(repoPath, *parallelThreadFlag)
	}
	err := sh.RunCommand("docker", "pull", pg.DockerImage)
	if err != nil {
		return err
	}
	var wg sync.WaitGroup
	wg.Add(*parallelFlag)
	atomicErr := atomic.NewError(nil)
	for p := 0; p < *parallelFlag; p++ {
		go func(p int) {
			defer wg.Done()
			if err := pg.buildThread(repoPath, p); err != nil {
				atomicErr.Store(err)
			}
		}(p)
	}
	wg.Wait()
	return atomicErr.Load()
}

func (pg platformGroup) buildThread(repoPath string, p int) error {
	minb := p * len(pg.Platforms) / *parallelFlag
	maxb := (p + 1) * len(pg.Platforms) / *parallelFlag
	if maxb > len(pg.Platforms) {
		maxb = len(pg.Platforms)
	}
	platformsParam := strings.Join(pg.Platforms[minb:maxb], " ")
	if len(platformsParam) == 0 {
		return nil
	}

	fmt.Printf("> running the %s builder docker image\n", pg.Name)

	cwd, err := os.Getwd()
	if err != nil {
		return fmt.Errorf("couldn't get current working directory: %w", err)
	}

	ctrName := "promu-crossbuild-" + pg.Name + strconv.FormatInt(time.Now().Unix(), 10) + "-" + strconv.Itoa(p)
	err = sh.RunCommand("docker", "create", "-t",
		"--name", ctrName,
		pg.DockerImage,
		"-i", repoPath,
		"-p", platformsParam)
	if err != nil {
		return err
	}

	err = sh.RunCommand("docker", "cp",
		cwd+"/.",
		ctrName+":/app/")
	if err != nil {
		return err
	}

	err = sh.RunCommand("docker", "start", "-a", ctrName)
	if err != nil {
		return err
	}

	err = sh.RunCommand("docker", "cp", "-a",
		ctrName+":/app/.build/.",
		cwd+"/.build")
	if err != nil {
		return err
	}
	return sh.RunCommand("docker", "rm", "-f", ctrName)
}

func removeDuplicates(strings []string) []string {
	keys := map[string]struct{}{}
	list := []string{}
	for _, s := range strings {
		if _, ok := keys[s]; !ok {
			list = append(list, s)
			keys[s] = struct{}{}
		}
	}
	sort.Strings(list)
	return list
}
0707010000001B000081A400000000000000000000000166239993000004F7000000000000000000000000000000000000002800000000promu-0.17.0/cmd/crossbuild_tarballs.go// Copyright © 2016 Prometheus Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
	"fmt"
	"os"
	"path/filepath"
	"strings"
)

func runCrossbuildTarballs() {
	dirs, err := os.ReadDir(".build")
	if err != nil {
		fatal(err)
	}

	fmt.Println(">> building release tarballs")
	for _, dir := range dirs {
		config.Tarball.Prefix = ".tarballs"

		if platform := strings.Split(dir.Name(), "-"); len(platform) == 2 {
			os.Setenv("GOOS", platform[0])
			os.Setenv("GOARCH", platform[1])
		} else {
			if err := fmt.Errorf("bad .build/%s directory naming, should be <GOOS>-<GOARCH>", platform); err != nil {
				fatal(err)
			}
		}

		runTarball(filepath.Join(".build", dir.Name()))
	}

	defer os.Unsetenv("GOOS")
	defer os.Unsetenv("GOARCH")
}
0707010000001C000081A40000000000000000000000016623999300000387000000000000000000000000000000000000002400000000promu-0.17.0/cmd/crossbuild_test.go// Copyright 2021 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
	"reflect"
	"testing"
)

func TestRemoveDuplicates(t *testing.T) {
	input := []string{"linux", "linux", "darwin"}
	output := []string{"darwin", "linux"}
	if deduplicate := removeDuplicates(input); !reflect.DeepEqual(deduplicate, output) {
		t.Fatalf("%q != %q", deduplicate, output)
	}
}
0707010000001D000081A400000000000000000000000166239993000003C9000000000000000000000000000000000000001900000000promu-0.17.0/cmd/info.go// Copyright © 2016 Prometheus Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
	"fmt"
)

var infocmd = app.Command("info", "Print info about current project and exit")

func runInfo() {
	fmt.Println("Name:", projInfo.Name)
	fmt.Println("Version:", projInfo.Version)
	fmt.Println("Owner:", projInfo.Owner)
	fmt.Println("Repo:", projInfo.Repo)
	fmt.Println("Branch:", projInfo.Branch)
	fmt.Println("Revision:", projInfo.Revision)
}
0707010000001E000081A400000000000000000000000166239993000018AF000000000000000000000000000000000000001A00000000promu-0.17.0/cmd/promu.go// Copyright © 2016 Prometheus Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
	"fmt"
	"go/build"
	"os"
	"os/exec"
	"path/filepath"
	"regexp"
	"strings"

	kingpin "github.com/alecthomas/kingpin/v2"
	yaml "gopkg.in/yaml.v2"

	"github.com/prometheus/promu/pkg/repository"
	"github.com/prometheus/promu/util/sh"
)

const (
	// DefaultConfigFilename contains the default filename of the promu config file
	DefaultConfigFilename = ".promu.yml"
)

// Binary represents a built binary.
type Binary struct {
	Name string
	Path string
}

// Config contains the Promu Command Configuration
type Config struct {
	Build struct {
		Binaries   []Binary
		Flags      string
		LDFlags    string
		ExtLDFlags []string
		Tags       map[string][]string
		Prefix     string
		Static     bool
	}
	Crossbuild struct {
		Platforms []string
	}
	Repository struct {
		Path string
	}
	Go struct {
		CGo     bool
		Version string
	}
	Tarball struct {
		Files  []string
		Prefix string
	}
}

// NewConfig creates a Config initialized with default values
// some values may be overridden by CLI args
func NewConfig() *Config {
	config := &Config{}
	config.Build.Binaries = []Binary{{Name: projInfo.Name, Path: "."}}
	config.Build.Prefix = "."
	config.Build.Static = true
	config.Crossbuild.Platforms = defaultPlatforms
	config.Tarball.Prefix = "."
	config.Go.Version = "1.12"
	config.Go.CGo = false
	config.Repository.Path = projInfo.Repo

	return config
}

var (
	buildContext = build.Default
	goos         = buildContext.GOOS
	goarch       = buildContext.GOARCH

	configFile = app.Flag("config", "Path to config file").Short('c').
			Default(DefaultConfigFilename).String()
	verbose  = app.Flag("verbose", "Verbose output").Short('v').Bool()
	config   *Config
	projInfo repository.Info

	// app represents the base command
	app = kingpin.New("promu", "promu is the utility tool for building and releasing Prometheus projects")
)

// init prepares flags
func init() {
	app.HelpFlag.Short('h')
}

// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
	var err error
	projInfo, err = repository.NewInfo(warn)
	checkError(err, "Unable to initialize project info")

	command := kingpin.MustParse(app.Parse(os.Args[1:]))
	sh.Verbose = *verbose
	initConfig(*configFile)

	info(fmt.Sprintf("Running command: %v %v", command, os.Args[2:]))

	switch command {
	case buildcmd.FullCommand():
		runBuild(optArg(*binariesArg, 0, "all"))
	case checkLicensescmd.FullCommand():
		runCheckLicenses(optArg(*checkLicLocation, 0, "."), *headerLength, *sourceExtensions)
	case checkChangelogcmd.FullCommand():
		if err := runCheckChangelog(*checkChangelogPath, *checkChangelogVersion); err != nil {
			fatal(err)
		}
	case checksumcmd.FullCommand():
		runChecksum(optArg(*checksumLocation, 0, "."))
	case crossbuildcmd.FullCommand():
		runCrossbuild()
	case infocmd.FullCommand():
		runInfo()
	case releasecmd.FullCommand():
		runRelease(optArg(*releaseLocation, 0, "."))
	case tarballcmd.FullCommand():
		runTarball(optArg(*tarBinariesLocation, 0, "."))
	case versioncmd.FullCommand():
		runVersion()
	case codesigncmd.FullCommand():
		runCodeSign(*binaryPath)
	}
}

// initConfig reads the given config file into the Config object
func initConfig(filename string) {
	info(fmt.Sprintf("Using config file: %v", filename))

	configData, err := os.ReadFile(filename)
	checkError(err, "Unable to read config file: "+filename)
	config = NewConfig()
	err = yaml.UnmarshalStrict(configData, config)
	checkError(err, "Unable to parse config file: "+filename)
}

// info prints the given message only if running in verbose mode
func info(message string) {
	if *verbose {
		fmt.Println(message)
	}
}

// warn prints a non-fatal error
func warn(err error) {
	if *verbose {
		fmt.Fprintf(os.Stderr, `/!\ %+v\n`, err)
	} else {
		fmt.Fprintln(os.Stderr, `/!\`, err)
	}
}

// printErr prints a error
func printErr(err error) {
	if *verbose {
		fmt.Fprintf(os.Stderr, "!! %+v\n", err)
	} else {
		fmt.Fprintln(os.Stderr, "!!", err)
	}
}

// fatal prints a error and exit
func fatal(err error) {
	printErr(err)
	os.Exit(1)
}

// shellOutput executes a shell command and returns the trimmed output
func shellOutput(cmd string) string {
	args := strings.Fields(cmd)
	out, _ := exec.Command(args[0], args[1:]...).Output()
	return strings.Trim(string(out), " \n\r")
}

// fileExists checks if a file exists and is not a directory
func fileExists(path ...string) bool {
	finfo, err := os.Stat(filepath.Join(path...))
	if err == nil && !finfo.IsDir() {
		return true
	}
	if os.IsNotExist(err) || finfo.IsDir() {
		return false
	}
	if err != nil {
		fatal(err)
	}
	return true
}

// readFile reads a file and return the trimmed output
func readFile(path string) string {
	data, err := os.ReadFile(path)
	if err != nil {
		return ""
	}
	return strings.Trim(string(data), "\n\r ")
}

func optArg(args []string, i int, def string) string {
	if i+1 > len(args) {
		return def
	}
	return args[i]
}

func envOr(name, def string) string {
	s := os.Getenv(name)
	if s == "" {
		return def
	}
	return s
}

func stringInSlice(needle string, haystack []string) bool {
	for _, hay := range haystack {
		if hay == needle {
			return true
		}
	}
	return false
}

func inSliceRE(needle *regexp.Regexp, haystack []string) []string {
	var list []string
	for _, hay := range haystack {
		if needle.MatchString(hay) {
			list = append(list, hay)
		}
	}
	return list
}

func stringInMapKeys(needle string, haystack map[string][]string) bool {
	_, ok := haystack[needle]
	return ok
}

// checkError prints the message and exits if the error is not nil
func checkError(e error, message string) {
	if e != nil {
		fmt.Println(message)
		fatal(e)
	}
}
0707010000001F000081A40000000000000000000000016623999300001691000000000000000000000000000000000000001C00000000promu-0.17.0/cmd/release.go// Copyright © 2016 Prometheus Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
	"context"
	"errors"
	"fmt"
	"os"
	"path/filepath"
	"strings"
	"time"

	"github.com/google/go-github/v25/github"
	"golang.org/x/oauth2"

	"github.com/prometheus/promu/pkg/changelog"
	"github.com/prometheus/promu/util/retry"
)

var (
	releasecmd     = app.Command("release", "Upload all release files to the Github release")
	timeout        = releasecmd.Flag("timeout", "Upload timeout").Duration()
	allowedRetries = releasecmd.Flag("retry", "Number of retries to perform when upload fails").
			Default("2").Int()
	releaseLocation = releasecmd.Arg("location", "Location of files to release").Default(".").Strings()
)

func runRelease(location string) {
	token := os.Getenv("GITHUB_TOKEN")
	if len(token) == 0 {
		fatal(errors.New("GITHUB_TOKEN not defined"))
	}

	ctx := context.Background()
	if *timeout != time.Duration(0) {
		var cancel context.CancelFunc
		ctx, cancel = context.WithTimeout(ctx, *timeout)
		defer cancel()
	}
	client := github.NewClient(
		oauth2.NewClient(
			ctx,
			oauth2.StaticTokenSource(
				&oauth2.Token{AccessToken: token},
			),
		),
	)

	semVer, err := projInfo.ToSemver()
	if err != nil {
		fatal(err)
	}

	// Find the GitHub release matching with the tag. We need to list all
	// releases because it is the only way to get draft releases too.
	var (
		release *github.RepositoryRelease
		opts    = &github.ListOptions{}
		tag     = fmt.Sprintf("v%s", projInfo.Version)
	)
	for {
		releases, resp, err := client.Repositories.ListReleases(ctx, projInfo.Owner, projInfo.Name, opts)
		if err != nil {
			fatal(fmt.Errorf("failed to list releases: %w", err))
		}
		for _, r := range releases {
			if r.GetTagName() == tag {
				release = r
				break
			}
		}
		if release != nil || resp.NextPage == 0 {
			break
		}
		opts.Page = resp.NextPage
	}
	if release == nil {
		f, err := os.Open("CHANGELOG.md")
		if err != nil {
			fatal(err)
		}
		defer f.Close()

		entry, err := changelog.ReadEntry(f, projInfo.Version)
		if err != nil {
			fatal(err)
		}
		name := entry.Name()
		// Create a draft release if none exists already.
		draft := true
		prerelease := semVer.Prerelease() != ""
		release, _, err = client.Repositories.CreateRelease(
			ctx,
			projInfo.Owner,
			projInfo.Name,
			&github.RepositoryRelease{
				TagName:         &tag,
				TargetCommitish: &projInfo.Revision,
				Name:            &name,
				Body:            &entry.Text,
				Draft:           &draft,
				Prerelease:      &prerelease,
			})
		if err != nil {
			fatal(fmt.Errorf("failed to create a draft release for %s: %w", projInfo.Version, err))
		}
	}

	if err := filepath.Walk(location, releaseFile(ctx, client, release)); err != nil {
		// Remove incomplete assets.
		// See https://developer.github.com/v3/repos/releases/#response-for-upstream-failure
		opts = &github.ListOptions{}
		for {
			assets, resp, err := client.Repositories.ListReleaseAssets(ctx, projInfo.Owner, projInfo.Name, release.GetID(), opts)
			if err != nil {
				break
			}
			for _, asset := range assets {
				if strings.EqualFold(asset.GetState(), "starter") {
					_, _ = client.Repositories.DeleteReleaseAsset(ctx, projInfo.Owner, projInfo.Name, asset.GetID())
				}
			}
			if resp.NextPage == 0 {
				break
			}
			opts.Page = resp.NextPage
		}
		fatal(fmt.Errorf("failed to upload all files: %w", err))
	}
}

func releaseFile(ctx context.Context, client *github.Client, release *github.RepositoryRelease) func(string, os.FileInfo, error) error {
	return func(path string, fi os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if fi.IsDir() {
			return nil
		}

		// Check if the asset has already been uploaded and remove it if it is a draft release.
		filename := filepath.Base(path)
		opts := &github.ListOptions{}
		for {
			assets, resp, err := client.Repositories.ListReleaseAssets(ctx, projInfo.Owner, projInfo.Name, release.GetID(), opts)
			if err != nil {
				return fmt.Errorf("failed to list release assets: %w", err)
			}
			var stop bool
			for _, asset := range assets {
				if asset.GetName() == filename {
					var err error
					stop = true
					if release.GetDraft() {
						_, err = client.Repositories.DeleteReleaseAsset(ctx, projInfo.Owner, projInfo.Name, asset.GetID())
						if err != nil {
							err = fmt.Errorf("failed to delete existing asset %q: %w", filename, err)
						}
					} else {
						err = fmt.Errorf("%q already exists", filename)
					}
					if err != nil {
						return err
					}
					break
				}
			}
			if stop || resp.NextPage == 0 {
				break
			}
			opts.Page = resp.NextPage
		}

		maxAttempts := *allowedRetries + 1
		err = retry.Do(func(attempt int) (bool, error) {
			again := attempt < maxAttempts

			f, err := os.Open(path)
			if err != nil {
				return again, err
			}

			_, _, err = client.Repositories.UploadReleaseAsset(
				ctx,
				projInfo.Owner, projInfo.Name, release.GetID(),
				&github.UploadOptions{Name: filename},
				f)
			if err != nil {
				time.Sleep(2 * time.Second)
			}

			return again, err
		})
		if err != nil {
			return fmt.Errorf("failed to upload %q after %d attempts: %w", filename, maxAttempts, err)
		}
		fmt.Println(" > uploaded", filename)

		return nil
	}
}
07070100000020000081A40000000000000000000000016623999300000D18000000000000000000000000000000000000001C00000000promu-0.17.0/cmd/tarball.go// Copyright © 2016 Prometheus Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
	"archive/zip"
	"fmt"
	"io"
	"os"
	"path/filepath"
	"strings"

	kingpin "github.com/alecthomas/kingpin/v2"

	"github.com/prometheus/promu/util/sh"
)

var (
	tarballcmd = app.Command("tarball", "Create a tarball from the built Go project")

	tarballPrefixSet bool
	tarballPrefix    = tarballcmd.Flag("prefix", "Specific dir to store tarballs").
				PreAction(func(c *kingpin.ParseContext) error {
			tarballPrefixSet = true
			return nil
		}).
		Default(".").String()

	tarBinariesLocation = tarballcmd.Arg("location", "location of binaries to tar").Default(".").Strings()
)

func runTarball(binariesLocation string) {
	if tarballPrefixSet {
		config.Tarball.Prefix = *tarballPrefix
	}

	var (
		prefix = config.Tarball.Prefix
		tmpDir = ".release"
		goos   = envOr("GOOS", goos)
		goarch = envOr("GOARCH", goarch)
		name   = fmt.Sprintf("%s-%s.%s-%s", projInfo.Name, projInfo.Version, goos, goarch)

		binaries = config.Build.Binaries
		ext      string
	)

	if goos == "windows" {
		ext = ".exe"
	}

	dir := filepath.Join(tmpDir, name)

	if err := os.MkdirAll(dir, 0o777); err != nil {
		fatal(fmt.Errorf("Failed to create directory: %w", err))
	}
	defer sh.RunCommand("rm", "-rf", tmpDir)

	projectFiles := config.Tarball.Files
	for _, file := range projectFiles {
		sh.RunCommand("cp", "-a", file, dir)
	}

	for _, binary := range binaries {
		binaryName := fmt.Sprintf("%s%s", binary.Name, ext)
		sh.RunCommand("cp", "-a", filepath.Join(binariesLocation, binaryName), dir)
	}

	if !fileExists(prefix) {
		os.Mkdir(prefix, 0o777)
	}

	tar := fmt.Sprintf("%s.tar.gz", name)
	fmt.Println(" >  ", tar)
	sh.RunCommand("tar", "zcf", filepath.Join(prefix, tar), "-C", tmpDir, name)

	// Windows systems don't have tar available by default. Produce archives in
	// the common zip format additionally.
	if goos == "windows" {
		archive := name + ".zip"
		fmt.Println(" >  ", archive)
		if err := createZIP(filepath.Join(prefix, archive), dir); err != nil {
			fatal(fmt.Errorf("Could not create ZIP archive: %w", err))
		}
	}
}

// createZIP creates a ZIP archive at the given path containing the specified
// directory.
func createZIP(path, dir string) error {
	f, err := os.Create(path)
	if err != nil {
		return err
	}
	defer f.Close()

	w := zip.NewWriter(f)
	defer w.Close()

	prefix := filepath.Dir(dir)
	walker := func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if info.IsDir() {
			return nil
		}

		r, err := os.Open(path)
		if err != nil {
			return err
		}
		defer r.Close()

		name := strings.TrimLeft(strings.TrimPrefix(path, prefix), "/")
		f, err := w.Create(name)
		if err != nil {
			return err
		}

		_, err = io.Copy(f, r)
		return err
	}
	return filepath.Walk(dir, walker)
}
07070100000021000081A400000000000000000000000166239993000003AF000000000000000000000000000000000000001C00000000promu-0.17.0/cmd/version.go// Copyright © 2016 Prometheus Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
	"fmt"

	"github.com/prometheus/common/version"
)

var (
	versioncmd = app.Command("version", "Print the version and exit")
	short      = versioncmd.Flag("short", "Print shorter version").Short('s').Bool()
)

func runVersion() {
	if *short {
		fmt.Printf(version.Version)
	} else {
		fmt.Println(version.Print("promu"))
	}
}
07070100000022000041ED0000000000000000000000026623999300000000000000000000000000000000000000000000001100000000promu-0.17.0/doc07070100000023000041ED0000000000000000000000026623999300000000000000000000000000000000000000000000001A00000000promu-0.17.0/doc/examples07070100000024000041ED0000000000000000000000026623999300000000000000000000000000000000000000000000002000000000promu-0.17.0/doc/examples/basic07070100000025000081A40000000000000000000000016623999300000088000000000000000000000000000000000000002B00000000promu-0.17.0/doc/examples/basic/.promu.ymlrepository:
    path: github.com/prometheus/promu
build:
    binaries:
        - name: basic-example
          path: doc/examples/basic
07070100000026000081A40000000000000000000000016623999300000044000000000000000000000000000000000000002A00000000promu-0.17.0/doc/examples/basic/README.mdBasic example

Run `promu build` to build the `basic-example` binary07070100000027000081A40000000000000000000000016623999300000004000000000000000000000000000000000000002800000000promu-0.17.0/doc/examples/basic/VERSION0.1
07070100000028000081A4000000000000000000000001662399930000008C000000000000000000000000000000000000002E00000000promu-0.17.0/doc/examples/basic/alt-promu.ymlrepository:
    path: github.com/prometheus/promu
build:
    binaries:
        - name: alt-basic-example
          path: doc/examples/basic
07070100000029000081A400000000000000000000000166239993000000BC000000000000000000000000000000000000002F00000000promu-0.17.0/doc/examples/basic/extldflags.ymlrepository:
    path: github.com/prometheus/promu
build:
    binaries:
        - name: extldflags
          path: doc/examples/basic
    extldflags:
      - -ltesting
      - -ltesting01

0707010000002A000081A400000000000000000000000166239993000002A3000000000000000000000000000000000000002800000000promu-0.17.0/doc/examples/basic/main.go// Copyright © 2018 Prometheus Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"fmt"
)

func main() {
	fmt.Println("Promu example")
}
0707010000002B000041ED0000000000000000000000026623999300000000000000000000000000000000000000000000002500000000promu-0.17.0/doc/examples/crossbuild0707010000002C000081A40000000000000000000000016623999300000117000000000000000000000000000000000000003000000000promu-0.17.0/doc/examples/crossbuild/.promu.ymlrepository:
    path: github.com/prometheus/promu
build:
    binaries:
        - name: crossbuild-example
          path: doc/examples/crossbuild
crossbuild:
    platforms:
        - linux/amd64
        - linux/386
        - windows/amd64
tarball:
    files:
        - README.md
0707010000002D000081A40000000000000000000000016623999300000380000000000000000000000000000000000000002E00000000promu-0.17.0/doc/examples/crossbuild/Makefile# Copyright 2018 The Prometheus Authors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# The crossbuild command sets the PREFIX env var for each platform
PREFIX                  ?= $(shell pwd)

# Workaround to mimic promu's build behavior which usually handles the extension.
ifeq ($(GOOS),windows)
	EXTENSION = .exe
endif

build:
	go build -o $(PREFIX)/crossbuild-example$(EXTENSION)

test:
	go test
0707010000002E000081A40000000000000000000000016623999300000106000000000000000000000000000000000000002F00000000promu-0.17.0/doc/examples/crossbuild/README.mdCrossbuild example

Run `promu crossbuild` to crossbuild for linux-386, linux-amd64, and windows-amd64 platforms.
Output will be in the `.build` directory.

Run `promu crossbuild tarballs` to build platform tarballs.
Output will be in the `.tarballs` directory.
0707010000002F000081A40000000000000000000000016623999300000004000000000000000000000000000000000000002D00000000promu-0.17.0/doc/examples/crossbuild/VERSION0.1
07070100000030000081A400000000000000000000000166239993000002AE000000000000000000000000000000000000002D00000000promu-0.17.0/doc/examples/crossbuild/main.go// Copyright © 2018 Prometheus Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"fmt"
)

func main() {
	fmt.Println("Promu crossbuild example")
}
07070100000031000041ED0000000000000000000000026623999300000000000000000000000000000000000000000000002500000000promu-0.17.0/doc/examples/prometheus07070100000032000081A4000000000000000000000001662399930000050E000000000000000000000000000000000000003000000000promu-0.17.0/doc/examples/prometheus/.promu.ymlgo:
    version: 1.15.1
    cgo: false
repository:
    path: github.com/prometheus/prometheus
build:
    prefix: .
    binaries:
        - name: prometheus
          path: ./cmd/prometheus
        - name: promtool
          path: ./cmd/promtool
    flags: -a -tags netgo
    ldflags: |
        -s
        -X {{repoPath}}/version.Version={{.Version}}
        -X {{repoPath}}/version.Revision={{.Revision}}
        -X {{repoPath}}/version.Branch={{.Branch}}
        -X {{repoPath}}/version.BuildUser={{user}}@{{host}}
        -X {{repoPath}}/version.BuildDate={{date "20060102-15:04:05"}}
tarball:
    prefix: .
    files:
        - consoles
        - console_libraries
        - documentation/examples/prometheus.yml
        - LICENSE
        - NOTICE
crossbuild:
    platforms:
        - linux/amd64
        - linux/386
        - darwin/amd64
        - windows/amd64
        - windows/386
        - freebsd/amd64
        - freebsd/386
        - openbsd/amd64
        - openbsd/386
        - netbsd/amd64
        - netbsd/386
        - dragonfly/amd64
        - linux/arm
        - linux/arm64
        - freebsd/arm
        - openbsd/arm
        - netbsd/arm
        - aix/ppc64
        - linux/ppc64
        - linux/ppc64le
        - linux/mips64
        - linux/mips64le
        - linux/s390x
07070100000033000081A4000000000000000000000001662399930000003A000000000000000000000000000000000000002F00000000promu-0.17.0/doc/examples/prometheus/README.mdPrometheus example

Run `promu build` to build prometheus
07070100000034000041ED0000000000000000000000026623999300000000000000000000000000000000000000000000002200000000promu-0.17.0/doc/examples/tarball07070100000035000081A400000000000000000000000166239993000000B4000000000000000000000000000000000000002D00000000promu-0.17.0/doc/examples/tarball/.promu.ymlrepository:
    path: github.com/prometheus/promu
build:
    binaries:
        - name: tarball-example
          path: doc/examples/tarball
tarball:
    files:
        - README.md
07070100000036000081A4000000000000000000000001662399930000005E000000000000000000000000000000000000002C00000000promu-0.17.0/doc/examples/tarball/README.mdTarball example

Run `promu build && promu tarball` to build the binary and create a tarball.
07070100000037000081A40000000000000000000000016623999300000004000000000000000000000000000000000000002A00000000promu-0.17.0/doc/examples/tarball/VERSION0.1
07070100000038000081A400000000000000000000000166239993000002AB000000000000000000000000000000000000002A00000000promu-0.17.0/doc/examples/tarball/main.go// Copyright © 2018 Prometheus Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"fmt"
)

func main() {
	fmt.Println("Promu tarball example")
}
07070100000039000081A4000000000000000000000001662399930000021A000000000000000000000000000000000000001400000000promu-0.17.0/go.modmodule github.com/prometheus/promu

go 1.21

require (
	github.com/Masterminds/semver/v3 v3.2.1
	github.com/alecthomas/kingpin/v2 v2.4.0
	github.com/google/go-github/v25 v25.1.3
	github.com/prometheus/common v0.52.3
	go.uber.org/atomic v1.11.0
	golang.org/x/oauth2 v0.19.0
	gopkg.in/yaml.v2 v2.4.0
)

require (
	github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
	github.com/google/go-querystring v1.0.0 // indirect
	github.com/kr/pretty v0.3.1 // indirect
	github.com/xhit/go-str2duration/v2 v2.1.0 // indirect
)
0707010000003A000081A4000000000000000000000001662399930000130F000000000000000000000000000000000000001400000000promu-0.17.0/go.sumgithub.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0=
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
github.com/alecthomas/kingpin/v2 v2.4.0 h1:f48lwail6p8zpO1bC4TxtqACaGqHYA22qkHjHpqDjYY=
github.com/alecthomas/kingpin/v2 v2.4.0/go.mod h1:0gyi0zQnjuFk8xrkNKamJoyUo382HRL7ATRpFZCw6tE=
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 h1:s6gZFSlWYmbqAuRjVTiNNhvNRfY2Wxp9nhfyel4rklc=
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-github/v25 v25.1.3 h1:Ht4YIQgUh4l4lc80fvGnw60khXysXvlgPxPP8uJG3EA=
github.com/google/go-github/v25 v25.1.3/go.mod h1:6z5pC69qHtrPJ0sXPsj4BLnd82b+r6sLB7qcBoRZqpw=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/common v0.52.3 h1:5f8uj6ZwHSscOGNdIQg6OiZv/ybiK2CO2q2drVZAQSA=
github.com/prometheus/common v0.52.3/go.mod h1:BrxBKv3FWBIGXw89Mg1AeBq7FSyRzXWI3l3e7W3RN5U=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc=
github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU=
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.19.0 h1:9+E/EZBCbTLNrbN35fHv/a/d/mOBatymz1zbtQrXpIg=
golang.org/x/oauth2 v0.19.0/go.mod h1:vYi7skDa1x015PmRRYZ7+s1cWyPgrPiSYRe4rnsexc8=
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
0707010000003B000081A400000000000000000000000166239993000002AB000000000000000000000000000000000000001500000000promu-0.17.0/main.go// Copyright © 2016 Prometheus Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import "github.com/prometheus/promu/cmd"

func main() {
	cmd.Execute()
}
0707010000003C000081A4000000000000000000000001662399930000184A000000000000000000000000000000000000001A00000000promu-0.17.0/main_test.go// Copyright © 2018 Prometheus Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"fmt"
	"go/build"
	"log"
	"os"
	"os/exec"
	"path"
	"path/filepath"
	"strings"
	"testing"
)

const (
	examplesDir    = "doc/examples"
	testOutputDir  = "testoutput"
	promuCmdBinary = "promu"
)

var (
	goos   = build.Default.GOOS
	goarch = build.Default.GOARCH

	promuBinaryRelPath      = path.Join(testOutputDir, promuCmdBinary)
	promuBinaryAbsPath, _   = filepath.Abs(promuBinaryRelPath)
	promuExamplesBasic      = path.Join(examplesDir, "basic")
	promuExamplesCrossbuild = path.Join(examplesDir, "crossbuild")
	promuExamplesTarball    = path.Join(examplesDir, "tarball")
)

func TestMain(m *testing.M) {
	setup()
	result := m.Run()
	os.Exit(result)
}

// setup any prerequisites for the tests
func setup() {
	err := os.Mkdir(examplesDir, os.ModePerm)
	if !os.IsExist(err) && err != nil {
		log.Fatal(err)
	}
	cmd := exec.Command("go", "build", "-o", promuBinaryAbsPath)
	output, err := cmd.Output()
	if err != nil {
		log.Fatal(err, string(output))
	}
}

func errcheck(t *testing.T, err error, output string) {
	if err != nil {
		log.Print(output)
		t.Error(err)
	}
}

func assertTrue(t *testing.T, cond bool) {
	if !cond {
		t.Error("condition isn't true")
	}
}

func assertFileExists(t *testing.T, filepath string) {
	if _, err := os.Stat(filepath); os.IsNotExist(err) {
		log.Print("file does not exist: ", filepath)
		t.Error(err)
	}
}

func createSymlink(t *testing.T, target, newlink string) {
	if _, err := os.Stat(newlink); os.IsNotExist(err) {
		err = os.Symlink(target, newlink)
		errcheck(t, err, "Unable to create symlink "+newlink)
	}
}

func dockerAvailable() bool {
	cmd := exec.Command("docker", "info")
	err := cmd.Run()
	return err == nil
}

func TestPromuInfo(t *testing.T) {
	cmd := exec.Command(promuBinaryAbsPath, "info")
	output, err := cmd.CombinedOutput()
	errcheck(t, err, string(output))
	if !strings.HasPrefix(string(output), "Name: promu") {
		t.Error("incorrect output for 'info' command: ", string(output))
	}
}

func TestPromuBuild_Basic(t *testing.T) {
	outputDir := path.Join(testOutputDir, "basic")
	err := os.MkdirAll(outputDir, os.ModePerm)
	errcheck(t, err, "Unable to create output dir")

	createSymlink(t, path.Join("..", "..", promuExamplesBasic, ".promu.yml"),
		path.Join(outputDir, ".promu.yml"))

	cmd := exec.Command(promuBinaryAbsPath, "build")
	cmd.Dir = outputDir
	output, err := cmd.CombinedOutput()
	errcheck(t, err, string(output))

	assertFileExists(t, path.Join(outputDir, "basic-example"))
}

func TestPromuBuild_AltConfig(t *testing.T) {
	outputDir := path.Join(testOutputDir, "altconf")
	promuConfig := path.Join(promuExamplesBasic, "alt-promu.yml")
	cmd := exec.Command(promuBinaryAbsPath, "build", "--config", promuConfig, "--prefix", outputDir)
	output, err := cmd.CombinedOutput()
	errcheck(t, err, string(output))

	assertFileExists(t, path.Join(outputDir, "alt-basic-example"))
}

func TestPromuBuild_ExtLDFlags(t *testing.T) {
	outputDir := path.Join(testOutputDir, "extldflags")
	promuConfig := path.Join(promuExamplesBasic, "extldflags.yml")
	cmd := exec.Command(promuBinaryAbsPath, "build", "-v", "--config", promuConfig, "--prefix", outputDir)
	output, err := cmd.CombinedOutput()
	assertTrue(t, strings.Contains(string(output), "-extldflags '-ltesting -ltesting01 -static'"))
	errcheck(t, err, string(output))
	assertFileExists(t, path.Join(outputDir, "extldflags"))
}

func TestTarball(t *testing.T) {
	outputDir := path.Join(testOutputDir, "tarball")
	err := os.MkdirAll(outputDir, os.ModePerm)
	errcheck(t, err, "Unable to create output dir")

	createSymlink(t, path.Join("..", "..", promuExamplesBasic, ".promu.yml"),
		path.Join(outputDir, ".promu.yml"))
	createSymlink(t, path.Join("..", "..", promuExamplesTarball, "README.md"),
		path.Join(outputDir, "README.md"))
	createSymlink(t, path.Join("..", "..", promuExamplesTarball, "VERSION"),
		path.Join(outputDir, "VERSION"))

	cmd := exec.Command(promuBinaryAbsPath, "build")
	cmd.Dir = outputDir
	output, err := cmd.CombinedOutput()
	errcheck(t, err, string(output))

	cmd = exec.Command(promuBinaryAbsPath, "tarball")
	cmd.Dir = outputDir
	output, err = cmd.CombinedOutput()
	errcheck(t, err, string(output))

	tarfileName := fmt.Sprintf("promu-0.1.%s-%s.tar.gz", goos, goarch)
	assertFileExists(t, path.Join(outputDir, tarfileName))
}

func TestPromuCrossbuild(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping crossbuild test in short mode.")
	}
	if !dockerAvailable() {
		t.Error("unable to connect to docker daemon.")
		return
	}

	cmd := exec.Command(promuBinaryAbsPath, "crossbuild")
	cmd.Dir = promuExamplesCrossbuild
	output, err := cmd.CombinedOutput()
	errcheck(t, err, string(output))
	assertFileExists(t, path.Join(promuExamplesCrossbuild, ".build", "linux-386", "crossbuild-example"))
	assertFileExists(t, path.Join(promuExamplesCrossbuild, ".build", "linux-amd64", "crossbuild-example"))
	assertFileExists(t, path.Join(promuExamplesCrossbuild, ".build", "windows-amd64", "crossbuild-example.exe"))

	cmd = exec.Command(promuBinaryAbsPath, "crossbuild", "tarballs")
	cmd.Dir = promuExamplesCrossbuild
	defer os.RemoveAll(path.Join(promuExamplesCrossbuild, ".tarballs"))
	output, err = cmd.CombinedOutput()
	errcheck(t, err, string(output))
	assertFileExists(t, path.Join(promuExamplesCrossbuild, ".tarballs", "promu-0.1.linux-386.tar.gz"))
	assertFileExists(t, path.Join(promuExamplesCrossbuild, ".tarballs", "promu-0.1.linux-amd64.tar.gz"))
	assertFileExists(t, path.Join(promuExamplesCrossbuild, ".tarballs", "promu-0.1.windows-amd64.tar.gz"))
	assertFileExists(t, path.Join(promuExamplesCrossbuild, ".tarballs", "promu-0.1.windows-amd64.zip"))
}
0707010000003D000041ED0000000000000000000000026623999300000000000000000000000000000000000000000000001100000000promu-0.17.0/pkg0707010000003E000081A4000000000000000000000001662399930000008B000000000000000000000000000000000000001B00000000promu-0.17.0/pkg/README.mdThe `pkg` directory is deprecated.
Please do not add new packages to this directory.
Existing packages will be moved elsewhere eventually.
0707010000003F000041ED0000000000000000000000026623999300000000000000000000000000000000000000000000001B00000000promu-0.17.0/pkg/changelog07070100000040000081A400000000000000000000000166239993000010BC000000000000000000000000000000000000002800000000promu-0.17.0/pkg/changelog/changelog.go// Copyright © 2019 Prometheus Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package changelog

import (
	"bufio"
	"fmt"
	"io"
	"regexp"
	"sort"
	"strings"
	"time"
)

// Kind represents the type of a change.
type Kind int

const (
	kindChange = iota
	kindFeature
	kindEnhancement
	kindBugfix
)

func (k Kind) String() string {
	switch k {
	case kindChange:
		return "CHANGE"
	case kindFeature:
		return "FEATURE"
	case kindEnhancement:
		return "ENHANCEMENT"
	case kindBugfix:
		return "BUGFIX"
	}
	return ""
}

// Kinds is a list of Kind which implements sort.Interface.
type Kinds []Kind

func (k Kinds) Len() int           { return len(k) }
func (k Kinds) Less(i, j int) bool { return k[i] < k[j] }
func (k Kinds) Swap(i, j int)      { k[i], k[j] = k[j], k[i] }

// ParseKinds converts a slash-separated list of Kind to a list of Kind.
func ParseKinds(s string) Kinds {
	m := make(map[Kind]struct{})
	for _, k := range strings.Split(s, "/") {
		switch k {
		case "CHANGE":
			m[kindChange] = struct{}{}
		case "FEATURE":
			m[kindFeature] = struct{}{}
		case "ENHANCEMENT":
			m[kindEnhancement] = struct{}{}
		case "BUGFIX":
			m[kindBugfix] = struct{}{}
		}
	}

	var kinds Kinds
	for k := range m {
		kinds = append(kinds, k)
	}
	sort.Stable(kinds)
	return kinds
}

func (k Kinds) String() string {
	var s []string
	for i := range k {
		s = append(s, k[i].String())
	}
	return strings.Join(s, "/")
}

// Change represents a change description.
type Change struct {
	Text  string
	Kinds Kinds
}

type Changes []Change

func (c Changes) Sorted() error {
	less := func(k1, k2 Kinds) bool {
		if len(k1) == 0 {
			return len(k2) == 0
		}
		if len(k2) == 0 {
			return true
		}

		n := len(k1)
		if len(k1) > len(k2) {
			n = len(k2)
		}
		for j := 0; j < n; j++ {
			if k1[j] == k2[j] {
				continue
			}
			return k1[j] < k2[j]
		}
		return len(k1) <= len(k2)
	}

	for i := 0; i < len(c)-1; i++ {
		k1, k2 := c[i].Kinds, c[i+1].Kinds
		if !less(k1, k2) {
			return fmt.Errorf("%q should be after %q", c[i].Text, c[i+1].Text)
		}
	}
	return nil
}

// Entry represents an entry in the changelog.
type Entry struct {
	Version string
	Date    time.Time
	Changes Changes
	Text    string
}

const dateFormat = "2006-01-02"

// Name returns the canonical name of the entry.
func (c Entry) Name() string {
	return fmt.Sprintf("%s / %s", c.Version, c.Date.Format(dateFormat))
}

// ReadEntry reads the entry for the given version from the changelog file.
// It returns an error if the version is not found.
func ReadEntry(r io.Reader, version string) (*Entry, error) {
	reHeader, err := regexp.Compile(fmt.Sprintf(`^#{1,2} %s / (\d{4}-\d{2}-\d{2})`, regexp.QuoteMeta(version)))
	if err != nil {
		return nil, err
	}
	reChange := regexp.MustCompile(`^\* \[([^\]]+)\]`)

	var (
		reading bool
		lines   []string

		entry   = Entry{Version: version}
		scanner = bufio.NewScanner(r)
	)
	for (len(lines) == 0 || reading) && scanner.Scan() {
		line := scanner.Text()
		m := reHeader.FindStringSubmatch(line)
		switch {
		case len(m) > 0:
			reading = true
			t, err := time.Parse(dateFormat, m[1])
			if err != nil {
				return nil, fmt.Errorf("invalid changelog date: %w", err)
			}
			entry.Date = t
		case strings.HasPrefix(line, "## "):
			reading = false
		case reading:
			if len(lines) == 0 && strings.TrimSpace(line) == "" {
				continue
			}
			m := reChange.FindStringSubmatch(line)
			if len(m) > 1 {
				entry.Changes = append(entry.Changes, Change{Text: line, Kinds: ParseKinds(m[1])})
			}
			lines = append(lines, line)
		}
	}

	if entry.Date.IsZero() {
		return nil, fmt.Errorf(
			"unable to locate release information in changelog for version %q, expected format: %q",
			version,
			reHeader)
	}

	entry.Text = strings.Join(lines, "\n")
	return &entry, nil
}
07070100000041000081A40000000000000000000000016623999300001DA3000000000000000000000000000000000000002D00000000promu-0.17.0/pkg/changelog/changelog_test.go// Copyright 2019 The Prometheus Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package changelog

import (
	"bytes"
	"reflect"
	"testing"
	"time"
)

func TestReadEntry(t *testing.T) {
	mustParse := func(s string) time.Time {
		d, err := time.Parse(dateFormat, s)
		if err != nil {
			t.Fatalf("unexpected error: %v", err)
		}
		return d
	}

	for _, tc := range []struct {
		in      string
		version string

		exp Entry
		err bool
	}{
		{
			// Version not found.
			in: `## 1.0.0 / 2016-01-02

* [BUGFIX] Some fix.
* [FEATURE] Some feature.`,
			version: "1.0.0-notfound",

			err: true,
		},
		{
			in: `## 1.0.0 / 2016-01-02

* [BUGFIX] Some fix.
* [FEATURE] Some feature.`,
			version: "1.0.0",

			exp: Entry{
				Version: "1.0.0",
				Date:    mustParse("2016-01-02"),
				Changes: []Change{
					{
						Kinds: []Kind{kindBugfix},
						Text:  "* [BUGFIX] Some fix.",
					},
					{
						Kinds: []Kind{kindFeature},
						Text:  "* [FEATURE] Some feature.",
					},
				},
				Text: `* [BUGFIX] Some fix.
* [FEATURE] Some feature.`,
			},
		},
		{
			in: `# 1.0.0 / 2016-01-02

* [BUGFIX] Some fix.
* [FEATURE] Some feature.`,
			version: "1.0.0",

			exp: Entry{
				Version: "1.0.0",
				Date:    mustParse("2016-01-02"),
				Changes: []Change{
					{
						Kinds: []Kind{kindBugfix},
						Text:  "* [BUGFIX] Some fix.",
					},
					{
						Kinds: []Kind{kindFeature},
						Text:  "* [FEATURE] Some feature.",
					},
				},
				Text: `* [BUGFIX] Some fix.
* [FEATURE] Some feature.`,
			},
		},
		{
			in: `## 1.0.0 / 2016-01-02
		
* [BUGFIX] Some fix.
* [FEATURE] Some feature.

## 0.0.1 / 2016-01-02

* [BUGFIX] Another fix.`,
			version: "1.0.0",

			exp: Entry{
				Version: "1.0.0",
				Date:    mustParse("2016-01-02"),
				Changes: []Change{
					{
						Kinds: []Kind{kindBugfix},
						Text:  "* [BUGFIX] Some fix.",
					},
					{
						Kinds: []Kind{kindFeature},
						Text:  "* [FEATURE] Some feature.",
					},
				},
				Text: `* [BUGFIX] Some fix.
* [FEATURE] Some feature.
`,
			},
		},
		{
			in: `## 1.0.0 / 2016-01-02

[BUGFIX] Some fix.
[FEATURE] Some feature.

## 0.0.1 / 2016-01-01

* [BUGFIX] Another fix.`,
			version: "0.0.1",

			exp: Entry{
				Version: "0.0.1",
				Date:    mustParse("2016-01-01"),
				Changes: []Change{
					{
						Kinds: []Kind{kindBugfix},
						Text:  "* [BUGFIX] Another fix.",
					},
				},
				Text: `* [BUGFIX] Another fix.`,
			},
		},
		{
			in: `## 1.0.0 / 2016-01-03
This is the first stable release.

* [CHANGE/BUGFIX] Some fix.
* [FEATURE] Some feature.

## 0.0.1 / 2016-01-02

* [BUGFIX] Another fix.`,
			version: "1.0.0",

			exp: Entry{
				Version: "1.0.0",
				Date:    mustParse("2016-01-03"),
				Changes: []Change{
					{
						Kinds: []Kind{kindChange, kindBugfix},
						Text:  "* [CHANGE/BUGFIX] Some fix.",
					},
					{
						Kinds: []Kind{kindFeature},
						Text:  "* [FEATURE] Some feature.",
					},
				},
				Text: `This is the first stable release.

* [CHANGE/BUGFIX] Some fix.
* [FEATURE] Some feature.
`,
			},
		},
		{
			in: `## 1.0.0 / 2016-01-04

### Breaking changes!

* [CHANGE] Some change.
* [FEATURE] Some feature.
* [ENHANCEMENT] Some enhancement.

## 0.0.1 / 2016-01-02

* [BUGFIX] Another fix.`,
			version: "1.0.0",

			exp: Entry{
				Version: "1.0.0",
				Date:    mustParse("2016-01-04"),
				Changes: []Change{
					{
						Kinds: []Kind{kindChange},
						Text:  "* [CHANGE] Some change.",
					},
					{
						Kinds: []Kind{kindFeature},
						Text:  "* [FEATURE] Some feature.",
					},
					{
						Kinds: []Kind{kindEnhancement},
						Text:  "* [ENHANCEMENT] Some enhancement.",
					},
				},
				Text: `### Breaking changes!

* [CHANGE] Some change.
* [FEATURE] Some feature.
* [ENHANCEMENT] Some enhancement.
`,
			},
		},
		{
			// Invalid date.
			in:      "## 1.0.0 / 2006-19-02",
			version: "1.0.0",

			err: true,
		},
	} {
		tc := tc
		t.Run("", func(t *testing.T) {
			got, err := ReadEntry(bytes.NewBufferString(tc.in), tc.version)
			if tc.err {
				if err == nil {
					t.Fatal("expected error, got none")
				}
				return
			}
			if err != nil {
				t.Fatalf("expected no error, got %v", err)
			}
			if !reflect.DeepEqual(&tc.exp, got) {
				t.Fatalf("expected:\n%v\ngot:\n%v", tc.exp, got)
			}
		})
	}
}

func TestKinds(t *testing.T) {
	for _, tc := range []struct {
		in  string
		exp Kinds
	}{
		{
			in:  "CHANGE",
			exp: Kinds{kindChange},
		},
		{
			in:  "BUGFIX/CHANGE",
			exp: Kinds{kindChange, kindBugfix},
		},
		{
			in:  "BUGFIX/BUGFIX",
			exp: Kinds{kindBugfix},
		},
		{
			in:  "BUGFIX/INVALID",
			exp: Kinds{kindBugfix},
		},
		{
			in: "INVALID",
		},
	} {
		t.Run("", func(t *testing.T) {
			got := ParseKinds(tc.in)
			if !reflect.DeepEqual(&tc.exp, &got) {
				t.Fatalf("expected:\n%v\ngot:\n%v", tc.exp, got)
			}
		})
	}
}

func TestChangesSorted(t *testing.T) {
	for _, tc := range []struct {
		in Changes

		err bool
	}{
		{
			in: Changes{
				{
					Kinds: Kinds{},
				},
			},
			err: false,
		},
		{
			in: Changes{
				{
					Kinds: Kinds{kindChange},
				},
				{
					Kinds: Kinds{},
				},
			},
			err: false,
		},
		{
			in: Changes{
				{
					Kinds: Kinds{},
				},
				{
					Kinds: Kinds{kindChange},
				},
				{
					Kinds: Kinds{kindFeature},
				},
			},
			err: true,
		},
		{
			in: Changes{
				{
					Kinds: Kinds{kindChange},
				},
				{
					Kinds: Kinds{kindChange},
				},
			},
			err: false,
		},
		{
			in: Changes{
				{
					Kinds: Kinds{kindChange},
				},
				{
					Kinds: Kinds{kindBugfix},
				},
			},
			err: false,
		},
		{
			in: Changes{
				{
					Kinds: Kinds{kindChange},
				},
				{
					Kinds: Kinds{kindBugfix},
				},
				{
					Kinds: Kinds{kindFeature},
				},
			},
			err: true,
		},
		{
			in: Changes{
				{
					Kinds: Kinds{kindChange},
				},
				{
					Kinds: Kinds{kindChange, kindBugfix},
				},
			},
			err: false,
		},
		{
			in: Changes{
				{
					Kinds: Kinds{kindChange, kindBugfix},
				},
				{
					Kinds: Kinds{kindChange},
				},
			},
			err: true,
		},
		{
			in: Changes{
				{
					Kinds: Kinds{kindChange, kindFeature, kindBugfix},
				},
				{
					Kinds: Kinds{kindChange, kindBugfix},
				},
			},
			err: false,
		},
		{
			in: Changes{
				{
					Kinds: Kinds{kindChange, kindBugfix},
				},
				{
					Kinds: Kinds{kindChange, kindFeature, kindBugfix},
				},
			},
			err: true,
		},
		{
			in: Changes{
				{
					Kinds: Kinds{kindChange, kindFeature, kindBugfix},
				},
				{
					Kinds: Kinds{kindChange, kindBugfix},
				},
				{
					Kinds: Kinds{},
				},
				{
					Kinds: Kinds{},
				},
			},
			err: false,
		},
		{
			in: Changes{
				{
					Kinds: Kinds{kindChange, kindFeature, kindBugfix},
				},
				{
					Kinds: Kinds{},
				},
				{
					Kinds: Kinds{kindChange, kindBugfix},
				},
				{
					Kinds: Kinds{},
				},
			},
			err: true,
		},
	} {
		t.Run("", func(t *testing.T) {
			err := tc.in.Sorted()
			if tc.err {
				if err == nil {
					t.Fatal("expected error but got nil")
				}
				return
			}
			if err != nil {
				t.Fatalf("expected no error but got: %v", err)
			}
		})
	}
}
07070100000042000041ED0000000000000000000000026623999300000000000000000000000000000000000000000000001C00000000promu-0.17.0/pkg/repository07070100000043000081A40000000000000000000000016623999300001296000000000000000000000000000000000000002400000000promu-0.17.0/pkg/repository/info.go// Copyright © 2019 Prometheus Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package repository

import (
	"fmt"
	"net/url"
	"os"
	"os/exec"
	"os/user"
	"path/filepath"
	"regexp"
	"strings"

	"github.com/Masterminds/semver/v3"
)

// Info represents current project useful information.
type Info struct {
	Branch   string
	Name     string
	Owner    string
	Repo     string
	Revision string
	Version  string
}

// shellOutput executes a shell command and returns the trimmed output.
func shellOutput(cmd string, arg ...string) string {
	out, _ := shellOutputWithError(cmd, arg...)
	return out
}

// shellOutputWithError executes a shell command and returns the trimmed output and error.
func shellOutputWithError(cmd string, arg ...string) (string, error) {
	out, err := exec.Command(cmd, arg...).Output()
	return strings.Trim(string(out), " \n\r"), err
}

// NewInfo returns a new Info.
func NewInfo(warnf func(error)) (Info, error) {
	if warnf == nil {
		warnf = func(error) {}
	}

	var (
		info Info
		err  error
	)

	cmd := exec.Command("git", "rev-parse", "--show-toplevel")
	cmd.Stdout, cmd.Stderr = nil, nil
	if err := cmd.Run(); err != nil {
		// Not a git repository.
		repo, err := os.Getwd()
		if err != nil {
			return info, fmt.Errorf("couldn't get current working directory: %w", err)
		}
		repo = strings.TrimPrefix(repo, os.Getenv("GOPATH"))
		repo = strings.TrimPrefix(repo, "/src/")

		user, err := user.Current()
		if err != nil {
			return info, fmt.Errorf("couldn't get current user: %w", err)
		}

		info = Info{
			Branch:   "non-git",
			Name:     filepath.Base(repo),
			Owner:    user.Username,
			Repo:     repo,
			Revision: "non-git",
		}
	} else {
		branch, err := shellOutputWithError("git", "rev-parse", "--abbrev-ref", "HEAD")
		if err != nil {
			return info, fmt.Errorf("unable to get the current branch: %w", err)
		}

		remote, err := shellOutputWithError("git", "config", "--get", fmt.Sprintf("branch.%s.remote", branch))
		if err != nil {
			// default to origin.
			remote = "origin"
		}

		repoURL, err := shellOutputWithError("git", "config", "--get", fmt.Sprintf("remote.%s.url", remote))
		if err != nil {
			warnf(fmt.Errorf("unable to get repository location for remote %q: %w", remote, err))
		}
		repo, err := repoLocation(repoURL)
		if err != nil {
			return info, fmt.Errorf("couldn't parse repository location: %q: %w", repoURL, err)
		}
		info = Info{
			Branch:   branch,
			Name:     filepath.Base(repo),
			Owner:    filepath.Base(filepath.Dir(repo)),
			Repo:     repo,
			Revision: shellOutput("git", "rev-parse", "HEAD"),
		}
	}

	info.Version, err = findVersion()
	if err != nil {
		warnf(fmt.Errorf("unable to find project's version: %w", err))
	}
	return info, nil
}

// Convert SCP-like URL to SSH URL(e.g. [user@]host.xz:path/to/repo.git/)
// ref. http://git-scm.com/docs/git-fetch#_git_urls
// (golang hasn't supported Perl-like negative look-behind match)
var (
	hasSchemePattern  = regexp.MustCompile("^[^:]+://")
	scpLikeURLPattern = regexp.MustCompile("^([^@]+@)?([^:]+):/?(.+)$")
)

func repoLocation(repo string) (string, error) {
	if !hasSchemePattern.MatchString(repo) && scpLikeURLPattern.MatchString(repo) {
		matched := scpLikeURLPattern.FindStringSubmatch(repo)
		user := matched[1]
		host := matched[2]
		path := matched[3]
		repo = fmt.Sprintf("ssh://%s%s/%s", user, host, path)
	}

	u, err := url.Parse(repo)
	if err != nil {
		return "", err
	}

	repo = fmt.Sprintf("%s%s", strings.Split(u.Host, ":")[0], u.Path)
	repo = strings.TrimSuffix(repo, ".git")
	return repo, nil
}

func findVersion() (string, error) {
	for _, file := range []string{"VERSION", "version/VERSION"} {
		b, err := os.ReadFile(file)
		if err != nil {
			continue
		}
		return strings.Trim(string(b), "\n\r "), nil
	}

	return strings.TrimPrefix(shellOutput("git", "describe", "--tags", "--always", "--dirty"), "v"), nil
}

// ToSemver returns a *semver.Version from Info.
func (i Info) ToSemver() (*semver.Version, error) {
	if strings.HasPrefix(i.Version, "v") {
		return nil, fmt.Errorf("version %q shouldn't start with 'v'", i.Version)
	}
	semVer, err := semver.NewVersion(i.Version)
	if err != nil {
		return nil, fmt.Errorf("invalid semver version: %w", err)
	}
	return semVer, nil
}
07070100000044000081A40000000000000000000000016623999300000796000000000000000000000000000000000000002900000000promu-0.17.0/pkg/repository/info_test.go// Copyright © 2017 Prometheus Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package repository

import (
	"testing"
)

func TestToSemver(t *testing.T) {
	for _, tc := range []struct {
		version string

		exp string
	}{
		{
			version: "1.0.0",
			exp:     "1.0.0",
		},
		{
			version: "1.0.0-rc0",
			exp:     "1.0.0-rc0",
		},
		{
			version: "x1.0.0-rc0",
		},
		{
			version: "v1.0.0-rc0",
		},
	} {
		tc := tc
		t.Run("", func(t *testing.T) {
			got, err := Info{Version: tc.version}.ToSemver()
			if err != nil {
				if tc.exp != "" {
					t.Fatalf("expected no error, got %v", err)
				}
				return
			}
			if got.String() != tc.exp {
				t.Fatalf("expected %q, got %q", tc.exp, got)
			}
		})
	}
}

func TestRepoLocation(t *testing.T) {
	repoTests := []struct {
		s        string // input
		expected string // expected result
	}{
		{"git@github.com:prometheus/promu.git", "github.com/prometheus/promu"},
		{"https://github.com/prometheus/promu.git", "github.com/prometheus/promu"},
		{"ssh://git@gitlab.fr:22443/prometheus/promu.git", "gitlab.fr/prometheus/promu"},
		{"https://sdurrheimer@gitlab.fr/prometheus/promu.git", "gitlab.fr/prometheus/promu"},
	}

	for _, tc := range repoTests {
		actual, err := repoLocation(tc.s)
		if err != nil {
			t.Errorf("repoLocation(%s): %+v", tc.s, err)
		}
		if actual != tc.expected {
			t.Errorf("repoLocation(%s): expected %s, got %s", tc.s, tc.expected, actual)
		}
	}
}
07070100000045000041ED0000000000000000000000026623999300000000000000000000000000000000000000000000001200000000promu-0.17.0/util07070100000046000041ED0000000000000000000000026623999300000000000000000000000000000000000000000000001800000000promu-0.17.0/util/retry07070100000047000081A400000000000000000000000166239993000003DA000000000000000000000000000000000000002100000000promu-0.17.0/util/retry/retry.go// Copyright © 2016 Prometheus Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package retry

// Func represents functions that can be retried.
type Func func(attempt int) (retry bool, err error)

// Do keeps trying the function until the second argument
// returns false, or no error is returned.
func Do(fn Func) error {
	var err error
	var cont bool
	attempt := 1
	for {
		cont, err = fn(attempt)
		if !cont || err == nil {
			break
		}
		attempt++
	}
	return err
}
07070100000048000041ED0000000000000000000000026623999300000000000000000000000000000000000000000000001500000000promu-0.17.0/util/sh07070100000049000081A400000000000000000000000166239993000004D7000000000000000000000000000000000000001B00000000promu-0.17.0/util/sh/sh.go// Copyright © 2016 Prometheus Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package sh

import (
	"fmt"
	"os"
	"os/exec"
	"regexp"
	"strings"
)

// Verbose enables verbose output
var Verbose bool

// RunCommand executes a shell command.
func RunCommand(name string, arg ...string) error {
	if Verbose {
		cmdText := name + " " + strings.Join(arg, " ")
		fmt.Fprintln(os.Stderr, " + ", cmdText)
	}
	cmd := exec.Command(name, arg...)
	cmd.Stdout = os.Stdout
	cmd.Stdin = os.Stdin
	cmd.Stderr = os.Stderr
	return cmd.Run()
}

// SplitParameters splits shell command parameters, taking quoting in account.
func SplitParameters(s string) []string {
	r := regexp.MustCompile(`'[^']*'|[^ ]+`)
	return r.FindAllString(s, -1)
}
0707010000004A000081A400000000000000000000000166239993000003B3000000000000000000000000000000000000002000000000promu-0.17.0/util/sh/sh_test.go// Copyright 2018 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package sh

import (
	"strings"
	"testing"
)

func TestSplitParameters(t *testing.T) {
	in := `-a -tags 'netgo static_build'`
	expect := []string{"-a", "-tags", `'netgo static_build'`}
	got := SplitParameters(in)
	for i, g := range got {
		if expect[i] != g {
			t.Error("expected", expect[i], "got", g, "full output: ", strings.Join(got, "#"))
		}
	}
}
07070100000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000B00000000TRAILER!!!246 blocks
openSUSE Build Service is sponsored by