Skip to content
This repository has been archived by the owner on Jan 31, 2025. It is now read-only.

Commit

Permalink
Merge pull request #23 from ipfs/address-feedback
Browse files Browse the repository at this point in the history
feat: make env optional, prompt when not set
  • Loading branch information
galargh authored Nov 14, 2023
2 parents dbc6bfc + daf7454 commit 16cde2b
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 92 deletions.
74 changes: 31 additions & 43 deletions .env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ github_token="$GITHUB_TOKEN"
if [[ -z "$github_token" ]]; then
echo "Please provide a GitHub token. You can create one at:"
echo " https://github.com/settings/tokens/new?scopes=repo,read:user,user:email,write:packages"
echo "If you don't want the value to be stored in a file, leave it empty and you will be prompted for it later."
echo "GitHub token: "
read -s github_token
fi
if [[ -z "$github_token" ]]; then
echo "GitHub token is required"
exit 1
fi

github_user_name="$GITHUB_USER_NAME"
if [[ -z "$github_user_name" ]]; then
Expand All @@ -19,13 +16,10 @@ fi
if [[ -z "$github_user_name" ]]; then
echo "Please provide a GitHub user name. You can also configure it with:"
echo " git config --global user.name \"Your Name\""
echo "If you don't want the value to be stored in a file, leave it empty and you will be prompted for it later."
echo "GitHub user name: "
read github_user_name
fi
if [[ -z "$github_user_name" ]]; then
echo "GitHub user name is required"
exit 1
fi

github_user_email="$GITHUB_USER_EMAIL"
if [[ -z "$github_user_email" ]]; then
Expand All @@ -34,13 +28,10 @@ fi
if [[ -z "$github_user_email" ]]; then
echo "Please provide a GitHub user email. You can also configure it with:"
echo " git config --global user.email \"Your Email\""
echo "If you don't want the value to be stored in a file, leave it empty and you will be prompted for it later."
echo "GitHub user email: "
read github_user_email
fi
if [[ -z "$github_user_email" ]]; then
echo "GitHub user email is required"
exit 1
fi

if [[ -z "$NO_GPG" ]]; then
gpg_id="$GPG_ID"
Expand All @@ -50,55 +41,50 @@ if [[ -z "$NO_GPG" ]]; then
if [[ -z "$gpg_id" ]]; then
echo "Please provide a GPG ID. You can also configure it by following:"
echo " https://docs.github.com/en/authentication/managing-commit-signature-verification/telling-git-about-your-signing-key"
echo "If you don't want the value to be stored in a file, leave it empty and you will be prompted for it later."
echo "GPG ID: "
read gpg_id
fi
if [[ -z "$gpg_id" ]]; then
echo "GPG ID is required"
exit 1
fi

gpg_passphrase="$GPG_PASSPHRASE"
if [[ -z "$gpg_passphrase" ]]; then
echo "Please provide a GPG passphrase for the key $gpg_id."
echo "GPG passphrase: "
read -s gpg_passphrase
fi
if [[ -z "$gpg_passphrase" ]]; then
echo "GPG passphrase is required"
exit 1
fi

gpg_key="$GPG_KEY"
if [[ -z "$gpg_key" ]]; then
gpg_key="$(gpg --armor --pinentry-mode=loopback --passphrase "$gpg_passphrase" --export-secret-key "$gpg_id" -w0 | base64 -w0)"
fi
if [[ -z "$gpg_key" ]]; then
echo "GPG key is required"
exit 1
if [[ -n "$gpg_id" ]]; then
gpg_passphrase="$GPG_PASSPHRASE"
if [[ -z "$gpg_passphrase" ]]; then
echo "Please provide a GPG passphrase for the key $gpg_id."
echo "If you don't want the value to be stored in a file, leave it empty and you will be prompted for it later."
echo "GPG passphrase: "
read -s gpg_passphrase
fi
if [[ -n "$gpg_passphrase" ]]; then
gpg_key="$GPG_KEY"
if [[ -z "$gpg_key" ]]; then
gpg_key="$(gpg --armor --pinentry-mode=loopback --passphrase "$gpg_passphrase" --export-secret-key "$gpg_id" -w0 | base64 -w0)"
fi
fi
fi
fi

