Skip to content
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

Write errors to stderr #166

Merged
merged 3 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
strategy:
matrix:
os: [ ubuntu-latest , macos-latest, windows-latest ]
go-version: [ '1.21', '1.22' ]
go-version: [ '1.22', '1.23' ]
steps:
- name: Configure git
run: git config --global core.autocrlf false # required on Windows
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.21'
go-version: '1.23'
- name: Test
uses: ./.github/actions/test
docker:
Expand Down Expand Up @@ -67,7 +67,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.21'
go-version: '1.23'
- name: Setup
run: |
sudo apt update
Expand Down Expand Up @@ -101,7 +101,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.21'
go-version: '1.23'
- name: Setup
env:
APPLE_CERT_ID: ${{ secrets.APPLE_CERT_ID }}
Expand Down Expand Up @@ -169,7 +169,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.21'
go-version: '1.23'
- name: Setup
shell: bash
run: |
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM docker.io/library/golang:1.21 AS builder
FROM docker.io/library/golang:1.23 AS builder

WORKDIR /walletd

Expand Down
31 changes: 16 additions & 15 deletions cmd/walletd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func readPasswordInput(context string) string {
fmt.Printf("%s: ", context)
input, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
stdoutFatalError("Could not read input: " + err.Error())
fatalError(fmt.Errorf("could not read password input: %w", err))
}
fmt.Println("")
return string(input)
Expand All @@ -30,7 +30,7 @@ func readInput(context string) string {
r := bufio.NewReader(os.Stdin)
input, err := r.ReadString('\n')
if err != nil {
stdoutFatalError("Could not read input: " + err.Error())
fatalError(fmt.Errorf("could not read input: %w", err))
}
return strings.TrimSpace(input)
}
Expand Down Expand Up @@ -84,12 +84,6 @@ func promptYesNo(question string) bool {
return strings.EqualFold(answer, "yes")
}

// stdoutFatalError prints an error message to stdout and exits with a 1 exit code.
func stdoutFatalError(msg string) {
stdoutError(msg)
os.Exit(1)
}

