Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
chiefMarlin committed Aug 11, 2023
1 parent 894d4f4 commit 37622d5
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 16 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module releaseMaker
go 1.20

require (
github.com/8ff/tuna v0.0.0-20230811173825-52af88c52674
github.com/google/go-github/v39 v39.2.0
golang.org/x/oauth2 v0.11.0
)
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
github.com/8ff/tuna v0.0.0-20230727181017-4fb2b65dac5a h1:W9Woe24Epz7OVCK5BXBsgilGyyT9F7gKxC2/Uj3LtOs=
github.com/8ff/tuna v0.0.0-20230727181017-4fb2b65dac5a/go.mod h1:brULTDkAKe2Ut39W20RPVcc0M6MhaHLTS/oGJiC5tVs=
github.com/8ff/tuna v0.0.0-20230811171600-4e4176964a26 h1:w8cZ/CDrumGmdA7ltOMeTkOAx9neKW99w55VbkatbqI=
github.com/8ff/tuna v0.0.0-20230811171600-4e4176964a26/go.mod h1:brULTDkAKe2Ut39W20RPVcc0M6MhaHLTS/oGJiC5tVs=
github.com/8ff/tuna v0.0.0-20230811173825-52af88c52674 h1:9L0K8szFUXJ0V71/I5YJeCmOXVhgU0+v0+9nf8mHqG0=
github.com/8ff/tuna v0.0.0-20230811173825-52af88c52674/go.mod h1:brULTDkAKe2Ut39W20RPVcc0M6MhaHLTS/oGJiC5tVs=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
Expand Down
126 changes: 110 additions & 16 deletions releaseMaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import (
"context"
"fmt"
"os"
"runtime"
"strings"
"time"

"github.com/8ff/tuna"
"github.com/google/go-github/v39/github"
"golang.org/x/oauth2"
)

var Version string

func CreateNewRelease(client *github.Client, ctx context.Context, owner, repo string, newRelease *github.RepositoryRelease) (*github.RepositoryRelease, error) {
release, _, err := client.Repositories.CreateRelease(ctx, owner, repo, newRelease)
if err != nil {
Expand Down Expand Up @@ -85,9 +89,12 @@ func DeleteReleaseByTag(client *github.Client, ctx context.Context, owner, repo,
func printUsage() {
fmt.Fprintf(os.Stderr, "Usage: %s [command] [arguments]\n", os.Args[0])
fmt.Fprintln(os.Stderr, "Commands:")
fmt.Fprintln(os.Stderr, " create [owner/repo] [tag] [name] [body] - Create a new release")
fmt.Fprintln(os.Stderr, " upload [owner/repo] [tag] [file] [assetName] - Upload a file as an asset to an existing release")
fmt.Fprintln(os.Stderr, " delete [owner/repo] [tag] - Delete an existing release")
fmt.Fprintln(os.Stderr, " create, v [owner/repo] [tag] [name] [body] - Create a new release")
fmt.Fprintln(os.Stderr, " upload, u [owner/repo] [tag] [file] [assetName] - Upload a file as an asset to an existing release")
fmt.Fprintln(os.Stderr, " delete, d [owner/repo] [tag] - Delete an existing release")
fmt.Fprintln(os.Stderr, " replace, r [owner/repo] [tag] [file] [assetName] - Delete an existing release and upload a file as an asset to a new release")
fmt.Fprintln(os.Stderr, " selfUpdate - Update releaseMaker to the latest version")
fmt.Fprintln(os.Stderr, " version, v - Print the version of releaseMaker")
}

func createClient(token string) *github.Client {
Expand All @@ -102,21 +109,21 @@ func createClient(token string) *github.Client {

func main() {
args := os.Args[1:]
if len(args) < 2 {
if len(args) < 1 {
printUsage()
os.Exit(2)
}

// Read TOKEN from env
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
fmt.Println("GITHUB_TOKEN is not set")
os.Exit(2)
}

switch args[0] {
case "create":
if len(args) < 6 {
case "create", "c":
// Read TOKEN from env
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
fmt.Println("GITHUB_TOKEN is not set")
os.Exit(2)
}

if len(args) < 5 {
fmt.Fprintf(os.Stderr, "Usage: %s create [owner/repo] [tag] [name] [body]\n", os.Args[0])
os.Exit(2)
}
Expand Down Expand Up @@ -158,8 +165,15 @@ func main() {

fmt.Println("Release created successfully!")
os.Exit(0)
case "upload":
if len(args) < 5 {
case "upload", "u":
// Read TOKEN from env
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
fmt.Println("GITHUB_TOKEN is not set")
os.Exit(2)
}

if len(args) < 4 {
fmt.Fprintf(os.Stderr, "Usage: %s upload [owner/repo] [tag] [file]\n", os.Args[0])
os.Exit(2)
}
Expand Down Expand Up @@ -193,7 +207,14 @@ func main() {

fmt.Println("Release uploaded successfully!")
os.Exit(0)
case "delete":
case "delete", "d":
// Read TOKEN from env
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
fmt.Println("GITHUB_TOKEN is not set")
os.Exit(2)
}

if len(args) < 3 {
fmt.Fprintf(os.Stderr, "Usage: %s delete [owner/repo] [tag]\n", os.Args[0])
os.Exit(2)
Expand Down Expand Up @@ -226,6 +247,79 @@ func main() {

fmt.Println("Release deleted successfully!")
os.Exit(0)

case "replace", "r":
// Read TOKEN from env
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
fmt.Println("GITHUB_TOKEN is not set")
os.Exit(2)
}

if len(args) < 5 {
fmt.Fprintf(os.Stderr, "Usage: %s replace [owner/repo] [tag] [name] [body]\n", os.Args[0])
os.Exit(2)
}
ownerRepo := strings.Split(args[1], "/")
if len(ownerRepo) != 2 {
fmt.Fprintf(os.Stderr, "Invalid owner/repo argument: %s\n", args[1])
os.Exit(2)
}
owner := ownerRepo[0]
repo := ownerRepo[1]
// Create an authenticated client
client := createClient(token)

// Read release tag, name, body from args
tagName := args[2]
releaseName := args[3]
releaseBody := args[4]

// Check for empty args
if owner == "" || repo == "" || tagName == "" || releaseName == "" || releaseBody == "" {
fmt.Fprintf(os.Stderr, "Invalid arguments: %s\n", args[1:])
os.Exit(2)
}

// Attempt to delete the release first, ignoring errors
_ = DeleteReleaseByTag(client, context.Background(), owner, repo, tagName)

// Define the new release information
newRelease := &github.RepositoryRelease{
TagName: github.String(tagName),
Name: github.String(releaseName),
Body: github.String(releaseBody),
Prerelease: github.Bool(false),
}

// Create a new release
_, err := CreateNewRelease(client, context.Background(), owner, repo, newRelease)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

fmt.Println("Release replaced successfully!")
os.Exit(0)
case "selfUpdate":
// Determine OS and ARCH
osRelease := runtime.GOOS
arch := runtime.GOARCH

// Build URL
e := tuna.SelfUpdate(fmt.Sprintf("https://github.com/8ff/releaseMaker/releases/download/latest/releaseMaker.%s.%s", osRelease, arch))
if e != nil {
fmt.Println(e)
os.Exit(1)
}

fmt.Println("Updated!")
case "version", "v":
fmt.Printf("%s\n", Version)
os.Exit(0)
case "help", "h":
printUsage()
os.Exit(0)
default:
printUsage()
os.Exit(2)
Expand Down

0 comments on commit 37622d5

Please sign in to comment.