if [[ -z "$NO_MATRIX" ]]; then
matrix_url="$MATRIX_URL"
if [[ -z "$matrix_url" ]]; then
echo "Please provide a Matrix URL. For example: https://matrix-client.matrix.org/"
echo "If you don't want the value to be stored in a file, leave it empty and you will be prompted for it later."
echo "Matrix URL: "
read matrix_url
fi
matrix_user="$MATRIX_USER"
if [[ -z "$matrix_user" ]]; then
echo "Please provide a Matrix username."
echo "If you don't want the value to be stored in a file, leave it empty and you will be prompted for it later."
echo "Matrix username: "
read matrix_user
fi
if [[ -z "$matrix_user" ]]; then
echo "Matrix username is required"
exit 1
fi

matrix_password="$MATRIX_PASSWORD"
if [[ -z "$matrix_password" ]]; then
echo "Please provide a Matrix password."
echo "If you don't want the value to be stored in a file, leave it empty and you will be prompted for it later."
echo "Matrix password: "
read -s matrix_password
fi
if [[ -z "$matrix_password" ]]; then
echo "Matrix password is required"
exit 1
fi
fi

export GITHUB_TOKEN="$github_token"
Expand All @@ -110,6 +96,8 @@ export GPG_ID="$gpg_id"
export GPG_PASSPHRASE="$gpg_passphrase"
export GPG_KEY="$gpg_key"

export NO_MATRIX="$NO_MATRIX"
export MATRIX_URL="$matrix_url"
export MATRIX_USER="$matrix_user"
export MATRIX_PASSWORD="$matrix_password"

Expand Down
1 change: 1 addition & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ GPG_KEY=$GPG_KEY
GPG_PASSPHRASE=$GPG_PASSPHRASE

NO_MATRIX=$NO_MATRIX
MATRIX_URL=$MATRIX_URL
MATRIX_USER=$MATRIX_USER
MATRIX_PASSWORD=$MATRIX_PASSWORD
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ make kubeleaser

2. Create the .env file with your credentials

_NOTE_: You can skip this step if you want to be prompted for the credentials instead.

```bash
make env
```
Expand Down
17 changes: 4 additions & 13 deletions actions/prepare_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,9 @@ func (ctx PrepareBranch) MkReleaseLog() error {
filename := fmt.Sprintf("docs/changelogs/%s.md", ctx.Version.MajorMinor())
branch := repos.Kubo.VersionReleaseBranch(ctx.Version)

name := os.Getenv("GITHUB_USER_NAME")
if name == "" {
name = "Kubo Releaser"
}
email := os.Getenv("GITHUB_USER_EMAIL")
if email == "" {
email = "noreply+kuboreleaser@ipfs.tech"
}
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
return fmt.Errorf("GITHUB_TOKEN not set")
}
name := util.GetenvPrompt("GITHUB_USER_NAME")
email := util.GetenvPrompt("GITHUB_USER_EMAIL")
token := util.GetenvPromptSecret("GITHUB_TOKEN", "The token should have the following scopes: ... Please enter the token:")