// stdoutError prints an error message to stdout
func stdoutError(msg string) {
if cfg.Log.StdOut.EnableANSI {
Expand Down Expand Up @@ -122,7 +116,7 @@ func setDataDirectory() {

dir, err := filepath.Abs(cfg.Directory)
if err != nil {
stdoutFatalError("Could not get absolute path of data directory: " + err.Error())
fatalError(fmt.Errorf("failed to get absolute path of data directory: %w", err))
}

fmt.Println("The data directory is where walletd will store its metadata and consensus data.")
Expand Down Expand Up @@ -200,12 +194,13 @@ func setAdvancedConfig() {
fmt.Println("This cannot be changed later without resetting walletd.")
fmt.Printf("Currently %q\n", cfg.Index.Mode)
mode := readInput(`Enter index mode ("personal" or "full")`)
if strings.EqualFold(mode, "personal") {
switch {
case strings.EqualFold(mode, "personal"):
cfg.Index.Mode = wallet.IndexModePersonal
} else if strings.EqualFold(mode, "full") {
case strings.EqualFold(mode, "full"):
cfg.Index.Mode = wallet.IndexModeFull
} else {
stdoutFatalError("Invalid index mode: " + mode)
default:
fatalError(fmt.Errorf("invalid index mode: %q", mode))
}

fmt.Println("")
Expand Down Expand Up @@ -242,14 +237,20 @@ func buildConfig() {
// write the config file
f, err := os.Create(configPath)
if err != nil {
stdoutFatalError("failed to create config file: " + err.Error())
fatalError(fmt.Errorf("failed to create config file: %w", err))
return
}
defer f.Close()

enc := yaml.NewEncoder(f)
if err := enc.Encode(cfg); err != nil {
stdoutFatalError("failed to encode config file: " + err.Error())
fatalError(fmt.Errorf("failed to encode config file: %w", err))
return
} else if err := f.Sync(); err != nil {
fatalError(fmt.Errorf("failed to sync config file: %w", err))
return
} else if err := f.Close(); err != nil {
fatalError(fmt.Errorf("failed to close config file: %w", err))
return
}
}
11 changes: 8 additions & 3 deletions cmd/walletd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ func mustSetAPIPassword() {
}
}

func fatalError(err error) {
os.Stderr.WriteString(err.Error() + "\n")
os.Exit(1)
}

// tryLoadConfig loads the config file specified by the WALLETD_CONFIG_FILE. If
// the config file does not exist, it will not be loaded.
func tryLoadConfig() {
Expand All @@ -126,7 +131,7 @@ func tryLoadConfig() {

f, err := os.Open(configPath)
if err != nil {
stdoutFatalError("failed to open config file: " + err.Error())
fatalError(fmt.Errorf("failed to open config file: %w", err))
return
}
defer f.Close()
Expand Down Expand Up @@ -237,7 +242,7 @@ func main() {
defer cancel()

if err := os.MkdirAll(cfg.Directory, 0700); err != nil {
stdoutFatalError("failed to create directory: " + err.Error())
fatalError(fmt.Errorf("failed to create data directory: %w", err))
}

mustSetAPIPassword()
Expand Down Expand Up @@ -284,7 +289,7 @@ func main() {

fileWriter, closeFn, err := zap.Open(cfg.Log.File.Path)
if err != nil {
stdoutFatalError("failed to open log file: " + err.Error())
fatalError(fmt.Errorf("failed to open log file: %w", err))
return
}
defer closeFn()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.22.5
require (
github.com/mattn/go-sqlite3 v1.14.22
go.sia.tech/core v0.4.4-0.20240814175157-ebc804c7119c
go.sia.tech/coreutils v0.2.6-0.20240814175830-40722f814395
go.sia.tech/coreutils v0.2.6-0.20240814205841-6bd57953a01b
go.sia.tech/jape v0.12.0
go.sia.tech/web/walletd v0.22.3
go.uber.org/zap v1.27.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ go.etcd.io/bbolt v1.3.10 h1:+BqfJTcCzTItrop8mq/lbzL8wSGtj94UO/3U31shqG0=
go.etcd.io/bbolt v1.3.10/go.mod h1:bK3UQLPJZly7IlNmV7uVHJDxfe5aK9Ll93e/74Y9oEQ=
go.sia.tech/core v0.4.4-0.20240814175157-ebc804c7119c h1:HJuHf6pBV9GOseVs3Yby3xbYzV8vZWTcsgrO2UGgQW8=
go.sia.tech/core v0.4.4-0.20240814175157-ebc804c7119c/go.mod h1:Zuq0Tn2aIXJyO0bjGu8cMeVWe+vwQnUfZhG1LCmjD5c=
go.sia.tech/coreutils v0.2.6-0.20240814175830-40722f814395 h1:MRK96OSDdxOXbHDRWH8LjYgTshUKUBDJyvD1Z/O1y80=
go.sia.tech/coreutils v0.2.6-0.20240814175830-40722f814395/go.mod h1:TjQITC7A7u3sX22sN54SmcPcn+YmnodEqzNElAA7G/s=
go.sia.tech/coreutils v0.2.6-0.20240814205841-6bd57953a01b h1:iV7PdyUf7CC6slo4CgY+XuJ6gRS/HtZGjzLVm383rTo=
go.sia.tech/coreutils v0.2.6-0.20240814205841-6bd57953a01b/go.mod h1:TjQITC7A7u3sX22sN54SmcPcn+YmnodEqzNElAA7G/s=
go.sia.tech/jape v0.12.0 h1:13fBi7c5X8zxTQ05Cd9ZsIfRJgdvGoZqbEzH861z7BU=
go.sia.tech/jape v0.12.0/go.mod h1:wU+h6Wh5olDjkPXjF0tbZ1GDgoZ6VTi4naFw91yyWC4=
go.sia.tech/mux v1.2.0 h1:ofa1Us9mdymBbGMY2XH/lSpY8itFsKIo/Aq8zwe+GHU=
Expand Down