-
Notifications
You must be signed in to change notification settings - Fork 860
Add release tagger tool #7230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add release tagger tool #7230
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
const mainModule = "github.com/uber/cadence" | ||
|
||
func generateTags(version string, goListOutput string, root string) (string, error) { | ||
tags := []string{version} | ||
|
||
for _, line := range strings.Split(goListOutput, "\n") { | ||
parts := strings.Split(line, "\t") | ||
if len(parts) != 2 { | ||
continue | ||
} | ||
modulePath, moduleDir := parts[0], parts[1] | ||
|
||
if strings.HasPrefix(modulePath, mainModule+"/") { | ||
relPath, err := filepath.Rel(root, moduleDir) | ||
if err != nil { | ||
return "", fmt.Errorf("error getting relative path for %s: %v", moduleDir, err) | ||
} | ||
tags = append(tags, fmt.Sprintf("%s/%s", relPath, version)) | ||
} | ||
} | ||
Comment on lines
+17
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. frustratingly hard in bash, yea. tbh I think minor go scripts like this are reasonable, and they have the extreme benefit of being far more likely to be cross-platform by accident. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO anything that has branching, loops or string processing should not be written in bash - it becomes impossible to read so fast. |
||
|
||
if len(tags) == 0 { | ||
return "", nil | ||
} | ||
tagsStr := strings.Join(tags, " ") | ||
return fmt.Sprintf("git tag %s\ngit push origin %s\n", tagsStr, tagsStr), nil | ||
} | ||
|
||
func main() { | ||
if len(os.Args) < 2 { | ||
fmt.Println("Usage: go run main.go <version>") | ||
os.Exit(1) | ||
} | ||
version := os.Args[1] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably worth doing a https://pkg.go.dev/golang.org/x/mod/semver#IsValid check to make sure it's a valid semver? |
||
|
||
cmd := exec.Command("go", "list", "-m", "-f", "{{.Path}}\t{{.Dir}}", "all") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is probably fine to use, but as an fyi: this can probably trigger downloads and be kinda slow. might want to limit it to like 1s and give up (and print stderr) if it takes too long. (it does not appear to need the modules to be downloaded, but it could still trigger e.g. a go-toolchain download-and-install during which it'll just hang silently) that said: I suppose this probably is the canonically correct way to get this info 🤔 tags need to be based on the relative path, not whatever the module names happen to be, so 👍 https://go.dev/doc/modules/managing-source#multiple-module-source |
||
var out bytes.Buffer | ||
cmd.Stdout = &out | ||
err := cmd.Run() | ||
if err != nil { | ||
fmt.Printf("Error running 'go list': %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
root, err := os.Getwd() | ||
if err != nil { | ||
fmt.Printf("Error getting current directory: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
output, err := generateTags(version, out.String(), root) | ||
if err != nil { | ||
fmt.Printf("Error generating tags: %v\n", err) | ||
os.Exit(1) | ||
} | ||
fmt.Print(output) | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you do end up using x/mod/semver, this is likely better as
as that way it'll rebuild on go / mod changes.
as long as it's just stdlib it doesn't really matter what builds it though.