err := os.MkdirAll(rootname, 0755)
if err != nil {
Expand Down Expand Up @@ -289,7 +280,7 @@ func (ctx PrepareBranch) Run() error {
return err
}

fmt.Printf(`Your release PR is ready at %s\n`, pr.GetHTMLURL())
fmt.Printf("Your release PR is ready at %s\n", pr.GetHTMLURL())

// TODO: check for conflicts and tell the user to resolve them
// or resolve them automatically with git merge origin/release -X ours
Expand Down
2 changes: 1 addition & 1 deletion cmd/kuboreleaser/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func main() {
&cli.BoolFlag{
Name: "skip-matrix",
Usage: "Do not use Matrix client",
Value: os.Getenv("NO_MATRIX") != "",
Value: util.GetenvBool("NO_MATRIX"),
},
},
Action: func(c *cli.Context) error {
Expand Down
26 changes: 7 additions & 19 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,27 @@ type Client struct {
}

func NewClient() (*Client, error) {
name := os.Getenv("GITHUB_USER_NAME")
if name == "" {
name = "Kubo Releaser"
}
email := os.Getenv("GITHUB_USER_EMAIL")
if email == "" {
email = "noreply+kuboreleaser@ipfs.tech"
}
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
return nil, fmt.Errorf("GITHUB_TOKEN not set")
}
name := util.GetenvPrompt("GITHUB_USER_NAME")
email := util.GetenvPrompt("GITHUB_USER_EMAIL")
token := util.GetenvPromptSecret("GITHUB_TOKEN", "The token should have the following scopes: ... Please enter the token:")

// create HeaderAuth
auth, err := NewHeaderAuth(token)
if err != nil {
return nil, err
}

disabled := os.Getenv("NO_GPG")
if disabled != "" {
disabled := util.GetenvBool("NO_GPG")
if disabled {
return &Client{
name: name,
email: email,
auth: auth,
}, nil
}

key64 := os.Getenv("GPG_KEY")
if key64 == "" {
return nil, fmt.Errorf("GPG_KEY not set")
}
pass := os.Getenv("GPG_PASSPHRASE")
key64 := util.GetenvPromptSecret("GPG_KEY", "The key should be base64 encoded. Please enter the key:")
pass := util.GetenvPromptSecret("GPG_PASSPHRASE")

// create OpenPGP Entity
key, err := base64.StdEncoding.DecodeString(key64)
Expand Down
7 changes: 2 additions & 5 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"fmt"
"io"
"net/http"
"os"
"sort"
"strings"

"github.com/ipfs/kuboreleaser/util"
"github.com/shurcooL/githubv4"
log "github.com/sirupsen/logrus"

Expand All @@ -24,10 +24,7 @@ type Client struct {
}

func NewClient() (*Client, error) {
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
return nil, fmt.Errorf("GITHUB_TOKEN not set")
}
token := util.GetenvPromptSecret("GITHUB_TOKEN", "The token should have the following scopes: ... Please enter the token:")

sts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/urfave/cli/v2 v2.23.7
golang.org/x/mod v0.7.0
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be
golang.org/x/term v0.2.0
)

require (
Expand Down
16 changes: 5 additions & 11 deletions matrix/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package matrix
import (
"fmt"
"net/url"
"os"

"github.com/ipfs/kuboreleaser/util"
"github.com/matrix-org/gomatrix"
log "github.com/sirupsen/logrus"
)
Expand All @@ -14,16 +14,10 @@ type Client struct {
}

func NewClient() (*Client, error) {
url := os.Getenv("MATRIX_URL")
if url == "" {
url = "https://matrix-client.matrix.org/"
}
user := os.Getenv("MATRIX_USER")
if user == "" {
return nil, fmt.Errorf("MATRIX_USER not set")
}
token := os.Getenv("MATRIX_TOKEN")
password := os.Getenv("MATRIX_PASSWORD")
url := util.GetenvPrompt("MATRIX_URL")
user := util.GetenvPrompt("MATRIX_USER")
token := util.GetenvPromptSecret("MATRIX_TOKEN", "If you don't have a token, you can leave it blank and use a password instead. Please enter the token:")
password := util.GetenvPromptSecret("MATRIX_PASSWORD", "If you don't have a password, you can leave it blank and use a token instead. Please enter the password:")
if token == "" && password == "" {
return nil, fmt.Errorf("MATRIX_TOKEN nor MATRIX_PASSWORD are set")
}
Expand Down
51 changes: 51 additions & 0 deletions util/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package util

import (
"fmt"
"os"
"strings"

"golang.org/x/term"
)

func Getenv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}

func GetenvBool(key string) bool {
return strings.ToLower(Getenv(key, "false")) == "true"
}

func GetenvPrompt(key string, prompt ...string) string {
value := Getenv(key, "")
for value == "" {
if len(prompt) > 0 {
fmt.Printf("%s is not set. %s: ", key, prompt[0])
} else {
fmt.Printf("%s is not set. Please enter a value: ", key)
}
fmt.Scanln(&value)
}
return value
}

func GetenvPromptSecret(key string, prompt ...string) string {
value := Getenv(key, "")
for value == "" {
if len(prompt) > 0 {
fmt.Printf("%s is not set. %s: ", key, prompt[0])
} else {
fmt.Printf("%s is not set. Please enter a secret value: ", key)
}
bytes, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
panic(err)
}
fmt.Println()
value = string(bytes)
}
return value
}

0 comments on commit 16cde2b

Please sign in to comment.