Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add little tool to archive go.mod code to git.
- Loading branch information
0 parents
commit 1c0e632
Showing
4 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
BSD 3-Clause License | ||
|
||
Copyright (c) 2021 Tailscale & AUTHORS. | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
1. Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
|
||
2. Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
|
||
3. Neither the name of the copyright holder nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// The go-mod-archiver command pushes a git tag named after the SHA-256 of the | ||
// go.mod, containing all the vendored code from go.mod. | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"crypto/sha256" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"os/exec" | ||
) | ||
|
||
func main() { | ||
goMod, err := ioutil.ReadFile("go.mod") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
modSum := fmt.Sprintf("%x", sha256.Sum256(goMod)) | ||
ref := fmt.Sprintf("archive/gomod/%v", modSum) | ||
|
||
lsRemote, err := exec.Command("git", "ls-remote").CombinedOutput() | ||
if err != nil { | ||
log.Fatalf("git ls-remote: %v, %s", err, lsRemote) | ||
} | ||
if bytes.Contains(lsRemote, []byte(ref)) { | ||
// A ref already exists, so go.mod hasn't changed and | ||
// it's already archived. This is the common case. | ||
log.Printf("go.mod already archived as %v", ref) | ||
return | ||
} | ||
run(exec.Command("go", "mod", "vendor")) | ||
if _, err := os.Stat("vendor"); os.IsNotExist(err) { | ||
out, err := exec.Command("go", "mod", "graph").Output() | ||
if err != nil { | ||
log.Fatalf("go mod graph: %v", err) | ||
} | ||
if len(out) == 0 { | ||
log.Printf("no deps; nothing to do") | ||
return | ||
} | ||
log.Fatalf("'go mod graph' shows dependencies but vendor folder absent after a 'go mod vendor'") | ||
} | ||
run(exec.Command("git", "add", "vendor")) | ||
msg := fmt.Sprintf("go mod vendor from a go.mod with SHA-256 of %v", modSum) | ||
run(exec.Command("git", "commit", "-m", msg)) | ||
run(exec.Command("git", "tag", "-a", ref, "-m", msg)) | ||
run(exec.Command("git", "push", "origin", ref)) | ||
log.Printf("Pushed %v", ref) | ||
} | ||
|
||
func run(cmd *exec.Cmd) { | ||
out, err := cmd.CombinedOutput() | ||
if err != nil { | ||
log.Fatalf("Running %v: %v, %s", cmd.Args, err, out) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/tailscale/go-mod-archiver | ||
|
||
go 1.16 |
Empty file.